This project demonstrates how to use MediaPipe's Object Detection model along with OpenCV for real-time object detection on images. The code loads an object detection model, processes an input image, and visualizes the detected objects by drawing bounding boxes and category labels.
Before running the code, ensure you have the following installed:
-
Python 3.7 or later
-
Dependencies: Install the required libraries using pip:
pip install mediapipe opencv-python-headless numpy
-
MediaPipe Model: Download the required
model.tflite
file for object detection and place it in the project directory. Update themodel_asset_path
in the code if necessary. -
Image File: Ensure you have an image at
./train/images/IMG_0509.jpg
or update the file path in the script.
-
Import Libraries:
mediapipe
for object detection.opencv-python
for image processing.numpy
for array manipulations.
-
Object Detector Initialization: The MediaPipe Object Detector is initialized with the
model.tflite
file and configured to process images. -
Image Preprocessing:
- Load the image using OpenCV.
- Convert it from BGR to RGB format for compatibility with MediaPipe.
- Ensure the image is in the correct format (uint8 with shape
(H, W, 3)
).
-
Object Detection: The image is passed to the detector, and the results are processed to draw bounding boxes and labels on the image.
-
Results Visualization: The processed image with detections is displayed using OpenCV.
- Clone or download this repository.
- Place your object detection model (
model.tflite
) in the root directory. - Place your test image in the
./train/images/
directory or update the path in the script. - Run the script:
python object_detection.py
- View the output image in a pop-up window. Press any key to close it.
Detected objects will be highlighted with green bounding boxes, and their categories along with confidence scores will be displayed on the image.
- If the image fails to load, a
ValueError
is raised. - The script includes checks for valid image format and dtype. If these are not met, appropriate error messages are shown.
- Errors during MediaPipe image creation or object detection are handled and displayed.
- Python
- MediaPipe
- OpenCV
- NumPy
To install the dependencies, run:
pip install mediapipe opencv-python-headless numpy
- Change Input Image: Replace the file path in
cv2.imread()
to point to a different image. - Adjust Model: Replace
model.tflite
with a different MediaPipe model if needed. - Modify Confidence Threshold: Change the
if confidence >= 0.50
line to adjust the detection threshold. - Bounding Box Color and Style: Edit the
cv2.rectangle
andcv2.putText
parameters to customize the visualization.
This project is open-source and free to use for personal and educational purposes. Check the respective licenses for MediaPipe and OpenCV if using in commercial projects.