Skip to content

SadeemAlBoqami/Object-recognition-from-video-with-opencv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Object-recognition-from-video-with-opencv

Running a YOLOv8-ready model to detect multiple objects within a video using OpenCV.

Overview:

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.

Requirements

  1. From anaconda>>base>>open terminal install these packages:
pip install ultralytics opencv-python 
  1. After that open vs code from terminal by typing:
   code .
  1. Open your project folder which contains the model code and video that will be tested.
  2. In same folder, create python file or download it (yolo_detect.py).
  3. 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.

The structure of the code and how it works:

  1. 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
  1. 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
  1. 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)
  1. Read the video frame by frame
while True:
    ret, frame = cap.read()
    if not ret:
        break
  1. Play the model on each Frame
results = model(frame)
  1. 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)
  1. Displaying the Frame to the user
    cv2.imshow("YOLOv8 Detection", frame)
  1. Exit when pressing q
 if cv2.waitKey(25) & 0xFF == ord('q'):
        break
  1. Cleaning the memory after the video ends
cap.release()
cv2.destroyAllWindows()

About

Running a YOLOv8-ready model to detect multiple objects within a video using OpenCV.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages