Trained ML model with the camera module is not giving predictions

30 views Asked by At

I have trained CNN model and saved it in .h5 format but when I use this for prediction with the help of camera I am not able to predict , the camera opens but there is no predictions and other content Here is my code , I am actually learning ML so could you please solve this problem ?

Here is my code , I am actually learning ML so could you please solve this problem ? I am expecting to predict the image class when I show a image to laptop camera

import numpy as np
import cv2
from keras.models import load_model

# Load the model
model = load_model('C:/Users/Win/Downloads/traffic_classifier.h5')

# Parameters
frameWidth = 640         # CAMERA RESOLUTION
frameHeight = 480
brightness = 180
threshold = 0.5         # PROBABILITY THRESHOLD
font = cv2.FONT_HERSHEY_SIMPLEX

# SETUP THE VIDEO CAMERA
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, brightness)

def preprocessing(img):
    # Convert image to RGB format
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # Resize image to 30x30
    img = cv2.resize(img, (30, 30))
    # Normalize pixel values
    img = img / 255.0
    # Add batch dimension
    img = np.expand_dims(img, axis=0)
    return img

def getClassName(classNo):
    classes = {
        0: 'Speed Limit 20 km/h',
        1: 'Speed Limit 30 km/h',
        2: 'Speed Limit 50 km/h',
        3: 'Speed Limit 60 km/h',
        4: 'Speed Limit 70 km/h',
        5: 'Speed Limit 80 km/h',
        6: 'End of Speed Limit 80 km/h',
        7: 'Speed Limit 100 km/h',
        8: 'Speed Limit 120 km/h',
        9: 'No passing',
        10: 'No passing for vehicles over 3.5 metric tons',
        11: 'Right-of-way at the next intersection',
        12: 'Priority road',
        13: 'Yield',
        14: 'Stop',
        15: 'No vehicles',
        16: 'Vehicles over 3.5 metric tons prohibited',
        17: 'No entry',
        18: 'General caution',
        19: 'Dangerous curve to the left',
        20: 'Dangerous curve to the right',
        21: 'Double curve',
        22: 'Bumpy road',
        23: 'Slippery road',
        24: 'Road narrows on the right',
        25: 'Road work',
        26: 'Traffic signals',
        27: 'Pedestrians',
        28: 'Children crossing',
        29: 'Bicycles crossing',
        30: 'Beware of ice/snow',
        31: 'Wild animals crossing',
        32: 'End of all speed and passing limits',
        33: 'Turn right ahead',
        34: 'Turn left ahead',
        35: 'Ahead only',
        36: 'Go straight or right',
        37: 'Go straight or left',
        38: 'Keep right',
        39: 'Keep left',
        40: 'Roundabout mandatory',
        41: 'End of no passing',
        42: 'End of no passing by vehicles over 3.5 metric tons'
    }
    return classes[classNo]

while True:
    success, imgOriginal = cap.read()

    # Preprocess the image
    img = preprocessing(imgOriginal)

    # Predictions
    predictions = model.predict(img)
    classIndex = np.argmax(predictions, axis=1)
    probabilityValue = np.amax(predictions)

    # Display results if probability is above threshold
    if probabilityValue > threshold:
        class_name = getClassName(classIndex)
        cv2.putText(imgOriginal, "CLASS: " + class_name, (20, 35), font, 0.75, (0, 0, 255), 2, cv2.LINE_AA)
        cv2.putText(imgOriginal, "PROBABILITY: " + str(round(probabilityValue * 100, 2)) + "%", (20, 75), font, 0.75, (0, 0, 255), 2, cv2.LINE_AA)
        cv2.imshow("Result", imgOriginal)
    else:
        cv2.imshow("Processed Image", imgOriginal)

    if cv2.waitKey(1) & 0xFF == ord('q'):  # Check for 'q' key press to exit
        break

cap.release()
cv2.destroyAllWindows()
0

There are 0 answers