OpenCV Object Detection Tutorial

Oct 9, 2024

Object Detection Tutorial with OpenCV

Introduction

  • Tutorial focuses on object detection using OpenCV.
  • The goal is to have the computer verbally identify objects (e.g., apple, orange, cell phone).

Dependencies Installation

  1. OpenCV Contrib Python

    • Command: pip install OpenCV-contrib-python
    • Contains extra libraries beyond basic OpenCV.
  2. CLIB

    • Command: pip install cvlib
    • Used for object detection.
  3. Text-to-Speech Libraries

    • Command: pip install gtts play sound
    • Used for verbal outputs.
  4. Pi Object C

    • Command: pip3 install pyobjC
    • Enhances sound playback efficiency.

Importing Libraries

  • Import necessary libraries:
    import cv2  
    import cvlib as cv  
    from cvlib.object_detection import draw_box  
    from gtts import gTTS  
    from playsound import playsound
    

Accessing Camera

  • Use cv2.VideoCapture to access the camera.
  • The index for the camera may vary (most use 0, but this tutorial uses 1).

Object Detection Loop

  1. Retrieve frames from the video feed:

    ret, frame = video.read()
    
  2. Detect objects with the following:

    boxes, labels, conf = cv.detect_common_objects(frame)
    
  3. Draw boxes around detected objects:

    output_image = draw_box(frame, boxes, labels, conf)
    
  4. Show the output image:

    cv2.imshow('Object Detection', output_image)
    
  5. Exit condition with key press (e.g., 'Q').

Handling Detected Labels

  • Create a list to store unique labels:
    labels = []
    
  • Add labels to the list if they are not already present.
  • Print the list of labels.

Constructing and Displaying Speech

  1. Create a function for speech:

    def speech(text):
        print(text)
        # Set language and save audio.
    
  2. Use string interpolation to format detected labels into natural language:

    • For example: "I found a person, an apple, a tie..."
    • Use lists and the join function for formatting.
  3. Save audio output as MP3 file and play it:

    output.save('./sounds/output.mp3')
    playsound('./sounds/output.mp3')
    

Conclusion

  • The program successfully detects objects and provides verbal feedback.
  • Encouragement for further questions and feedback in comments.
  • Reminder to like and subscribe for more tutorials.