Running a YOLOv8-ready model to detect multiple objects within a video using OpenCV.
This project uses the YOLOv8 Nano model integrated with OpenCV to detect multiple objects in real-time from video files. It's lightweight, fast, and easy to run.
- From anaconda>>base>>open terminal install these packages:
pip install ultralytics opencv-python
- After that open vs code from terminal by typing:
code .
- Open your project folder which contains the model code and video that will be tested.
- In same folder, create python file or download it (yolo_detect.py).
- Running your code, the output will appear as a video after recognizing the elements, you will notice that there is a box around each element with its name whose elements have been recognized by the model.
- Import libraries:
- cv2 library from opencv to read video, view images and draw boxes.
- Ultralytics.YOLO to download and run the YOLOv8 prototype.
import cv2
from ultralytics import YOLO
- Download the model: There are several versions of the YOLO model, multiple speeds and resolutions, I chose Nano, the lightweight version of the model, supporting more than 80 types of objects (people, cars, bicycles, etc.)
model = YOLO('yolov8n.pt') #It loads automatically the first time
- Open the video file and edit the video frame measurements: Cap is an object that represents the video.
cap = cv2.VideoCapture('test6.mp4')
cv2.namedWindow("YOLOv8 Detection", cv2.WINDOW_NORMAL)
cv2.resizeWindow("YOLOv8 Detection", 1280, 720)
- Read the video frame by frame
while True:
ret, frame = cap.read()
if not ret:
break
- Play the model on each Frame
results = model(frame)
- Extract objects, draw rectangle and text
for result in results:
boxes = result.boxes
for box in boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
conf = box.conf[0]
cls = int(box.cls[0])
label = model.names[cls]
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
text = f"{label} {conf:.2f}"
cv2.putText(frame, text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.6, (0, 255, 0), 2)
- Displaying the Frame to the user
cv2.imshow("YOLOv8 Detection", frame)
- Exit when pressing q
if cv2.waitKey(25) & 0xFF == ord('q'):
break
- Cleaning the memory after the video ends
cap.release()
cv2.destroyAllWindows()