Real-time webcam object detection for people, cars, cats, and dogs using Python, YOLOv8, and OpenCV
Python
9
13 commits
updated Oct 12, 2026
A real-time object detection application built with Python, YOLOv8, and OpenCV.
The application uses a pre-trained YOLOv8n model to detect four object classes — Person, Dog, Cat, and Car — from a live webcam feed. It displays bounding boxes, class labels, confidence scores, per-class object counts, and FPS information.
The project also includes multi-threaded frame capture and a bounded frame queue to help manage frame buffering during real-time inference.
Install the required packages:
pip install ultralytics opencv-python
python main.py
Or:
python3 main.py
Use python or python3 depending on your environment.
Press ESC to exit the application.
Camera capture runs in a separate thread from the inference loop, allowing frame acquisition and model processing to operate independently.
A bounded queue with a maximum size of two frames is used to limit buffering. When the queue becomes full, the capture thread removes the oldest queued frame before attempting to insert the latest frame.
If the queue fills again before the new frame can be inserted, the insertion is skipped rather than blocking the capture thread.
This approach is intended to reduce the accumulation of outdated frames, although end-to-end latency has not yet been benchmarked.
The application uses CPU inference on macOS and selects CUDA on Windows/Linux when PyTorch detects an available CUDA device. Otherwise, it falls back to CPU execution.
The YOLOv8 model's detections are filtered to four selected classes, keeping the displayed results focused on the intended use case.
Suggestions and contributions are welcome, particularly regarding real-time inference performance, frame buffering, and code architecture.
Real-time webcam object detection for people, cars, cats, and dogs using Python, YOLOv8, and OpenCV
Python
9
13 commits
updated Oct 12, 2026
A real-time object detection application built with Python, YOLOv8, and OpenCV.
The application uses a pre-trained YOLOv8n model to detect four object classes — Person, Dog, Cat, and Car — from a live webcam feed. It displays bounding boxes, class labels, confidence scores, per-class object counts, and FPS information.
The project also includes multi-threaded frame capture and a bounded frame queue to help manage frame buffering during real-time inference.
Install the required packages:
pip install ultralytics opencv-python
python main.py
Or:
python3 main.py
Use python or python3 depending on your environment.
Press ESC to exit the application.
Camera capture runs in a separate thread from the inference loop, allowing frame acquisition and model processing to operate independently.
A bounded queue with a maximum size of two frames is used to limit buffering. When the queue becomes full, the capture thread removes the oldest queued frame before attempting to insert the latest frame.
If the queue fills again before the new frame can be inserted, the insertion is skipped rather than blocking the capture thread.
This approach is intended to reduce the accumulation of outdated frames, although end-to-end latency has not yet been benchmarked.
The application uses CPU inference on macOS and selects CUDA on Windows/Linux when PyTorch detects an available CUDA device. Otherwise, it falls back to CPU execution.
The YOLOv8 model's detections are filtered to four selected classes, keeping the displayed results focused on the intended use case.
Suggestions and contributions are welcome, particularly regarding real-time inference performance, frame buffering, and code architecture.