6

First of all here is my code :

        image = cv2.imread(filePath)
        height, width, channels = image.shape
        
        # USing blob function of opencv to preprocess image
        blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416),
        swapRB=True, crop=False)
        #Detecting objects
        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.7:
                    # 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,score_threshold=0.4,nms_threshold=0.8,top_k=1)
                
        font = cv2.FONT_HERSHEY_PLAIN
        colors = np.random.uniform(0, 255, size=(len(classes), 3))
        labels = ['bicycle','car','motorbike','bus','truck']
        for i in range(len(boxes)):
            if i in indexes:
                label = str(classes[class_ids[i]])
                if label in labels:
                    x, y, w, h = boxes[i]
                    color = colors[class_ids[i]]
                    cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
                    cv2.putText(image, label, (x, y + 30), font, 2, color, 3)
        cv2.imshow(fileName,image)

My Question is : Isn't cv2.dnn.NMSBoxes is suppose to eliminate multiple bounding boxes? then why I still get output like sample below :

sample 1 of multiple bounding blocks

sample 2 of multiple bounding blocks

What I expected is something like below :

enter image description here

Did I do something wrong with my code? Is there any better alternative? Thank you very much for your help.

3
  • Try reducing the value of the nms threshold to values within 0.4 to 0.7 with 0.1 step increments and see if it makes a difference? Mar 19, 2021 at 18:24
  • @JiteshMalipeddi It works, but could you help explain to me what actually happen? Thanks
    – Albert
    Mar 20, 2021 at 2:49
  • I'll add details in the answer section Mar 20, 2021 at 2:51

1 Answer 1

8

The process of NMS goes like this
Input - A list of Proposal boxes B, corresponding confidence scores S and overlap threshold N
Output - A list of filtered proposals D

Algorithm/steps

  1. Select the proposal with highest confidence score, remove it from B and add it to the final proposal list D. (Initially D is empty)
  2. Now compare this proposal with all the proposals — calculate the IOU (Intersection over Union) of this proposal with every other proposal. If the IOU is greater than the threshold N, remove that proposal from B
  3. Again take the proposal with the highest confidence from the remaining proposals in B and remove it from B and add it to D
  4. Once again calculate the IOU of this proposal with all the proposals in B and eliminate the boxes which have high IOU than threshold
  5. This process is repeated until there are no more proposals left in B

The threshold that is being referred to here is nothing but the nms_threshold.
In the cv2.dnn.NMSBoxes function, nms_threshold is the IOU threshold used in non-maximum suppression.
So if you have a large value, you are enforcing two boxes to have a very high overlap (which is usually not the case) and the box will be removed only if it has an IOU more than 0.8 with another box. Since there's usually not this much overlap, the boxes won't be removed. Reducing this value will make it easier to remove redundant detections

Hope this makes sense

You can read more about Non-Maxima Suppresion here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.