This project contains three simple Python programs that run object detection with YOLOv8 using OpenCV:
simple_object_detection.py
: Detect objects in a single image and show an annotated window.object_detection_from_video.py
: Detect objects from a video file (e.g.,traffic.mp4
).live_camera_feed.py
: Detect objects from your live webcam feed.
A small YOLOv8 model file yolov8n.pt
is included for quick tests.
- Python 3.8–3.12 on Windows
- A working webcam (for
live_camera_feed.py
) - A video file (e.g.,
traffic.mp4
) forobject_detection_from_video.py
pip install -r requirements.txt
This installs ultralytics
(YOLOv8) and opencv-python
.
Rename the environment files as ".env" from ".env.example" and enter your API_KEYS
simple_object_detection.py
— image inference scriptobject_detection_from_video.py
— video file inference scriptlive_camera_feed.py
— webcam/live camera inference scriptyolov8n.pt
— YOLOv8n model weightstraffic.mp4
— example input videoperson-image.jpg
,fruits.jpg
— example images
Make sure your virtual environment is activated if you created one.
Run:
python simple_object_detection.py
Defaults to person-image.jpg
. To change the image, edit the script and set the file you want:
image = cv2.imread("person-image.jpg")
# image = cv2.imread("fruits.jpg")
The program will open a window titled "Annotated Image". Press q
or close the window to exit.
Run:
python object_detection_from_video.py
- The script reads
traffic.mp4
by default:Change the path to your own video file if needed.cap = cv2.VideoCapture("traffic.mp4")
- The annotated video is resized to 800×600 for display and shown in a window titled "Video Object Detection".
- Press
q
to quit.
Run:
python live_camera_feed.py
- Uses the default camera index
0
:If you have multiple cameras or the default doesn’t work, trycap = cv2.VideoCapture(0)
1
,2
, etc. - A window titled "Live Camera Feed" will appear. Press
q
to quit.
yolov8n.pt
is the small, fast model. You can switch to other YOLOv8 models (e.g., yolov8s.pt
, yolov8m.pt
) by placing the weights in the project folder and changing:
model = YOLO("yolov8n.pt")
- GPU (CUDA) is used automatically if available via PyTorch; otherwise, inference runs on CPU.
- Larger models may improve accuracy but will be slower.
- For best webcam performance, close other apps that use the camera.
- Camera window is black or doesn’t open:
- Another app may be using the camera. Close it and try again.
- Try a different camera index:
cv2.VideoCapture(1)
. - Ensure Windows camera privacy permissions allow desktop apps.
- Video file won’t open:
- Check the file path and file name in the script.
- Try an absolute path like
C:\\path\\to\\your_video.mp4
.
- Module not found (e.g., cv2):
- Reinstall dependencies:
pip install -r requirements.txt
(ensure your venv is active).
- Reinstall dependencies:
- Very slow on CPU:
- Stick with
yolov8n.pt
(already the smallest model). - Close other CPU-intensive applications.
- Stick with
- All programs use the same exit key: press
q
while the window is focused.
This sample is intended for educational and demonstration purposes.