Simple, real-time object detection using Ultralytics YOLOv8 and OpenCV.
This repo gives you a single script (detect.py) that can:
- run YOLOv8 on your webcam, images, or videos
- overlay bounding boxes, FPS, and per-class counts
- optionally save annotated output to disk
- 🎥 Live webcam detection (
--source 0by default) - 🖼️ Image & video file support
- 📊 Per-class object counts overlay (e.g.
person: 3, car: 1) - ⚡ FPS overlay showing inference speed
- 💾 Optional saving of annotated images/videos (
--save,--out,--fps) - 🧠 YOLOv8 model selection via
--model(default:yolov8n.pt) - 🧪 Verbose logging for debugging (
--verbose)
- Python: 3.8 – 3.12
(avoid 3.13 for now, some wheels may be missing) - OS: Windows, Linux, or macOS
Python dependencies (in requirements.txt):
ultralytics>=8.2.0
opencv-python>=4.8.0
numpy>=1.24.0Install them with:
pip install -r requirements.txtBy default the script uses:
--model yolov8n.ptIf yolov8n.pt is not found locally, Ultralytics will automatically download it on first run.
You can also:
- place
yolov8n.ptin the project folder, or - use any other YOLOv8 model, e.g.
--model yolov8s.ptor a custom.ptfile.
git clone <your-repo-url>
cd <your-repo-folder>pip install -r requirements.txtpython detect.py --source 0 --show0= default webcam- add
--verboseif you want detailed logs
python detect.py --source path/to/image.jpg --show --save--show→ opens a window with detections--save→ writesimage_det.jpgnext to the original
python detect.py --source path/to/video.mp4 --show --save --fps 30--fpssets the output video FPS (for the saved file)
For convenience, the repo includes:
python detect.py --source 0 --show --verboseYou can:
- double-click
start.batin Explorer to start webcam detection with verbose logging - or edit it if you want a different default model/source.
detect.py supports these arguments:
| Option | Default | Description |
|---|---|---|
--source |
"0" |
Input source: webcam index (0, 1, …), image path, video path, or stream URL (RTSP/HTTP). |
--model |
yolov8n.pt |
YOLOv8 model weights file. Can be any .pt compatible with Ultralytics. |
--conf |
0.25 |
Confidence threshold (0–1). Higher = fewer, more confident detections. |
--imgsz |
640 |
Inference image size (pixels). |
--device |
auto | Device string, e.g. cpu, cuda:0. If omitted, Ultralytics auto-selects. |
--show |
False |
Show a window with live detections. |
--save |
False |
Save annotated image/video to disk. |
--out |
None |
Output path for --save. If omitted, a default name is chosen. |
--fps |
0 |
Target FPS for saved video; 0 = use source FPS (or 30 if unknown). |
--verbose |
False |
Print detailed startup and per-frame logs. |
-
Dependencies (
opencv-python,ultralytics,numpy) are imported lazily inensure_deps()so startup is fast. -
The script decides whether
--sourceis:- a webcam index (like
"0") - a file path (image/video)
- or a generic stream URL
- a webcam index (like
-
For images:
- runs
model.predict(...) - draws boxes + class labels
- overlays per-class counts
- optionally saves the annotated image
- runs
-
For video/streams:
- reads frames in a loop
- runs inference per frame
- overlays boxes, FPS, and object counts
- optionally shows a window and/or saves to a video file