Real-Time Camera Streaming with Object Detection
Loading...
Searching...
No Matches
๐Ÿ“ธ Real-Time Camera Streaming with Object Detection

A real-time camera streaming system built on a Raspberry Pi that integrates a custom Linux kernel module, a multithreaded user-space capture pipeline, MJPEG HTTP streaming, and optional on-device object detection using TensorFlow Lite.

This project demonstrates end-to-end system design across kernel space and user space. It combines Linux interfaces (V4L2, IOCTL, MMAP) with concurrent data pipelines and computer vision inference.

๐Ÿ’ก Additional Notes: Each key feature includes a link to in-depth implementation notes that describe how the module was designed and built.

๐ŸŒฟ Branches

  • main - Stable, fully integrated version of the project
  • stream - Core camera capture and MJPEG streaming pipeline
  • stream_detect - Streaming pipeline with on-device object detection
  • gh-pages - Generated documentation hosted via github pages

๐Ÿ“ Documentation: The project includes comprehensive Doxygen documentation covering modules, functions, classes and detailed usage.

๐Ÿ‘‰ Explore the generated docs: Doxygen Documentation
๐Ÿ‘‰ Explore how the documentation was structured and written: Notes on Notion

๐Ÿ—๏ธ Key Features

  1. Custom Linux Kernel Module Notes on Notion
  • Character device driver exposing camera control and LED status signaling via ioctl
  • Well-defined kernel โ†” user-space interface with minimal surface area
  • GPIO-driven LED indicators reflecting real-time camera streaming state

GPIO ยท IOCTL ยท Character device ยท Linux kernel ยท kernel โ†” user space interface

  1. V4L2-Based Camera Pipeline Notes on Notion
  • Camera configuration using V4L2 API, including format negotiation and stream parameters
  • Buffer allocation and zero-copy frame access via memory mapping I/O (MMAP)
  • Continuous frame capture with explicit buffer dequeue and re-queue operations

V4L2 ยท Camera drivers ยท MMAP ยท Buffer management ยท Video streaming

  1. Multithreaded Producer-Consumer Architecture
  • Dedicated producer thread captures frames from the camera pipeline
  • Consumer thread streams encoded frames to connected HTTP clients
  • Lock-protected circular buffer ensure safe, low-latency data exchange between threads

Mutex ยท Semaphore ยท Circular buffers ยท Multithreading ยท Producer-consumer model

  1. MJPEG HTTP Streaming Notes on Notion
  • Lightweight HTTP server for serving video streams
  • Multipart MJPEG streaming compatible with web browsers and MJPEG clients

HTTP ยท MJPEG ยท Sockets ยท Lightweight server ยท Multipart streams

  1. Real-Time Object Detection Notes on Notion
  • Performs on-device inference using TensorFlow Lite on captured frames
  • Optimized for real-time edge deployment on the Raspberry Pi

Edge AI ยท Object Detection ยท Embedded ML ยท TensorFlow Lite ยท Real-time Inference.


๐Ÿงถ Threading Model

  • Producer Thread
    • Continously capture frames from the camera using V4L2
    • Converts raw frames to JPEG and pushes them into a circular buffer
    • Signals frame availability using a semaphore
  • Consumer Thread
    • Waits on the semaphore for available frames
    • Retrieves JPEG frames from the circular buffer
    • Streams JPEG frames to connected HTTP clients
    • Frees the memory of the processed frames

This design allows for producer thread to run continously, while a new consumer thread is spawned per client.

๐Ÿ—๏ธ High Level Flow

Block Diagram

Program Flow Explanation

main.c (Program Entry Point)
โ”œโ”€> Initialize camera module (opens custom kernel module)
โ”œโ”€> Start HTTP server for MJPEG streaming
โ””โ”€> Initialize threading pipeline
Producer Thread
โ”œโ”€> Capture frames from the camera (YUYV format)
โ”œโ”€> Convert YUYV โ†’ RGB
โ”œโ”€> Optional: Perform object detection on RGB frames
โ”œโ”€> Convert RGB โ†’ JPEG
โ””โ”€> Push JPEG frames into circular buffer
Consumer Thread
โ”œโ”€> Retrieve JPEG frames from circular buffer
โ””โ”€> Stream frames over HTTP (MJPEG)
โ”œโ”€> Display stream in browser

โš™๏ธ Hardware

  • Raspberry Pi 5 - primary embedded platform for kernel and user-space execution
  • Logitech C270 USB webcam - V4L2-compatible video capture device
  • GPIO-connected RGB LED - real-time system status indication
    • RED: idle state or error condition
    • GREEN: active camera streaming

๐Ÿงฑ Build and Run

  • make module: Build the kernel module
  • make user: Build the user-space application
  • make: Build both the kernel module & user-space application
  • sudo insmod kernel/cam_stream.ko: Insert the kernel module
  • sudo ./camera_client: Start the camera streaming application
  • http://<raspberry-pi-ip>/stream: Open broswer and view the stream

๐Ÿ“‚ Repository Structure

๐Ÿ“ pi_live_stream/
โ”‚
โ”œโ”€โ”€ docs/ # Doxygen-generated documentation
โ”‚
โ”œโ”€โ”€ kernel/ # Linux kernel module
โ”‚ โ”œโ”€โ”€ cam_stream.c # Character device + ioctl implementation
โ”‚ โ”œโ”€โ”€ cam_stream_ioctl.h # Shared ioctl interface (kernel โ†” user)
โ”‚ โ””โ”€โ”€ Makefile # Kernel module build rules
โ”‚
โ”œโ”€โ”€ src/ # User-space application
โ”‚ โ”œโ”€โ”€ camera/ # V4L2 camera capture & buffer management
โ”‚ โ”‚ โ”œโ”€โ”€ camera.c
โ”‚ โ”‚ โ””โ”€โ”€ camera.h
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ cb/ # Lock-protected circular buffer
โ”‚ โ”‚ โ”œโ”€โ”€ circular_buffer.c
โ”‚ โ”‚ โ””โ”€โ”€ circular_buffer.h
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ detection/ # Real-time object detection (TFLite)
โ”‚ โ”‚ โ”œโ”€โ”€ detection.cpp
โ”‚ โ”‚ โ”œโ”€โ”€ detection.h
โ”‚ โ”‚ โ””โ”€โ”€ models/
โ”‚ โ”‚ โ””โ”€โ”€ detect.tflite
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ http/ # HTTP server + MJPEG streaming
โ”‚ โ”‚ โ”œโ”€โ”€ http_server.c
โ”‚ โ”‚ โ”œโ”€โ”€ http_server.h
โ”‚ โ”‚ โ”œโ”€โ”€ mjpeg_stream.c
โ”‚ โ”‚ โ””โ”€โ”€ mjpeg_stream.h
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ image/ # Image processing & encoding
โ”‚ โ”‚ โ”œโ”€โ”€ image_encoder.c
โ”‚ โ”‚ โ”œโ”€โ”€ image_encoder.h
โ”‚ โ”‚ โ”œโ”€โ”€ image_processor.c
โ”‚ โ”‚ โ””โ”€โ”€ image_processor.h
โ”‚ โ”‚
โ”‚ โ””โ”€โ”€ main.c # Application entry point & thread orchestration
โ”‚
โ”œโ”€โ”€ README.md # Project overview & usage
โ””โ”€โ”€ Makefile # Builds kernel module and user-space client