Skip to content

Tutorials

YOLO object detection using Opencv with Python

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

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:

  1. Create a dataset containing images of the objects you want to detect
  2. 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.

96 comments

  1. 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.

    1. 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.

    1. 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”.

    1. 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

  2. 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

  3. 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

  4. 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.

  5. 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?

          1. 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.

  6. 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.

  7. 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

  8. 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”,)

  9. 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.

  10. 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.

    1. 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

  11. 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]”.

  12. I download these code and run that coding .but not detecting object  
    just showing the picture helpme.how todetect that object in images?

  13. 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 ?

  14. 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

  15. 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?

  16. 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’

  17. 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

  18. 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

  19. 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.

  20. 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’

  21. 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)
    ^

  22. 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?

  23. 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

  24. 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?

  25. 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

  26. 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

  27. 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.

  28. 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

Join the discussion