Project 3: Real-time object detection with YOLOv8

Backend

The backend, built using OpenCV and the YOLOv8 model from the Ultralytics library, performs the following tasks:

  • Accepts raw video frames from the frontend.
  • If object detection is enabled, backend processes the frames with YOLOv8 and draws bounding boxes are drawn detected objects.
  • Returns processed frames (with or without annotations) back to the frontend.

import cv2
import numpy as np
from PIL import Image, ImageTk
from ultralytics import YOLO

class VideoStreamHandler:
	def __init__(self):
		self.cap = None
		self.model = YOLO("yolov8n.pt")
		self.detecting = False

	def start_stream(self):
		if self.cap is None:
			self.cap = cv2.VideoCapture(0)

	def get_frame(self):
		if self.cap and self.cap.isOpened():
			ret, frame = self.cap.read()
			if ret:
				if self.detecting:
					results = self.model(frame)  # YOLOv8 detection
					frame = results[0].plot()  # Annotate frame
				return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  #convert to RGB
		return None

	def enable_detection(self):
		self.detecting = True

	def disable_detection(self):
		self.detecting = False

	def stop_stream(self):
		if self.cap:
			self.cap.release()
			self.cap = None

	def get_black_frame(self):
		return np.zeros((480, 640, 3), dtype=np.uint8)  #black frame