Object Detection with Python & YOLO | by Mauro Di Pietro | Towards Data Science

In this article, I will show how to play with computer vision and have a lot of fun with a few lines of code.
Computer vision is the field of Artificial Intelligence that studies how computers can gain high-level understanding from digital images or videos in order to produce numerical or symbolic information.
The main tasks of computer vision are image classification and object detection. The first one recognizes what an image is about and classifies it with a label. The best example would be to classify photos of dogs and cats. But what if the task is to count how many dogs and cats are in a picture? That would be an object detection problem. Object detection deals with detecting instances of semantic objects of a certain class in digital images and videos.
YOLO (You Only Look Once) is the fastest and therefore most used real-time object detection system. Basically, it applies a single neural network to the full image dividing it into regions, then the network predicts bounding boxes and probabilities for each region. In this article I will use a pre-trained YOLO model to do object detection, therefore if you want to know more about the neural network structure and how to train it, I recommend reading the original paper.
There are several ways to use a pre-trained model for computer vision, the most popular frameworks are Tensorflow and Keras. However, I think that ImageAI is the most convenient tool for a lazy programmer like I am. This package facilitates the usage of deep learning and computer vision as it provides very powerful and easy functions to perform object detection and image classification.
First of all, I will download the weights of the pre-trained YOLO from here (file “yolo.h5”) and store the file in some folder on my computer.
modelpath = "mycomputer/myfolder/yolo.h5"
Then, I can load the model very easily using ImageAI:
from imageai import Detectionyolo = Detection.ObjectDetection()yolo.setModelTypeAsYOLOv3()yolo.setModelPath(modelpath)yolo.loadModel()
Now the model is ready to make predictions, we just need data. I shall use the live video stream from my webcam to feed the model with real-world images.
You can access your device cameras with the package OpenCV, which provides a video capture object that handles everything related to the opening and closing of the webcam.
import cv2cam = cv2.VideoCapture(0) #0=front-cam, 1=back-camcam.set(cv2.CAP_PROP_FRAME_WIDTH, 1300)cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 1500)
Now we can start playing with YOLO. The model will be used to predict frame by frame as they are captured by the camera until the loop gets interrupted.
while True:    ## read frames    ret, img = cam.read()    ## predict yolo    img, preds = yolo.detectCustomObjectsFromImage(input_image=img,                       custom_objects=None, input_type="array",                      output_type="array",                      minimum_percentage_probability=70,                      display_percentage_probability=False,                      display_object_name=True)    ## display predictions    cv2.imshow("", img)    ## press q or Esc to quit        if (cv2.waitKey(1) & 0xFF == ord("q")) or (cv2.waitKey(1)==27):        break## close cameracam.release()cv2.destroyAllWindows()
Fun, right? And the model is doing pretty well too, even though it recognizes an orange as a “sports ball”.
I’m using a minimum percentage probability of 0.70, which means that we’re able to detect objects only when the model is at least 70% sure. Lowering the value shows more objects while increasing the value ensures objects with the highest accuracy are detected.
I hope you enjoyed it! Feel free to contact me for questions and feedback or just to share your interesting projects.
👉 Let’s Connect 👈