Skip to content

Commit d5cb167

Browse files
committed
use zip(*listHit) to unzip listHit
1 parent 72170f3 commit d5cb167

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

Diff for: MTM/NMS.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,9 @@ def NMS(listHit:Sequence[Hit], scoreThreshold:float=0.5, sortAscending:bool=Fals
5555
return listHit[:] # same just making a copy to avoid side effects
5656

5757
# Get separate lists for the bounding boxe coordinates and their score
58-
listBoxes = [None] * nHits # list of (x,y,width,height)
59-
listScores = [None] * nHits # list of associated scores
60-
61-
for i, hit in enumerate(listHit): # single iteration through the list instead of using 2 list comprehensions
62-
listBoxes[i] = hit[1]
63-
listScores[i] = hit[2]
64-
58+
# use zip in the other direction with the * to "unzip"
59+
listLabel, listBoxes, listScores = zip(*listHit)
60+
6561
if N_object == 1:
6662

6763
# Get hit with highest or lower score

Diff for: README.md

+17-5
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,27 @@ The [wiki](https://github.com/multi-template-matching/MultiTemplateMatching-Pyth
3131
The [website](https://multi-template-matching.github.io/Multi-Template-Matching/) of the project contains some more general documentation.
3232

3333
# Tips and tricks
34+
35+
- `matchTemplates` expects for the template a list of tuples with one tuple per template in the form (a string identifier for the template, array for the template image). You can generate such list of tuples, from separate lists of the label and template images as following
36+
37+
```python
38+
listLabel = [] # this one should have the string identifier for each template
39+
listTemplate = [] # this one should have the image array for each template, both list should have the same length
40+
listTemplateTuple = zip(listLabel, listTemplate)
41+
```
42+
43+
Similarly, from the list of hits returned by matchTemplates (or NMS), you can get individual lists for the label, bounding-boxes and scores, using `listLabel, listBbox, listScore = zip(*listHit)`
44+
3445
- To have a nicer formatting when printing the list of detected hits, you can wrap it into a numpy array, and print that array as following
3546
`print(np.array(listHit, dtype=object))`, the `dtype=object` argument is required as each hit in the list is made of different data type (string, tuple and float)
3647

37-
- Before version 2.0.0, most functions were returning or accepting pandas DataFrame for the list of hit.
38-
You can still get such DataFrame from the list of hit returned by the newer version of the package, using these commands
48+
- Before version 2.0.0, most functions were returning or accepting pandas DataFrame for the list of hit.
49+
You can still get such DataFrame from the list of hits returned by MTM v2.0.0 and later, using the command below
3950

4051
```python
4152
import pandas as pd
4253

43-
listLabel = [hit[0] for hit in listHit]
44-
listBbox = [hit[1] for hit in listHit]
45-
listScore = [hit[2] for hit in listHit]
54+
listLabel, listBbox, listScore = zip(*listHit)
4655

4756
df = pd.DataFrame({"Label":listLabel,
4857
"bounding box":listBbox,
@@ -51,6 +60,9 @@ df = pd.DataFrame({"Label":listLabel,
5160
print(df)
5261
```
5362

63+
You can also stick to previous version of MTM by specifying the version to pip install, or in a requirements.txt or environment.yml
64+
`pip install "Multi-Template-Matching < 2.0.0"
65+
5466
# Examples
5567
Check out the [jupyter notebook tutorial](https://github.com/multi-template-matching/MultiTemplateMatching-Python/tree/master/tutorials) for some example of how to use the package.
5668
You can run the tutorials online using Binder, no configuration needed ! (click the Binder banner on top of this page).

Diff for: test.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@
4646
#%% Use GluonCV for display
4747
import gluoncv as gcv
4848

49+
# "Unzip" the list of tuple, into individual lists
50+
listLabel, listBbox, listScore = zip(*listHit)
51+
4952
# Convert from x,y,w,h to xmin, ymin, xmax, ymax
50-
BBoxes_xywh = np.array( [hit[1] for hit in listHit] )
53+
BBoxes_xywh = np.array(listBbox)
5154
BBoxes_xyxy = gcv.utils.bbox.bbox_xywh_to_xyxy(BBoxes_xywh)
5255

53-
Overlay2 = gcv.utils.viz.cv_plot_bbox(cv2.cvtColor(image, cv2.COLOR_GRAY2RGB), BBoxes_xyxy.astype("float64"), scores=[hit[2] for hit in listHit], thresh=0 )
56+
Overlay2 = gcv.utils.viz.cv_plot_bbox(cv2.cvtColor(image, cv2.COLOR_GRAY2RGB), BBoxes_xyxy.astype("float64"), scores=listScore, thresh=0 )
5457
plt.figure()
5558
plt.imshow(Overlay2)

0 commit comments

Comments
 (0)