videocam

Experiment 6: Live Video Streaming (Capstone)

OpenCV · Python · Kafka

arrow_back Back to Experiments
Edge AI / Computer Vision

Streaming Webcam Frames

In real-world Edge AI applications (like self-driving cars or security systems), we stream video frames to cloud servers for analysis. Here, we'll use OpenCV and Kafka to stream your webcam!

flag Aim

To capture live video frames using a webcam, compress them, serialize them into bytes, and stream them over a Kafka topic to a remote consumer that reconstructs and displays the video.

data_object Theory: OpenCV + Kafka

Video is just a sequence of image arrays. We capture an image, encode it to JPEG (to save bandwidth), and send the raw bytes over Kafka.

Producer (Capture & Encode)
import cv2

camera = cv2.VideoCapture(0)
ret, frame = camera.read()
# Compress frame to JPEG
_, buffer = cv2.imencode('.jpg', frame)
producer.send('video-stream', buffer.tobytes())
Consumer (Decode & Render)
import cv2, numpy as np

for msg in consumer:
    # Convert bytes back to Image
    nparr = np.frombuffer(msg.value, np.uint8)
    frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    cv2.imshow('Video', frame)

checklist Mission Milestones

0 / 4 Done
radio_button_unchecked 1. Start the Kafka Broker
radio_button_unchecked 2. Run the Camera Producer
radio_button_unchecked 3. Run the Video Consumer
radio_button_unchecked 4. Stream at 2 FPS (Hint: change time.sleep(0.1) to 0.5)
Terminal 1 — Broker (192.168.1.10)
Offline
KRaft Broker — Ready to initialize.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Powered by Simulated Python & OpenCV Engine
terminal Console Output
Awaiting execution...
visibility Video Feeds
PRODUCER CAM
CONSUMER STREAM
FPS: 0 BW: 0 KB/s