-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug_tools.py
81 lines (71 loc) · 2.99 KB
/
debug_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import cv2
import itertools
import numpy as np
def dump_images(
names, pil_images, annotations, detections, stats,
labelmap, dir):
"""
Dumps images with bbox overlays to disk.
:param names: batch of sample names
:param pil_images: batch of original PIL images
:param annotations: batch of annotations
:param detections: batch of detections from NN
:param stats: batch of debug info from a network. Keeps number of anchors that match particular GT box.
:param labelmap: names of classes
:param dir: destination directory to save images
:return: None
"""
det_color = (0, 255, 0)
anno_color = (255, 0, 0)
if annotations is None: annotations = []
if detections is None: detections = []
if stats is None: stats = []
try:
for ib, (name, pil_img, anno, detection, stat) in \
enumerate(itertools.zip_longest(names, pil_images, annotations, detections, stats)):
img = np.asarray(pil_img).copy()
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
scale = [img.shape[1], img.shape[0], img.shape[1], img.shape[0]]
if detection is not None:
for icls, cls_det in enumerate(detection):
for det in cls_det:
conf = det[0]
if conf > 0.0:
bbox = det[1:]
bbox_pix = bbox * scale
type = labelmap[icls]
cv2.rectangle(
img,
(int(bbox_pix[0]), int(bbox_pix[1])),
(int(bbox_pix[2]), int(bbox_pix[3])),
det_color, 1)
cv2.putText(
img,
'{} {:.2f}'.format(type, conf),
(int(bbox_pix[0]), int(bbox_pix[1])+10),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
det_color)
if anno is not None and stat is not None:
for obj, num_matches in zip(anno, stat):
bbox = obj['bbox']
bbox_pix = bbox * scale
cv2.rectangle(
img,
(int(bbox_pix[0]), int(bbox_pix[1])),
(int(bbox_pix[2]), int(bbox_pix[3])),
anno_color, 1)
cv2.putText(
img,
obj['type'] + " M{}".format(num_matches), # M - number of matching anchors
(int(bbox_pix[0]), int(bbox_pix[1])+10),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
anno_color)
filename = name + '.png'
cv2.imwrite(os.path.join(dir, filename), img)
pass
except Exception as e:
pass
pass