View Code


import cv2
import numpy as np
import pyttsx3
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input, decode_predictions

# Initialize the text-to-speech engine
engine = pyttsx3.init()

# Load MobileNetV2 model pre-trained on ImageNet
model = MobileNetV2(weights='imagenet')

def say(text):
    engine.say(text)
    engine.runAndWait()

def recognize_object():
    cap = cv2.VideoCapture(0)  # Use the default camera (0)
    if not cap.isOpened():
        say("Unable to access the camera.")
        print("Unable to access the camera.")
        return

    say("Please hold the object in front of the camera.")
    while True:
        ret, frame = cap.read()
        if not ret:
            continue

        # Display the frame to the user
        cv2.imshow('Object Recognition', frame)

        # Press 'q' to capture the image and exit
        if cv2.waitKey(1) & 0xFF == ord('q'):
            print("Capturing image...")
            break

    cap.release()
    cv2.destroyAllWindows()

    # Preprocess the captured frame
    img = cv2.resize(frame, (224, 224))  # Resize image to 224x224
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # Convert image to RGB
    img = preprocess_input(np.expand_dims(img, axis=0))  # Preprocess image for MobileNetV2

    # Predict the object in the image
    preds = model.predict(img)
    decoded_preds = decode_predictions(preds, top=3)[0]  # Decode the top 3 predictions

    print("Top predictions:")
    for i, pred in enumerate(decoded_preds):
        object_name = pred[1]  # Get the object name (e.g., "dog")
        confidence = pred[2]  # Get the confidence score
        print(f"{i + 1}. {object_name} with {confidence * 100:.2f}% confidence.")
        say(f"Prediction {i + 1}: I think this is a {object_name} with {confidence * 100:.2f}% confidence.")

if __name__ == '__main__':
    print("Starting object detection...")
    recognize_object()
    

Instructions for Running the Object Detection Code

Follow these steps to set up and run the object detection code on your system:

If you encounter any issues, verify your camera permissions and ensure the required libraries are correctly installed.

Object Detection Example