We’re going to learn in this tutorial YOLO object detection. Yolo is a deep learning algorythm which came out on may 2016 and it became quickly so popular because it’s so fast compared with the previous deep learning algorythm.
With yolo we can detect objects at a relatively high speed. With a GPU we would be able to process over 45 frames/second while with a CPU around a frame per second.
How to install YOLO?
Let’s clear up a few things. YOLO is a deep learning algorythm, so itself doesn’t need any installation, what we need instead is a deep learning framework where to run te algorythm.
Here I’m going to describe the 3 most used and known frameworks compatible with YOLO and the advantages and disadvantages of each one:
- Darknet : it’s the framework built from the developer of YOLO and made specifically for yolo.
Advantage: it’s fast, it can work with GPU or CPU
Disadvantage: it olny works with Linux os - Darkflow: it’s the adaptation of darknet to Tensorflow (another deep leanring framework).
Advantage: it’s fast, it can work with GPU or CPU, and it’s also compatible with Linux, Windows and Mac.
Disadvantage: the installation it’s really complex, especially on windows - Opencv: also opencv has a deep learning framework that works with YOLO. Just make sure you have opencv 3.4.2 at least.
Advantage: it works without needing to install anything except opencv.
Disadvantage: it only works with CPU, so you can’t get really high speed to process videos in real time.
How to use YOLO with Opencv
We will focus in this tutorial on how to use YOLO with Opencv. This is the best approach for beginners, to get quickly the algorythm working without doing complex installations.
Let’s start by importing the libraries Opencv and numpy and then we load the algorythm.
We import the classes:
import cv2 import numpy as np
We load the algorythm. The run the algorythm we need three files:
- Weight file: it’s the trained model, the core of the algorythm to detect the objects.
- Cfg file: it’s the configuration file, where there are all the settings of the algorythm.
- Name files: contains the name of the objects that the algorythm can detect.
# Load Yolo
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))
We then load the image where we want to perform the object detection and we also get its width and height.
# Loading image
img = cv2.imread("room_ser.jpg")
img = cv2.resize(img, None, fx=0.4, fy=0.4)
height, width, channels = img.shape
Now that we have the algorythm ready to work and also the image, it’s time to pass the image into the network and do the detection.
Keep in mind that we can’t use right away the full image on the network, but first we need it to convert it to blob. Blob it’s used to extract feature from the image and to resize them. YOLO accepts three sizes:
- 320×320 it’s small so less accuracy but better speed
- 609×609 it’s bigger so high accuracy and slow speed
- 416×416 it’s in the middle and you get a bit of both.
The outs on line 21 it’s the result of the detection. Outs is an array that conains all the informations about objects detected, their position and the confidence about the detection.
# Detecting objects blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False) net.setInput(blob) outs = net.forward(output_layers)
At this point the detection is done, and we only need to show the result on the screen.
We then loop trough the outs array, we calculate the confidence and we choose a confidence threshold.
On line 32 we set a threshold confidence of 0.5, if it’s greater we consider the object correctly detected, otherwise we skip it.
The threshold goes from 0 to 1. The closer to 1 the greater is the accuracy of the detection, while the closer to 0 the less is the accuracy but also it’s greater the number of the objects detected.
# Showing informations on the screen
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
When we perform the detection, it happens that we have more boxes for the same object, so we should use another function to remove this “noise”.
It’s called Non maximum suppresion.
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
We finally extract all the informations and show them on the screen.
- Box: contain the coordinates of the rectangle sorrounding the object detected.
- Label: it’s the name of the object detected
- Confidence: the confidence about the detection from 0 to 1.
font = cv2.FONT_HERSHEY_PLAIN
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
color = colors[i]
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
cv2.putText(img, label, (x, y + 30), font, 3, color, 3)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
It’s called Non maximum suppresion.
How to detect custom objects
To detect custom objects, you would need to create your custom YOLO model, instead of using the pretrained model.
To create a custom object detector, two steps are necessary:
- Create a dataset containing images of the objects you want to detect
- Train the YOLO model on that image dataset
For this purpose I recommend you to evaluate the purchase of my Object Detection course. You will find a dedicated lessons to easily train a custom object detector with YOLO and a notebook file that automatically configures itself for the training of multiple classes.

Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Developers to build efficient computer vision software.
Sir,
This is very useful for me while detecting and classifying objects in am image.
But, when I am applying your method on video, playback becomes very slow i.e, single frame per second. Please help me to tackle that delay in video playback.
Hi, that’s normal. You can’t have a high speed using the CPU, and at the moment the opencv deep learning framework supports only the CPU. You should use a different framework like darknet or darkflow with tensorflow and use them with a GPU to have a real time detection with high frame rates.
Anyway probably in the next video I will show some tips on how to improve the speed so that it can work faster also on the cpu.
When I click on the download button nothing happen, it doesn’t start downloading
I had the same problem too.
How do you detect objects in videos? i simply replaced the image file with video file and it shows errors
So funny
It doesn’t work with videos in the same ways.
I’ve got a tutorial for videos as well and realtime detection with webcam.
See this article: https://pysource.com/2019/07/08/yolo-real-time-detection-on-cpu/
its says an error that open cv has no attribute such as readnet
Hi, that happens because you’re using an old version of Opencv. Keep in mind that Readned works with opencv 3.4.2 or newer.
Having a problem with the code; it gives me an error: <<>> Any idea? Using Python 3.7.3 and opencv version 4.1.0.
Ok, so the comment section does not allow me to post such large amount of data. The error says something like “error: (-212:Parsing error) Failed to parse NetParameter file: yolov3.cfg in function”.
I met the same problem with you,so do you have solved it?
I was using an older version of yolov3.cfg and getting a (-215) error. Getting the latest one sorted it out. Don’t know if that’s helpful.
Mention absolute path for yolov3.weight and yolov3.cfg.
See more info: https://github.com/opencv/opencv/issues/10160
i am having the same problem, has anybody found a solution? @Sergio Canu
I am having the same problem. Does anybody have a solution for this? @Sergio Canu
can we make our own yolov3.WEIGHTS, yolov3.cfg, coco.names ?
if can…how to make it?
It would be really interesting to know. Why maybe you want to detect objects that are not present or maybe you are interested in having only a few objects so maybe you could streamline the file yolov3.WEIGHTS
I have same question please answer a lil explainatory
hi , thank you for such a great tutorial I really loved how simple you kept it and it actually worked.
however there is some part of the code that I still don’t get as to why they were used as in the purpose of this Code wasn’t clear to me :
1. layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] – 1] for i in net.getUnconnectedOutLayers()]
2.height, width, channels = img.shape
3.class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
4.indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
print(indexes)
I would also like to know how to work with a video…now I’ve a video sample how do I run this model on that video
i want to do this for real time using webcam can you plz help me
Watch the next video, He already did it with a video file and webcam. Best regards!
could you please leave a link, i can’t find it
Hi,thanks for this tuto, i want to know the rate of my model using test.txt.
error: OpenCV(3.4.3) /io/opencv/modules/dnn/src/layers/region_layer.cpp:93: error: (-215:Assertion failed) inputs[0][3] == (1 + coords + classes)*anchors in function ‘getMemoryShapes’
I am getting this error
sir,
this very helpful in my project and i want to do this for video please help me
how to do for video
net = cv2.dnn.readNet(“yolov3.weights”, “yolov3.cfg”)
what exactly does net object contains?
Hi Sergio Canu,
This is a very good tutorial and it helps me for my University project a lot. It gives me more FPS when I resize images in
blob = cv2.dnn.blobFromImage(frame, 0.00392, (220, 220), (0, 0, 0), True, crop=False) # 220,200.
I also debug each of line and calculate the execution time. it takes a long time to execute this line
outs = net.forward(output_layers)
* In yolo-v3 model it took an average 0.42 second
* In tiny-Yolo-v3 model it took an average 0.05 second
So, If you have any suggestions to reduce execution time in that line, please tall me what can I do or if you have any alternative method to increase FPS rate both tiny-yolo-v3 and yolo-v3 please suggest me.
And thanks again for this simple explanation.
Hi, i got some error when i want to detect some image with my own weights file. The error is “index 3 is out of bounds for axis 0 with size 3”. Can you help me to fix it? why do I get this error?
hi i m student and i want to make a project which includes AI,image processing and IOT
Hi, Thank you for this tutorial. would you plz give us the code useful for video in object tracking?
how I train our image dataset using Yolo for human face recognization.
can you tell me plz where is coco names file link ?
You can download the ZIP file from this tutorial, the file coco.names is there
yaa I got it
& Thankkkk you very much Sir !!! for helping us 🙂
Hi,
Can you please provide me the link for the zip file for the object detection code.
I am sorry sir ! Zip file is not available right now you have to copy paste this code as it is and just change the path of image, weight file etc you”ll get output for sure.
Great example, thanks. I have tried to execute it at windows and jupyter-notebook and its worked perfectly. But it need to notice, that example need to get anywhere weights and labels, I have find out it on github, may be its exist at that source link, havent checked it.
Very good example. I’m facing problem in one code which is related to openCV. Kindly check this code, This code is not related to your example but you can help me to solve it.
if filename is not None:
video = cv2.VideoCapture(filename)
assert video.isOpened()
else:
if disable_vidgear:
video = cv2.VideoCapture(camera_id)
assert video.isOpened()
else:
video = CamGear(camera_id).start()
fourcc = cv2.VideoWriter_fourcc(*’XVID’)
out = cv2.VideoWriter(‘result.avi’, fourcc, 20.0, (640, 480))
model = SimpleHRNet(
hrnet_c,
hrnet_j,
hrnet_weights,
)
while True:
if filename is not None or disable_vidgear:
ret, frame = video.read()
if not ret:
break
else:
frame = video.read()
if frame is None:
break
pts = model.predict(frame)
for i, pt in enumerate(pts):
frame = draw_points_and_skeleton(frame, pt)
if has_display:
cv2.imshow(‘frame.png’, frame)
out.write(frame)
k = cv2.waitKey(1)
if k == 27:
if disable_vidgear:
out.release()
video.release()
else:
video.stop()
break
else:
cv2.imwrite(‘frame.png’, frame)
I’m getting the error (UnboundLocalError: local variable ‘out’ referenced before assignment
) when I passed a video file but it works fine with webcam and OpenCV write the video. What is possible problem in the “out” variable regarding the passing a video file. Thanking you in anticipation
Hi Sergio
Great Code and thanks
In a few cases where detection fails I tried 609,609 in place of 416,416
Program crashes with errors as below
But if I use 608,608 then it takes approx 30 secs more but does not crash
Looking forward to your help
609×609 failure message
failed error(“OpenCV(3.4.4) /io/opencv/modules/dnn/src/layers/concat_layer.cpp:94: error: (-201:Incorrect size of input array) Inconsistent shape for ConcatLayer in function ‘getMemoryShapes’\n”,)
Change it to 608 x 608 from 609. It should work after that.
Refer to the cfg file for details.
Hi, your tutorial worked great, but on a raspberry pi 3 it is taking about 45 sec to display the result. Is their any way to speed up the processing time.
Hi,
Can you please let me know from where i can get .idea, yolov3.weights, yolov.cfg, and coco.names files. Can you please help me in getting them.
After downloading those two files (mentioned in tutorial)you will get zip file then just download it and you”ll get yolov.cfg and coco.names as well as image in example also
Can you please tell me how do I train yolo model using my datasets as well as how to measure distance along with obstacle?
Are you using a depth camera or are you trying to use simple monocular camera?
I am using stereo deph camera which will mounted on any wearable object
How about using “color = color[class_ids[i]]” instead of “color = color[i]” ?
because when length of boxes is over 80, an error occurs with “color = color[i]”.
Yes, thanks for pointing that out. There was a mistake on my tutorial, the color is for Class IDs, not for the objects.
I download these code and run that coding .but not detecting object
just showing the picture helpme.how todetect that object in images?
same wid me
Trying to run this code>>>
import cv2
import numpy as np
import time
# Load Yolo
net = cv2.dnn.readNet(“F:/Python/YOLO_REALTIME/cfg/yolov3.weights”)
net = cv2.dnn.readNet(“F:/Python/YOLO_REALTIME/cfg/yolov3.cfg”)
classes = []
with open(“coco.names”, “r”) as f:
classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] – 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))
# Loading camera
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_PLAIN
starting_time = time.time()
frame_id = 0
while True:
_, frame = cap.read()
frame_id += 1
height, width, channels = frame.shape
# Detecting objects
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
# Showing informations on the screen
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.2:
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x – w / 2)
y = int(center_y – h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.4, 0.3)
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
confidence = confidences[i]
color = colors[class_ids[i]]
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.rectangle(frame, (x, y), (x + w, y + 30), color, -1)
cv2.putText(frame, label + ” ” + str(round(confidence, 2)), (x, y + 30), font, 3, (255,255,255), 3)
elapsed_time = time.time() – starting_time
fps = frame_id / elapsed_time
cv2.putText(frame, “FPS: ” + str(round(fps, 2)), (10, 50), font, 3, (0, 0, 0), 3)
cv2.imshow(“Image”, frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
I got this error:
Exception has occurred: error
OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\dnn\src\darknet\darknet_importer.cpp:207: error: (-212:Parsing error) Failed to parse NetParameter file: in function ‘cv::dnn::dnn4_v20190902::readNetFromDarknet’
File “F:\Python\YOLO_REALTIME\real_time_yolo.py”, line 6, in
net = cv2.dnn.readNet(“F:/Python/YOLO_REALTIME/cfg/yolov3.weights”)
Can anyone help ?
Hi,
you should utilize the OpenCV version of 3.4.2.16,
I will hope, it is useful.
Hi ,
Can the yolov3.weights,which to my understanding is the pre-trained model here, be customized to detect only the objects that we want?
for ex- I have an image of a bill from a restaurant, and i want the program to detect only the price.
Thanks
cv2.error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\dnn\src\darknet\darknet_io.cpp:784: error: (-212:Parsing error) Failed to parse NetParameter file: in function ‘cv::dnn::ReadNetParamsFromCfgFileOrDie’
can anyone help me to solve this issue?
i got an error as follows. plz help me for get-out from this.
cv2.error: OpenCV(4.2.0) /io/opencv/modules/dnn/src/darknet/darknet_importer.cpp:207: error: (-212:Parsing error) Failed to parse NetParameter file: yolov3.cfg in function ‘readNetFromDarknet’
i got the same error if you solve this please help me
the output doesn’t showing the boundary boxes of an image
Does this work with cv 3.4?
I have subscribed but the download link is not being sent?
Hi sir, there are some error that occur when i run the code.
Traceback (most recent call last):
File “E:/FYP/YOLO/main.py”, line 51, in
label = str(classes[class_ids[i]])
IndexError: list index out of range
I tried to do it with two limited classes and got the error. Any solution ?
Awesome tutorial.
How to create a custom dataset to replace yolov3.weights?
thanks!
HI,
where do I get those three files yolov3.weights, yolov3.cfg, coco.names)
If you sign up with email, then you can see the download button for the files and the code file. Worked for me.
Super Guide <3 And your code writing is really simple and easy to work with. I will definitely follow more of your guides .
Thanks, your code is easy to understand.
Hi Sergio. I want to thank you cause this tuttorial couldn be more clear. I spent hours looking for any tutorial that explain clear all the frameworks and their relation and I couldnt. Thank you very very much
With Python 3 it works nicely, but with Python 2.7 I don’t get any predictions. Does anybody have any experience with that?
Hello sergio,
firstly thanks for the this tutorial. i have question. I wanna detect and track only person by yolo. is it possible ? could u help me about that?
thanks, best regards.
Hey Sergio!!
Do you have any tutorials to train YOLO with our own dataset for specific detection requirements?
when i run this in google colab environment no output is displayed. kindly help me. thanks
In this project, Can u help me to identify the colour of a object (Like a person image then if shirt colour is red then it also indicate it)
i get this error please help me
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\dnn\src\darknet\darknet_importer.cpp:207: error: (-212:Parsing error) Failed to parse NetParameter file: yolov3.cfg in function ‘cv::dnn::dnn4_v20191202::readNetFromDarknet’
Thank you for the great tutorial Sir.
Also can you tell me how to crop the detected part of the image?
how can you open the file yolov3.weights. and please could you make the vedio on how can we trained the object
can we use yolov4 on your code? do you have any solution for yolov4 on python?
Yolo v4 is not supported yet. we need to wait until the new Opencv release for that.
Hi I want to sign up and download the files mentioned in the video
Hi Ivan need to click on “Click here to download the source code”, just above the video
Thank you Sergio!
i find a error can not find a solve
“c:/Users/saber/AppData/Local/Temp/Temp1_yolo_object_detection.zip/yolo_object_detection.py”, line 31
center_x = int(detection[0] * width)
^
Hi Sergio! Thank you for nice tutorial !
How I know blobFromImage’s second parameter? (In this post, 0.00392)
As I know, blobFromImage’s second parameter is Scale Factor that scale our images by some factor, default value :1.0
0.00392 is fixed value? Or There is any calculating process?
I need some help for object detection to detect irregular speckles, were there any examples or suggestions for me? thanks
its nice work jobwell done. Will it be possible to encrypt the code for security purpose?
like to insert password or any restriction to avoid intruders to invade in.
Reply
Hi, i am having a error “‘cv2.dnn_Net’ object has no attribute ‘getUnconnectedLayers” . As i see the 1st point you raised have this “output_layers”. what is the reason behind my error? do i need to install any packages to to run (net.) ? can you please help me in this?
great work Sergio
i tried merging your 2 project i.e conveyor belt and custom object detection using yolo
there you used area to control belt but if i specify a particular class of object then what should i do?
it would be great if you reply
Hello, I couldn’t able to find the .cfg file. Please help me to get it.
It is taking too long to load the program
Hye..I try to run the code too but I got this error [cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6lylwdcz\opencv\modules\dnn\src\darknet\darknet_importer.cpp:207: error: (-212:Parsing error) Failed to parse NetParameter file: yolov3.cfg in function ‘cv::dnn::dnn4_v20200609::readNetFromDarknet’]
Could you help me pleaseee…T_T
How do I modify it to only detect to one class. I want it to only detect the person class. How do I do it?
Thank you for all the great videos and blogs.
Thank you for listing out the different tools for training models. Is Yolact/++ another implementation of YOLO?
I was able to use that in google colab to leverage train models and had good results with 300 pictures and GPU was able to train overnight. Also, with google colab prime, it doesn’t timeout for 24 hours, which give ample time to test some of the use cases.
Hey, tried running this thing, getting this error :
OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-kh7iq4w7\opencv\modules\dnn\src\darknet\darknet_importer.cpp:207: error: (-212:Parsing error) Failed to parse NetParameter file: yolov3.cfg in function ‘cv::dnn::dnn4_v20201117::readNetFromDarknet’
Help me out anyone….thanks in advance
Hi probably the .weights and .cfg files are not correctly imported. make sure that the path name doesn’t have any typos