Detecting objects on video in less than 5 minutes

In this tutorial, we'll show you how easy is detecting objects on video with the Abraia Python SDK. Moreover, you can easily customize the YOLOv8 detection model using Abraia DeepLab.

people detected

Follow these steps to achieve this:

Step 1: Install the Abraia Python SDK

You can install the package on Windows, Mac, and Linux:

python -m pip install -U abraia

Step 2: Load and run the object detection model

Import the detect module from abraia and load the yolov8n detection model. Then use "load_image" and run the model to detect the objects on the image. To show the results on the image, simple use the "render_results" function.

from abraia import detect

model_path = "multiple/camera/yolov8n.onnx"
model = detect.load_model(model_path)

img = detect.load_image('people-walking.png')
results = model.run(img, conf_threshold=0.5, iou_threshold=0.5)
img = detect.render_results(img, results)
detect.show_image(img)

Save the video before and use the class "Video" to create a video object to iterate on the frames, detecting the objects. As the frame is a "numpy" array and the model expected a "pillow" image as input, you need to convert from array to image, and from image to array to show the results.

from abraia import detect

model_path = "multiple/camera/yolov8n.onnx"
model = detect.load_model(model_path)

video = detect.Video('people-walking.mp4')
for frame in video:
    results = model.run(frame, conf_threshold=0.5, iou_threshold=0.5)
    frame = detect.render_results(frame, results)
    video.show(frame)

You can even run the model directly on the camera stream, just use "Video(0)" to use your webcam.

Step 3: Record the YOLOv8 detection results on video

To record the output video, you just need to add the output parameter when you declare the video object, and use the "write" method from video.

from abraia import detect

model_path = "multiple/camera/yolov8n.onnx"
model = detect.load_model(model_path)

video = detect.Video('people-walking.mp4', output='people-detected.mp4')
for frame in video:
    results = model.run(frame)
    frame = detect.render_results(frame, results)
    video.write(frame)

Congratulations! You have successfully run the "yolov8n" object detection model on a video. This tutorial provides a basic overview, and you can further explore advanced features, optimize your model, and fine-tune parameters based on your specific use case. Refer to DeepLab's contact email for more detailed information and features.