Skip to content

Commit e57eb58

Browse files
committed
use pprint instead of turning to no.array
1 parent 0ebe9fc commit e57eb58

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ listTemplateTuple = zip(listLabel, listTemplate)
4242

4343
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)`
4444

45-
- 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
46-
`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)
45+
- To have a nicer formatting (one list item per row) when printing the list of detected hits, you can use the `pprint` function from the pprint module (for pretty print).
46+
It's usually not needed in notebooks (see the example notebooks).
47+
```
48+
from pprint import pprint
49+
pprint(listHit)
50+
```
4751

4852
- Before version 2.0.0, most functions were returning or accepting pandas DataFrame for the list of hit.
4953
You can still get such DataFrame from the list of hits returned by MTM v2.0.0 and later, using the command below

test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
listHit = MTM.matchTemplates(listTemplates, image, score_threshold=0.3, method=cv2.TM_CCOEFF_NORMED, maxOverlap=0) # Correlation-score
2525
#tableHit = MTM.matchTemplates(listTemplates, image, score_threshold=0.4, method=cv2.TM_SQDIFF_NORMED, maxOverlap=0) # Difference-score
2626

27+
from pprint import pprint # pretty print to have one list item per line
2728
print("Found {} coins".format(len(listHit)))
28-
print(np.array(listHit, dtype=object))
29+
pprint(listHit)
2930

3031

3132
#%% Display matches

tutorials/InteractiveParameters.ipynb

+11-9
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
{
168168
"data": {
169169
"text/html": [
170-
"<div id='6e2f2a1d-c3f1-4e6b-8482-5cf3c14badba'></div>"
170+
"<div id='e6f8fc7c-8471-4182-9aaf-5bec812cb347'></div>"
171171
],
172172
"text/plain": [
173173
"<IPython.core.display.HTML object>"
@@ -179,7 +179,7 @@
179179
{
180180
"data": {
181181
"text/plain": [
182-
"<matplotlib.image.AxesImage at 0x245ab382130>"
182+
"<matplotlib.image.AxesImage at 0x1ac74925eb0>"
183183
]
184184
},
185185
"execution_count": 4,
@@ -219,7 +219,7 @@
219219
{
220220
"data": {
221221
"text/html": [
222-
"<div id='770f0897-5537-420b-b39d-d604b416e553'></div>"
222+
"<div id='701c8036-af76-4a6f-b518-d25c2527b9de'></div>"
223223
],
224224
"text/plain": [
225225
"<IPython.core.display.HTML object>"
@@ -231,7 +231,7 @@
231231
{
232232
"data": {
233233
"text/plain": [
234-
"<matplotlib.image.AxesImage at 0x245df9c84f0>"
234+
"<matplotlib.image.AxesImage at 0x1ac2943a9d0>"
235235
]
236236
},
237237
"execution_count": 5,
@@ -270,7 +270,7 @@
270270
{
271271
"data": {
272272
"text/html": [
273-
"<div id='710e35b3-9a5a-47f2-be3d-5e05f93cb92f'></div>"
273+
"<div id='14b10e59-34aa-4ef7-b00d-c4b11b6a7efd'></div>"
274274
],
275275
"text/plain": [
276276
"<IPython.core.display.HTML object>"
@@ -332,7 +332,7 @@
332332
},
333333
{
334334
"cell_type": "code",
335-
"execution_count": 9,
335+
"execution_count": 8,
336336
"metadata": {},
337337
"outputs": [
338338
{
@@ -348,7 +348,7 @@
348348
{
349349
"data": {
350350
"text/html": [
351-
"<div id='6e529456-1c75-4e15-8386-ade0ca6d8894'></div>"
351+
"<div id='42e21ad2-0780-47f1-9b8c-9a1d0e017579'></div>"
352352
],
353353
"text/plain": [
354354
"<IPython.core.display.HTML object>"
@@ -360,7 +360,7 @@
360360
{
361361
"data": {
362362
"application/vnd.jupyter.widget-view+json": {
363-
"model_id": "4ee5e0e23f1749b9b81112b8a76b5fef",
363+
"model_id": "9b9ebb96006247d888a67339a4c66a5a",
364364
"version_major": 2,
365365
"version_minor": 0
366366
},
@@ -373,6 +373,8 @@
373373
}
374374
],
375375
"source": [
376+
"from pprint import pprint # for pretty printing the list of hits\n",
377+
"\n",
376378
"plt.figure(\"Multi-Template Matching\", figsize = (5,5))\n",
377379
"\n",
378380
"@interact( N_object=(0,10,1), score_threshold=(0, 1, 0.05), max_overlap=(0, 1, 0.1) )\n",
@@ -389,7 +391,7 @@
389391
" plt.cla()\n",
390392
" plt.imshow(Overlay)\n",
391393
" \n",
392-
" print(np.array(listHits, dtype=object))"
394+
" pprint(listHits)"
393395
]
394396
},
395397
{

tutorials/NoiseBenchmark.ipynb

+18-19
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
{
101101
"data": {
102102
"text/plain": [
103-
"<matplotlib.image.AxesImage at 0x2cf6f888370>"
103+
"<matplotlib.image.AxesImage at 0x23cdebb51f0>"
104104
]
105105
},
106106
"execution_count": 3,
@@ -223,7 +223,7 @@
223223
{
224224
"data": {
225225
"text/plain": [
226-
"<matplotlib.image.AxesImage at 0x2cf70e38490>"
226+
"<matplotlib.image.AxesImage at 0x23cdf8571c0>"
227227
]
228228
},
229229
"execution_count": 6,
@@ -270,7 +270,7 @@
270270
},
271271
{
272272
"cell_type": "code",
273-
"execution_count": 10,
273+
"execution_count": 7,
274274
"metadata": {
275275
"scrolled": true
276276
},
@@ -280,31 +280,30 @@
280280
"output_type": "stream",
281281
"text": [
282282
"Initial detections\n",
283-
"[['0' (946, 784, 414, 400) 1.0]\n",
284-
" ['180' (1525, 968, 414, 400) 0.5919372]\n",
285-
" ['180' (1173, 1354, 414, 400) 0.55106765]\n",
286-
" ['90' (1459, 474, 400, 414) 0.5381054]]\n",
283+
"[('0', (946, 784, 414, 400), 1.0),\n",
284+
" ('180', (1525, 968, 414, 400), 0.5919372),\n",
285+
" ('180', (1173, 1354, 414, 400), 0.55106765),\n",
286+
" ('90', (1459, 474, 400, 414), 0.5381054)]\n",
287287
"\n",
288288
"Detections with noise\n",
289-
"[['0' (946, 784, 414, 400) 0.766042]\n",
290-
" ['180' (1524, 968, 414, 400) 0.46325678]\n",
291-
" ['180' (1172, 1354, 414, 400) 0.43588048]\n",
292-
" ['90' (1459, 474, 400, 414) 0.43311062]]\n"
289+
"[('0', (946, 784, 414, 400), 0.766042),\n",
290+
" ('180', (1524, 968, 414, 400), 0.46325678),\n",
291+
" ('180', (1172, 1354, 414, 400), 0.43588048),\n",
292+
" ('90', (1459, 474, 400, 414), 0.43311062)]\n"
293293
]
294294
}
295295
],
296296
"source": [
297+
"from pprint import pprint\n",
298+
"\n",
297299
"# Call again matchTemplates with the noisy image this time, but the same template\n",
298300
"listHits_Noise = matchTemplates(listTemplate, imageNoise, N_object=4, score_threshold=0.3, method=cv2.TM_CCOEFF_NORMED, maxOverlap=0.3)\n",
299301
"\n",
300-
"def pretty_print(listHits):\n",
301-
" print(np.array(listHits, dtype=object))\n",
302-
"\n",
303302
"print(\"Initial detections\")\n",
304-
"pretty_print(listHits)\n",
303+
"pprint(listHits)\n",
305304
"\n",
306305
"print(\"\\nDetections with noise\")\n",
307-
"pretty_print(listHits_Noise)"
306+
"pprint(listHits_Noise)"
308307
]
309308
},
310309
{
@@ -316,18 +315,18 @@
316315
},
317316
{
318317
"cell_type": "code",
319-
"execution_count": 11,
318+
"execution_count": 8,
320319
"metadata": {
321320
"scrolled": false
322321
},
323322
"outputs": [
324323
{
325324
"data": {
326325
"text/plain": [
327-
"<matplotlib.image.AxesImage at 0x2cf707b5550>"
326+
"<matplotlib.image.AxesImage at 0x23cdf8e11c0>"
328327
]
329328
},
330-
"execution_count": 11,
329+
"execution_count": 8,
331330
"metadata": {},
332331
"output_type": "execute_result"
333332
},

0 commit comments

Comments
 (0)