Skip to content

Instantly share code, notes, and snippets.

@kittinan
Last active September 21, 2023 16:54
Show Gist options
  • Save kittinan/f6245584eec237eaea04a31fd1d8aabb to your computer and use it in GitHub Desktop.
Save kittinan/f6245584eec237eaea04a31fd1d8aabb to your computer and use it in GitHub Desktop.
Ultralytics YOLOv8 object detection model to detect objects in a video stream from a webcam and stream the annotated video to a virtual camera device.
"""
pip install ultralytics pyvirtualcam
Enable virtual camera on Linux:
modprobe v4l2loopback devices=1 max_buffers=2 exclusive_caps=1 card_label="VirtualCam"
# Remove v4l2
modprobe -r v4l2loopback
"""
from ultralytics import YOLO
from pyvirtualcam import PixelFormat
import cv2
import pyvirtualcam
VIDEO_INPUT_DEVICE = 0
# VIDEO_INPUT_DEVICE = "/home/tun/Videos/inbound499628027817585732.mp4"
VIRTUAL_CAM_DEVICE = "/dev/video2"
MODEL_PATH = "yolov8n.pt"
# Load the YOLOv8n model
model = YOLO(MODEL_PATH)
# Start the webcam
cap = cv2.VideoCapture(VIDEO_INPUT_DEVICE)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
print("[-] width: {} height: {} fps: {}".format(width, height, fps))
with pyvirtualcam.Camera(
width=width, height=height, fps=fps, device=VIRTUAL_CAM_DEVICE, fmt=PixelFormat.BGR
) as cam:
print(f"Virtual cam started: {cam.device} ({cam.width}x{cam.height} @ {cam.fps}fps)")
# Start streaming
while True:
# Read the next frame from the webcam
ret, frame = cap.read()
# If the frame is empty, break the loop
if not ret:
print("No frame")
break
# Detect objects in the frame
results = model(frame)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Write the frame to the v4l2loopback device
cam.send(annotated_frame)
cam.sleep_until_next_frame()
# Release the webcam and v4l2loopback device
cap.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment