Webcam Image Classification with Keras Model

This script uses OpenCV to capture images from a webcam, processes them using PIL and NumPy, and classifies the images using a pre-trained Keras model.
mail@pastecode.io avatar
unknown
python
9 days ago
2.3 kB
1
Indexable
Never
import cv2
import numpy as np
from keras.models import load_model
from PIL import Image, ImageOps

# Load the model
model = load_model(r"C:\Users\Student\Desktop\ArjunK\content\keras_model.h5", compile=False)

# Load the labels
class_names = open(r"C:\Users\Student\Desktop\ArjunK\content\labels.txt", "r").readlines()

# Function to preprocess image
def preprocess_image(image):
    size = (224, 224)
    image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
    image_array = np.asarray(image)
    normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
    return normalized_image_array

# Open a connection to the webcam
cap = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    
    # Check if frame is captured correctly
    if not ret:
        print("Failed to grab frame")
        break
    
    # Convert the frame to RGB (OpenCV uses BGR by default)
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    
    # Convert the frame to PIL format
    pil_frame = Image.fromarray(rgb_frame)
    
    # Preprocess the image
    preprocessed_image = preprocess_image(pil_frame)
    
    # Reshape and expand dimensions to fit the model input requirements
    input_data = np.expand_dims(preprocessed_image, axis=0)
    
    # Perform prediction
    predictions = model.predict(input_data)
    
    # Process predictions
    for i, prediction in enumerate(predictions):
        class_index = np.argmax(prediction)
        class_name = class_names[class_index].strip()
        confidence_score = prediction[class_index]
        
        # Draw bounding box around detected object
        if confidence_score > 0.5:  # Adjust confidence threshold as needed
            h, w, _ = frame.shape
            cv2.rectangle(frame, (0, 0), (w, h), (255, 0, 0), 2)
            cv2.putText(frame, f'{class_name}: {confidence_score:.2f}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
    
    # Display the frame with bounding boxes
    cv2.imshow('Object Detection', frame)
    
    # Break the loop if 'q' is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the webcam and close windows
cap.release()
cv2.destroyAllWindows()
Leave a Comment