#Python –Detecting #Hololens in realtime in webcam feed using #ImageAI and #OpenCV (thanks to @OlafenwaMoses)


Let’s start with a very quick intro:

During the past months, I’ve been playing around with several Image Analysis tools. And ImageAI (see references) is one that deserves a full series of posts. Please take a look at the product and the source code in GitHub, and also please thank the one behind this: Moses Olafenwa (@OlafenwaMoses).

And now, my 2 cents. I’ve started to test ImageAI to create my own image detection models. Most of the times, this is a hard path to do, however ImageAI show me an interesting option.

… with the latest release of ImageAI v2.1.0, support for training your custom YOLOv3 models to detect literally any kind and number of objects is now fully supported, …

Wow! That’s mean that I can pick up my own set of images dataset and train on top of a YOLOv3 and use it as a trained model. Again, this is amazing.

So, I started to read the article [Train Object Detection AI with 6 lines of code, see references] where Olafenwa explains how to do this using a data set with almost 500 rows with images for Hololens and Oculus Rift.

The code is very simple and easy to read. There are also examples on how to analyze a single file, or a video, or even a camera feed. The output for the analysis can be also in a new file, in a processed video or even a full log file with the detected information.

I started to read the code samples and I realized that I’m missing a scenario:

Display the realtime feed from a webcam, analyze each webcam frame and if a device is found, add a frame to the realtime feed to display this.

I use OpenCV to access to my camera, and it took me some time to figure out how to convert my OpenCV2 camera frame to the format needed by ImageAI. At the end, thanks to the GitHub code I manage to create this (very slow but working) demo

As usual in this scenario, now it’s time to improve the performance and start testing with some tweaks to get a decent up and running App.

And of course, the code

# load HL detection model from imageAI
# open camera with openCV, analyze frame by frame
# draw a red frame around the detected object
from imageai.Detection.Custom import CustomObjectDetection
import os
import cv2
detector = CustomObjectDetection()
# init camera
execution_path = os.getcwd()
camera = cv2.VideoCapture(0)
while True:
# Grab a single frame of video
ret, frame = camera.read()
detected_image, detections = detector.detectObjectsFromImage(input_image=frame, input_type="array", output_type="array")
for detection in detections:
print(detection["name"], " : ", detection["percentage_probability"])
(x1, y1, x2, y2) = detection["box_points"]
print("x1: ", x1, " – y1: ", y1, " – x2: ", x2, " – y2: ", y2)
# frame for the detected object
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
# Draw a label with the detected object type below the frame
cv2.putText(frame, detection["name"], (x1 + 6, y1 6), font, 1.0, (255, 255, 255), 1)
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
# Release handle to the webcam

Happy coding!

Greetings @ Toronto

El Bruno


1 comment

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: