This repository contains a simple Python script for real-time object detection using a pre-trained Keras model and OpenCV. The script captures video from the default camera (usually the webcam), processes each frame using the loaded Keras model, and displays the frame with the predicted object class label.
Before running the script, ensure you have the following dependencies installed:
- OpenCV (
cv2
) - NumPy (
numpy
) - TensorFlow (
tensorflow
) - A trained Keras model saved in
keras_model.h5
- A text file
labels.txt
containing the class labels corresponding to the model's output
You can install the required Python packages using the following command:
pip install opencv-python numpy tensorflow
-
Place your trained Keras model (
keras_model.h5
) and a text file with class labels (labels.txt
) in the same directory as the script. -
Update the
keras_model.h5
andlabels.txt
filenames in the script if they're named differently. -
Run the script using the following command:
python object_detection.py
-
A window will appear displaying the real-time video stream from your camera. The predicted class label for the detected object will be displayed on each frame.
-
To exit the script, press the 'q' key in the OpenCV window.
- The script assumes that your Keras model takes input images of size
(224, 224, 3)
and outputs class probabilities. - The camera feed can be adjusted by modifying the
cap = cv2.VideoCapture(0)
line. Replace0
with the index of your desired camera if you have multiple cameras connected. - Make sure your camera is accessible and working properly before running the script.
This script is inspired by tutorials and examples from the OpenCV and TensorFlow communities.