Real-Time Object Detection and Webcam Live Streaming on Raspberry Pi

If you’ve got an old USB webcam lying around and a Raspberry Pi collecting dust, here’s the perfect weekend project: turn them into a real-time object detection and web livestream system using Python, OpenCV, and the powerful YOLOv8 model!

This project is lightweight, beginner-friendly, and completely open-source. You can find the full code on GitHub here: Webcam-Livestream Repository

What You’ll Need

  • Raspberry Pi (any model with a USB port, but Pi 4 is ideal)
  • A USB webcam
  • MicroSD card with Raspberry Pi OS
  • Basic knowledge of Python

What This Project Does

This project allows you to:

  1. Capture video from a USB webcam in real-time.
  2. Detect objects in the video feed using YOLOv8 (a fast, accurate deep learning model).
  3. Serve the processed video stream to a browser using a lightweight web server.
  4. View it all live from your local network!

What This Project Does

This project allows you to:

  1. Capture video from a USB webcam in real-time.
  2. Detect objects in the video feed using YOLOv8 (a fast, accurate deep learning model).
  3. Serve the processed video stream to a browser using a lightweight web server.
  4. View it all live from your local network!

Under the Hood

Here’s how it works:

1. Real-Time Video with OpenCV

OpenCV grabs frames from your USB webcam and sends them for processing.

cap = cv2.VideoCapture(0)
ret, frame = cap.read()

2. YOLOv8 for Object Detection

I’m using the Ultralytics YOLOv8n model—it’s compact and fast enough for Raspberry Pi:

from ultralytics import YOLO
model = YOLO("yolov8n.pt")
results = model(frame)

3. Annotate and Stream

Bounding boxes are drawn on the frame, and the output is streamed using Python’s aiohttp server.

cv2.rectangle(...)
web.Response(body=jpeg_frame, content_type="image/jpeg")

For full detailed project: https://github.com/nickdu088/Webcam-Livestream

If you build something with it, let me know or contribute to the repo!