|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | """
|
3 |
| -Non-Maxima Supression (NMS) for match template |
| 3 | +Non-Maxima Supression (NMS) for match template. |
| 4 | +
|
4 | 5 | From a pool of bounding box each predicting possible object locations with a given score,
|
5 | 6 | the NMS removes the bounding boxes overlapping with a bounding box of higher score above the maxOverlap threshold
|
6 | 7 |
|
7 | 8 | This effectively removes redundant detections of the same object and still allow the detection of close objects (ie small possible overlap)
|
8 | 9 | The "possible" allowed overlap is set by the variable maxOverlap (between 0 and 1) which is the ratio Intersection over Union (IoU) area for a given pair of overlaping BBoxes
|
9 | 10 |
|
| 11 | +Since version 1.6.0 the NMS is assured by opencv cv2.dnn.NMSBoxes function |
10 | 12 |
|
11 | 13 | @author: Laurent Thomas
|
12 | 14 | """
|
|
15 | 17 |
|
16 | 18 |
|
17 | 19 | def NMS(tableHit, scoreThreshold=0.5, sortAscending=False, N_object=float("inf"), maxOverlap=0.5):
|
18 |
| - ''' |
19 |
| - Perform Non-Maxima supression : it compares the hits after maxima/minima detection, and removes the ones that are too close (too large overlap) |
20 |
| - This function works both with an optionnal threshold on the score, and number of detected bbox |
21 |
| -
|
22 |
| - if a scoreThreshold is specified, we first discard any hit below/above the threshold (depending on sortDescending) |
23 |
| - if sortDescending = True, the hit with score below the treshold are discarded (ie when high score means better prediction ex : Correlation) |
24 |
| - if sortDescending = False, the hit with score above the threshold are discared (ie when low score means better prediction ex : Distance measure) |
25 |
| -
|
26 |
| - Then the hit are ordered so that we have the best hits first. |
27 |
| - Then we iterate over the list of hits, taking one hit at a time and checking for overlap with the previous validated hit (the Final Hit list is directly iniitialised with the first best hit as there is no better hit with which to compare overlap) |
| 20 | + """ |
| 21 | + Perform Non-Maxima Supression (NMS). |
28 | 22 |
|
29 |
| - This iteration is terminate once we have collected N best hit, or if there are no more hit left to test for overlap |
| 23 | + it compares the hits after maxima/minima detection, and removes detections overlapping above the maxOverlap |
| 24 | + Also removes detections that do not satisfy a minimum score |
30 | 25 |
|
31 |
| - INPUT |
32 |
| - - tableHit : (Panda DataFrame) Each row is a hit, with columns "TemplateName"(String),"BBox"(x,y,width,height),"Score"(float) |
| 26 | + INPUT |
| 27 | + - tableHit : Pandas DataFrame |
| 28 | + List of potential detections as returned by MTM.findMatches. |
| 29 | + Each row is a hit, with columns "TemplateName"(String),"BBox"(x,y,width,height),"Score"(float). |
33 | 30 |
|
34 |
| - - scoreThreshold : Float (or None), used to remove hit with too low prediction score. |
35 |
| - If sortAscending=False (ie we use a correlation measure so we want to keep large scores) the scores above that threshold are kept |
36 |
| - If True (we use a difference measure ie we want to keep low score), the scores below that threshold are kept |
37 |
| - |
38 |
| - - N_object : maximum number of hit to return. Default=-1, ie return all hit passing NMS |
39 |
| - - maxOverlap : float between 0 and 1, the maximal overlap authorised between 2 bounding boxes, above this value, the bounding box of lower score is deleted |
40 |
| - - sortAscending : use True when low score means better prediction (Difference-based score), True otherwise (Correlation score) |
| 31 | + - scoreThreshold : Float, used to remove low score detections. |
| 32 | + If sortAscending=False, ie when best detections have high scores (correlation method), the detections with score below the threshold are discarded. |
| 33 | + If sortAscending=True, ie when best detections have low scores (difference method), the detections with score above the threshold are discarded. |
| 34 | + |
| 35 | + - sortAscending : Boolean |
| 36 | + use True when low score means better prediction (Difference-based score), False otherwise (Correlation score). |
| 37 | + |
| 38 | + - N_object : int or infinity/float("inf") |
| 39 | + maximum number of best detections to return, to use when the number of object is known. |
| 40 | + Otherwise Default=infinity, ie return all detections passing NMS. |
| 41 | + |
| 42 | + - maxOverlap : float between 0 and 1 |
| 43 | + the maximal overlap (IoU: Intersection over Union of the areas) authorised between 2 bounding boxes. |
| 44 | + Above this value, the bounding box of lower score is deleted. |
| 45 | + |
41 | 46 |
|
42 |
| - OUTPUT |
43 |
| - Panda DataFrame with best detection after NMS, it contains max N detection (but potentially less) |
44 |
| - ''' |
| 47 | + Returns |
| 48 | + ------- |
| 49 | + Panda DataFrame with best detection after NMS, it contains max N detections (but potentially less) |
| 50 | + """ |
45 | 51 | listBoxes = tableHit["BBox"].to_list()
|
46 | 52 | listScores = tableHit["Score"].to_list()
|
47 | 53 |
|
|
0 commit comments