NMSBoxes output with top_k parameter
Hi all,
the top_K
parameter in the cv2.dnn.NMSBoxes
correspond to the maximum number of bounding-boxes to return right ?
Like if we know how many object we expect in the image.
If that's the case, I have an unexpected output when setting top_k to a value >0.
With the default value (<0), I have a sensible set of bounding-boxes (on the left below).
However with the same parameters, except setting top_k to an actual value, here 4, I get only the 2 top detections and not the top 4. Why is that ?
The bounding boxes with index 4, 100, 72 and 17 are indeed the one I want, so I can use top_k<0 and select the top_4 afterwards but I though setting top_K in the NMS could stop the NMS earlier and thus save time.
I am using
- opencv-python-headless 4.3.0.36 (installed via pip)
- python 3.7.4
on a win10 machine
Thanks !
EDIT: Test code
import cv2
listBoxes = [
(946, 784, 414, 400),
(1525, 968, 414, 400),
(1533, 960, 414, 400),
(947, 784, 400, 414),
(1173, 1354, 414, 400),
(1459, 474, 400, 414),
(1021, 888, 414, 400),
(1450, 492, 400, 414),
(1398, 889, 414, 400),
(1005, 872, 400, 414),
(686, 1367, 414, 400),
(697, 1371, 414, 400),
(694, 1371, 414, 400),
(1128, 1377, 414, 400),
(950, 787, 414, 400),
(1438, 481, 414, 400),
(1064, 1238, 414, 400),
(1455, 485, 414, 400),
(111, 787, 414, 400),
(109, 782, 414, 400)]
listScores = [
1.0,
0.5919371843338013,
0.5894666314125061,
0.5689446926116943,
0.5510676503181458,
0.5381054282188416,
0.5311822891235352,
0.5298448204994202,
0.5123124122619629,
0.511840283870697,
0.5080571174621582,
0.5080069303512573,
0.5079731941223145,
0.5000045895576477,
0.49151238799095154,
0.4728872776031494,
0.4612887501716614,
0.4540329873561859,
0.4483684003353119,
0.44806933403015137]
'''
# Cast boxes to float (optional)
convertBox = lambda box : list(map(float, box))
listBoxes = list(map(convertBox, listBoxes))
print (listBoxes)
'''
indexes1 = cv2.dnn.NMSBoxes(listBoxes, listScores, score_threshold=0.4, nms_threshold=0.3, top_k=5)
print(indexes1)
print("-------")
indexes2 = cv2.dnn.NMSBoxes(listBoxes, listScores, score_threshold=0.4, nms_threshold=0.3)
print(indexes2)
can you try to come up with a small, reproducable example ?
I added a test code, with 2 calls to NMSBoxes: with and without the top_k parameters. With top_k=5, it returns only 3 detections.
I'm wondering if it could be related to this PR https://github.com/opencv/opencv/pull..., but it should be fixed in 4.3.0 isn't it ?
nice example !
using latest master, i get: