3
3
import cv2
4
4
from .colors import get_color
5
5
6
+
6
7
class BoundBox :
7
- def __init__ (self , xmin , ymin , xmax , ymax , c = None , classes = None ):
8
+ def __init__ (self , xmin , ymin , xmax , ymax , c = None , classes = None ):
8
9
self .xmin = xmin
9
10
self .ymin = ymin
10
11
self .xmax = xmax
11
12
self .ymax = ymax
12
13
13
- self .c = c
14
+ self .c = c
14
15
self .classes = classes
15
16
16
17
self .label = - 1
@@ -28,6 +29,7 @@ def get_score(self):
28
29
29
30
return self .score
30
31
32
+
31
33
def _interval_overlap (interval_a , interval_b ):
32
34
x1 , x2 = interval_a
33
35
x3 , x4 = interval_b
@@ -36,59 +38,81 @@ def _interval_overlap(interval_a, interval_b):
36
38
if x4 < x1 :
37
39
return 0
38
40
else :
39
- return min (x2 ,x4 ) - x1
41
+ return min (x2 , x4 ) - x1
40
42
else :
41
43
if x2 < x3 :
42
- return 0
44
+ return 0
43
45
else :
44
- return min (x2 ,x4 ) - x3
46
+ return min (x2 , x4 ) - x3
47
+
45
48
46
49
def bbox_iou (box1 , box2 ):
47
50
intersect_w = _interval_overlap ([box1 .xmin , box1 .xmax ], [box2 .xmin , box2 .xmax ])
48
51
intersect_h = _interval_overlap ([box1 .ymin , box1 .ymax ], [box2 .ymin , box2 .ymax ])
49
52
50
53
intersect = intersect_w * intersect_h
51
54
52
- w1 , h1 = box1 .xmax - box1 .xmin , box1 .ymax - box1 .ymin
53
- w2 , h2 = box2 .xmax - box2 .xmin , box2 .ymax - box2 .ymin
55
+ w1 , h1 = box1 .xmax - box1 .xmin , box1 .ymax - box1 .ymin
56
+ w2 , h2 = box2 .xmax - box2 .xmin , box2 .ymax - box2 .ymin
54
57
55
- union = w1 * h1 + w2 * h2 - intersect
58
+ union = w1 * h1 + w2 * h2 - intersect
56
59
57
- if union == 0 : return 0
60
+ if union == 0 :
61
+ return 0
58
62
59
63
return float (intersect ) / union
60
64
61
- def draw_boxes (image , boxes , labels , obj_thresh , quiet = True , number_color = False ):
65
+
66
+ def draw_boxes (image , boxes , labels , obj_thresh , quiet = True , number_color = False ):
62
67
for box in boxes :
63
- label_str = ''
68
+ label_str = ""
64
69
label = - 1
65
70
66
71
for i in range (len (labels )):
67
72
if box .classes [i ] > obj_thresh :
68
- if label_str != '' : label_str += ', '
69
- label_str += (labels [i ] + ' ' + str (round (box .get_score ()* 100 ,0 )) + '%' )
73
+ if label_str != "" :
74
+ label_str += ", "
75
+ label_str += (
76
+ labels [i ] + " " + str (round (box .get_score () * 100 , 0 )) + "%"
77
+ )
70
78
label = i
71
- if not quiet : print (label_str )
79
+ if not quiet :
80
+ print (label_str )
72
81
73
82
if label >= 0 :
74
- text_size = cv2 .getTextSize (label_str , cv2 .FONT_HERSHEY_SIMPLEX , 1.1e-4 * image .shape [0 ], 2 )
83
+ text_size = cv2 .getTextSize (
84
+ label_str , cv2 .FONT_HERSHEY_SIMPLEX , 1.1e-4 * image .shape [0 ], 2
85
+ )
75
86
width , height = text_size [0 ][0 ], text_size [0 ][1 ]
76
- region = np .array ([[box .xmin - 3 , box .ymin ],
77
- [box .xmin - 3 , box .ymin - height - 16 ],
78
- [box .xmin + width + 6 , box .ymin - height - 16 ],
79
- [box .xmin + width + 6 , box .ymin ]], dtype = 'int32' )
87
+ region = np .array (
88
+ [
89
+ [box .xmin - 3 , box .ymin ],
90
+ [box .xmin - 3 , box .ymin - height - 16 ],
91
+ [box .xmin + width + 6 , box .ymin - height - 16 ],
92
+ [box .xmin + width + 6 , box .ymin ],
93
+ ],
94
+ dtype = "int32" ,
95
+ )
80
96
# Use one only color
81
97
if number_color :
82
98
label = number_color
83
99
84
- cv2 .rectangle (img = image , pt1 = (box .xmin ,box .ymin ), pt2 = (box .xmax ,box .ymax ), color = get_color (label ), thickness = 1 )
100
+ cv2 .rectangle (
101
+ img = image ,
102
+ pt1 = (box .xmin , box .ymin ),
103
+ pt2 = (box .xmax , box .ymax ),
104
+ color = get_color (label ),
105
+ thickness = 1 ,
106
+ )
85
107
cv2 .fillPoly (img = image , pts = [region ], color = get_color (label ))
86
- cv2 .putText (img = image ,
87
- text = label_str ,
88
- org = (box .xmin + 6 , box .ymin - 6 ),
89
- fontFace = cv2 .FONT_HERSHEY_SIMPLEX ,
90
- fontScale = 0.7e-3 * image .shape [0 ],
91
- color = (0 ,0 ,0 ),
92
- thickness = 2 )
108
+ cv2 .putText (
109
+ img = image ,
110
+ text = label_str ,
111
+ org = (box .xmin + 6 , box .ymin - 6 ),
112
+ fontFace = cv2 .FONT_HERSHEY_SIMPLEX ,
113
+ fontScale = 0.7e-3 * image .shape [0 ],
114
+ color = (0 , 0 , 0 ),
115
+ thickness = 2 ,
116
+ )
93
117
94
118
return image
0 commit comments