Live Webcam OCR using OpenCV and PySimpleGUI

Posted by


In this tutorial, we will be using OpenCV, PySimpleGUI, and Tesseract OCR to create a live webcam OCR application. This application will allow us to capture live images from a webcam, perform optical character recognition (OCR) on those images, and display the recognized text to the user using a simple GUI.

Step 1: Install the Required Libraries
Before we can get started with the project, we need to install the required libraries. We will be using OpenCV for image processing, PySimpleGUI for creating the GUI, and Tesseract OCR for optical character recognition.

You can install these libraries using pip:

pip install opencv-python
pip install opencv-python-headless
pip install pytesseract
pip install PySimpleGUI

Step 2: Set Up the GUI
Next, we will create a simple GUI using PySimpleGUI. We will set up a window with a live webcam feed and a text box to display the OCR results. Here is the basic code for the GUI:

import PySimpleGUI as sg
import cv2

layout = [
    [sg.Image(filename='', key='image')],
    [sg.Multiline(size=(60, 5), key='text')]
]

window = sg.Window('Live Webcam OCR', layout)

Step 3: Capture Live Webcam Feed
Now, we will write a function to capture the live webcam feed using OpenCV. This function will continuously capture frames from the webcam, perform OCR on the frames, and update the GUI with the recognized text. Here is the code for the function:

import pytesseract

def live_ocr():
    cap = cv2.VideoCapture(0)

    while True:
        ret, frame = cap.read()

        text = pytesseract.image_to_string(frame)
        window['text'].update(text)

        event, values = window.read(timeout=20)
        if event == sg.WIN_CLOSED:
            break

        ret, frame = cv2.imencode('.png', frame)
        window['image'].update(data=frame.tobytes())

    cap.release()
    cv2.destroyAllWindows()

Step 4: Run the Application
Finally, we will run the application by calling the live_ocr() function. This will open a window with a live webcam feed and display the OCR results in real-time. Here is the code to run the application:

live_ocr()

That’s it! You have successfully created a live webcam OCR application using OpenCV, PySimpleGUI, and Tesseract OCR. You can now use this application to perform optical character recognition on live webcam feeds. Feel free to customize the GUI and OCR settings to fit your needs.

0 0 votes
Article Rating

Leave a Reply

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@BlackAck90
2 hours ago

Hey, is this available as a compiled application?

@manoelvictorribeiropinto2474
2 hours ago

Nice Job!! I'm looking for this for my project, could you please share the code?

@РандомныйАккаунт-м3ю
2 hours ago

Hi! Nice video! Can you share the code please?

3
0
Would love your thoughts, please comment.x
()
x