Thursday, April 27, 2023

Handwritten Digit Recognition using OpenCV using Python

This code loads a pre-trained CNN model to recognize the digits, captures the video from the webcam, and analyzes each frame in real-time to recognize the digits. The code uses OpenCV to preprocess the images and extract the digits from the video frames. The recognized digits are printed on the video frames and displayed in real-time.

 

import cv2

import numpy as np

from keras.models import load_model


# Load the pre-trained CNN model

model = load_model('model.h5')


# Define the size of the image to be analyzed

IMG_SIZE = 28


# Define the function to preprocess the image

def preprocess_image(img):

    img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))

    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

    img = img.astype('float32') / 255.0

    img = np.reshape(img, (1, IMG_SIZE, IMG_SIZE, 1))

    return img


# Define the function to recognize the digit

def recognize_digit(img):

    img_processed = preprocess_image(img)

    digit = model.predict_classes(img_processed)[0]

    return digit


# Capture the video from the webcam

cap = cv2.VideoCapture(0)


while True:

    # Read a frame from the video stream

    ret, frame = cap.read()

    

    # Convert the frame to grayscale

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    

    # Threshold the grayscale image

    ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)

    

    # Find the contours in the thresholded image

    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    

    # Loop through all the contours

    for contour in contours:

        # Find the bounding rectangle of the contour

        x, y, w, h = cv2.boundingRect(contour)

        

        # Ignore contours that are too small

        if w < 10 or h < 10:

            continue

        

        # Draw the rectangle around the contour

        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

        

        # Extract the digit from the image

        digit_img = gray[y:y+h, x:x+w]

        

        # Recognize the digit

        digit = recognize_digit(digit_img)

        

        # Print the recognized digit on the frame

        cv2.putText(frame, str(digit), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

    

    # Display the video stream

    cv2.imshow('Handwritten Digit Recognition', frame)

    

    # Wait for a key press

    key = cv2.waitKey(1)

    

    # If the 'q' key is pressed, exit the loop

    if key == ord('q'):

        break


# Release the resources

cap.release()

cv2.destroyAllWindows()

 

No comments:

Post a Comment

ASP.NET Core

 Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...