Detecting people with YOLO and OpenCV.

Luana Gonçalves
2 min readNov 22, 2019

People detection is an important task for surveillance and business. The human visual system can quickly and effectively detect and recognize objects in images. Automating this task has been constantly discussed.

For detection, we’ll be using YOLOv3 in this post. In particular, YOLO trained on the COCO dataset. All you need is download the network, weights here and file named coco.names here.

First, we need to import the packages and read the image.

import cv2
import numpy as np
import matplotlib.pyplot as plt
image = plt.imread('nome_image.jpg')

Now, we’ll create a list with trained object names.

classes = None
with open(‘coco.names’, ‘r’) as f:
classes = [line.strip() for line in f.readlines()]

Then we’ll read the pre-trained model and config file, create and set the input blob for the network, run inference through the network and gather predictions from output layers.


net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
net.setInput(cv2.dnn.blobFromImage(image, 0.00392, (416,416), (0,0,0), True, crop=False))
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
outs = net.forward(output_layers)

Now, let’s create the bounding boxes.

class_ids = []
confidences = []
boxes = []
Width = image.shape[1]
Height = image.shape[0]
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.1:
center_x = int(detection[0] * Width)
center_y = int(detection[1] * Height)
w = int(detection[2] * Width)
h = int(detection[3] * Height)
x = center_x - w / 2
y = center_y - h / 2
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])

Here, we’ll check if the class of people detected is a person and display the image.

indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.1, 0.1)#check if is people detection
for i in indices:
i = i[0]
box = boxes[i]
if class_ids[i]==0:
label = str(classes[class_id])
cv2.rectangle(image, (round(box[0]),round(box[1])), (round(box[0]+box[2]),round(box[1]+box[3])), (0, 0, 0), 2)
cv2.putText(image, label, (round(box[0])-10,round(box[1])-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
plt.imshow(image)

Download the code here.

That’s all folks!!
P.S. If you like to read more stuff like this in Medium, consider supporting me and thousands of other writers by signing up for a membership. Or you can buy me a coffee here instead. Have a nice day :)

--

--

Luana Gonçalves

Specialist in machine learning, Stats and signal processing. Brazilian.