Search code examples
opencvcomputer-visionobject-detectionyolo

Poor object detection with OpenCV DNN and YOLOv3


OpenCV comes with object detection sample code for its DNN module.

I downloaded the YOLOv3 model and a sample image from the same COCO dataset it was trained on:

wget https://pjreddie.com/media/files/yolov3.weights  
wget https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3.cfg
wget http://farm8.staticflickr.com/7272/7745390240_0dcf7d107d_z.jpg

Here are the objects that are labeled in the scene:

labeled objects

Then I ran the sample code to detect objects in the unlabeled image:

python object_detection.py --model yolov3.weights --config yolov3.cfg --height 320 --input 7745390240_0dcf7d107d_z.jpg 

This draws rectangles around the objects it detects. But the detections are basically garbage.

detections


If I just use Darknet directly, here is what it labels:

darknet output


Solution

  • enter image description here

    The missing parameters seem to be --scale 0.00392 --rgb which come from this tutorial.

      --scale SCALE         Preprocess input image by multiplying on a scale
                            factor. (default: 1.0)
      --rgb                 Indicate that model works with RGB input images
                            instead BGR ones. (default: False)
    

    These are passed as parameters to cv2.dnn.blobFromImage.

    I think scale is not actually resizing the input image but converting each element of the image to a float in the [0.0, 1.0] range. 0.00392 is approximately 1/255.