Skip to content

Commit cbe1c92

Browse files
committed
Replace Descending with Ascending like pandas
1 parent 069e9a7 commit cbe1c92

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

MTM/NMS.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def computeIoU(BBox1,BBox2):
7777

7878

7979

80-
def NMS(tableHit, scoreThreshold=None, sortDescending=True, N_object=float("inf"), maxOverlap=0.5):
80+
def NMS(tableHit, scoreThreshold=None, sortAscending=False, N_object=float("inf"), maxOverlap=0.5):
8181
'''
8282
Perform Non-Maxima supression : it compares the hits after maxima/minima detection, and removes the ones that are too close (too large overlap)
8383
This function works both with an optionnal threshold on the score, and number of detected bbox
@@ -99,8 +99,8 @@ def NMS(tableHit, scoreThreshold=None, sortDescending=True, N_object=float("inf"
9999
While if we use sortDescending=False (we use a difference measure ie we want to keep low score), the scores below that threshold are kept
100100
101101
- N_object : number of best hit to return (by increasing score). Min=1, eventhough it does not really make sense to do NMS with only 1 hit
102-
- 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
103-
- sortDescending : use True when high score means better prediction, False otherwise (ex : if score is a difference measure, then the best prediction are low difference and we sort by ascending order)
102+
- 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
103+
- sortAscending : use True when low score means better prediction (Difference-based score), True otherwise (Correlation score)
104104
105105
OUTPUT
106106
Panda DataFrame with best detection after NMS, it contains max N detection (but potentially less)
@@ -110,17 +110,15 @@ def NMS(tableHit, scoreThreshold=None, sortDescending=True, N_object=float("inf"
110110
if scoreThreshold==None :
111111
threshTable = tableHit.copy() # copy to avoid modifying the input list in place
112112

113-
elif sortDescending : # We keep rows above the threshold
113+
elif not sortAscending : # We keep rows above the threshold
114114
threshTable = tableHit[ tableHit['Score']>=scoreThreshold ]
115115

116-
elif not sortDescending : # We keep hit below the threshold
116+
elif sortAscending : # We keep hit below the threshold
117117
threshTable = tableHit[ tableHit['Score']<=scoreThreshold ]
118118

119-
# Sort score to have best predictions first (important as we loop testing the best boxes against the other boxes)
120-
if sortDescending:
121-
threshTable.sort_values("Score", ascending=False, inplace=True) # Hit = [list of (x,y),score] - sort according to descending (best = high correlation)
122-
else:
123-
threshTable.sort_values("Score", ascending=True, inplace=True) # sort according to ascending score (best = small difference)
119+
# Sort score to have best predictions first (ie lower score if difference-based, higher score if correlation-based)
120+
# important as we loop testing the best boxes against the other boxes)
121+
threshTable.sort_values("Score", ascending=sortAscending, inplace=True) # Warning here is fine
124122

125123

126124
# Split the inital pool into Final Hit that are kept and restTable that can be tested
@@ -196,6 +194,6 @@ def NMS(tableHit, scoreThreshold=None, sortDescending=True, N_object=float("inf"
196194
{'TemplateName':1,'BBox':(1074, 530, 680, 390), 'Score':0.4}
197195
]
198196

199-
FinalHits = NMS( pd.DataFrame(ListHit), scoreThreshold=0.7, sortDescending=True, maxOverlap=0.5 )
197+
FinalHits = NMS( pd.DataFrame(ListHit), scoreThreshold=0.7, sortAscending=False, maxOverlap=0.5 )
200198

201199
print(FinalHits)

MTM/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ def matchTemplates(listTemplates, image, method=cv2.TM_CCOEFF_NORMED, N_object=f
166166

167167
tableHit = findMatches(listTemplates, image, method, N_object, score_threshold, searchBox)
168168

169-
if method == 1: bestHits = NMS(tableHit, N_object=N_object, maxOverlap=maxOverlap, sortDescending=False)
169+
if method == 1: bestHits = NMS(tableHit, N_object=N_object, maxOverlap=maxOverlap, sortAscending=True)
170170

171-
elif method in (3,5): bestHits = NMS(tableHit, N_object=N_object, maxOverlap=maxOverlap, sortDescending=True)
171+
elif method in (3,5): bestHits = NMS(tableHit, N_object=N_object, maxOverlap=maxOverlap, sortAscending=False)
172172

173173
return bestHits
174174

@@ -251,8 +251,8 @@ def drawBoxesOnGray(image, tableHit, boxThickness=2, boxColor=255, showLabel=Fal
251251
image = coins()
252252

253253
## Perform matching
254-
#tableHit = matchTemplates([('small', smallCoin), ('big', bigCoin)], image, score_threshold=0.4, method=cv2.TM_CCOEFF_NORMED, maxOverlap=0)
255-
tableHit = matchTemplates([('small', smallCoin), ('big', bigCoin)], image, N_object=1, score_threshold=0.4, method=cv2.TM_CCOEFF_NORMED, maxOverlap=0)
254+
#tableHit = matchTemplates([('small', smallCoin), ('big', bigCoin)], image, score_threshold=0.6, method=cv2.TM_CCOEFF_NORMED, maxOverlap=0) # Correlation-score
255+
tableHit = matchTemplates([('small', smallCoin), ('big', bigCoin)], image, score_threshold=0.4, method=cv2.TM_SQDIFF_NORMED, maxOverlap=0) # Difference-score
256256

257257
print("Found {} coins".format(len(tableHit)))
258258

0 commit comments

Comments
 (0)