-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
135 lines (102 loc) · 3.81 KB
/
helpers.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
import numpy as np
import cv2
class Box:
def __init__(self):
self.x, self.y = float(), float()
self.w, self.h = float(), float()
self.c = float()
self.prob = float()
def overlap(x1,w1,x2,w2):
l1 = x1 - w1 / 2.;
l2 = x2 - w2 / 2.;
left = max(l1, l2)
r1 = x1 + w1 / 2.;
r2 = x2 + w2 / 2.;
right = min(r1, r2)
return right - left;
def box_intersection(a, b):
w = overlap(a.x, a.w, b.x, b.w);
h = overlap(a.y, a.h, b.y, b.h);
if w < 0 or h < 0: return 0;
area = w * h;
return area;
def box_union(a, b):
i = box_intersection(a, b);
u = a.w * a.h + b.w * b.h - i;
return u;
def box_iou(a, b):
return box_intersection(a, b) / box_union(a, b);
def box_iou2(a, b):
'''
Helper funciton to calculate the ratio between intersection and the union of
two boxes a and b
a[0], a[1], a[2], a[3] <-> left, up, right, bottom
'''
w_intsec = np.maximum (0, (np.minimum(a[2], b[2]) - np.maximum(a[0], b[0])))
h_intsec = np.maximum (0, (np.minimum(a[3], b[3]) - np.maximum(a[1], b[1])))
s_intsec = w_intsec * h_intsec
s_a = (a[2] - a[0])*(a[3] - a[1])
s_b = (b[2] - b[0])*(b[3] - b[1])
return float(s_intsec)/(s_a + s_b -s_intsec)
def convert_to_pixel(box_yolo, img, crop_range):
'''
Helper function to convert (scaled) coordinates of a bounding box
to pixel coordinates.
Example (0.89361443264143803, 0.4880486045564924, 0.23544462956491041,
0.36866588651069609)
crop_range: specifies the part of image to be cropped
'''
box = box_yolo
imgcv = img
[xmin, xmax] = crop_range[0]
[ymin, ymax] = crop_range[1]
h, w, _ = imgcv.shape
# Calculate left, top, width, and height of the bounding box
left = int((box.x - box.w/2.)*(xmax - xmin) + xmin)
top = int((box.y - box.h/2.)*(ymax - ymin) + ymin)
width = int(box.w*(xmax - xmin))
height = int(box.h*(ymax - ymin))
# Deal with corner cases
if left < 0 : left = 0
if top < 0 : top = 0
# Return the coordinates (in the unit of the pixels)
box_pixel = np.array([left, top, width, height])
return box_pixel
def convert_to_cv2bbox(bbox, img_dim = (1280, 720)):
'''
Helper fucntion for converting bbox to bbox_cv2
bbox = [left, top, width, height]
bbox_cv2 = [left, top, right, bottom]
img_dim: dimension of the image, img_dim[0]<-> x
img_dim[1]<-> y
'''
left = np.maximum(0, bbox[0])
top = np.maximum(0, bbox[1])
right = np.minimum(img_dim[0], bbox[0] + bbox[2])
bottom = np.minimum(img_dim[1], bbox[1] + bbox[3])
return (left, top, right, bottom)
def draw_box_label(img, bbox_cv2, num_p, box_color=(0, 255, 255), show_label=True):
'''
Helper funciton for drawing the bounding boxes and the labels
bbox_cv2 = [left, top, right, bottom]
'''
#box_color= (0, 255, 255)
font = cv2.FONT_HERSHEY_SIMPLEX
font_size = 0.7
font_color = (0, 0, 0)
left, top, right, bottom = bbox_cv2[1], bbox_cv2[0], bbox_cv2[3], bbox_cv2[2]
# Draw the bounding box
cv2.rectangle(img, (left, top), (right, bottom), box_color, 4)
if show_label:
# Draw a filled box on top of the bounding box (as the background for the labels)
cv2.rectangle(img, (left-2, top-45), (right+2, top), box_color, -1, 1)
# Output the labels that show the x and y coordinates of the bounding box center.
text_x= 'x='+str((left+right)/2)
cv2.putText(img,text_x,(left,top-25), font, font_size, font_color, 1, cv2.LINE_AA)
text_y='y='+str((top+bottom)/2)
cv2.putText(img,text_y,(left,top-5), font, font_size, font_color, 1, cv2.LINE_AA)
#output number of people detected
text_num = 'Num_people = '+str(num_p)
cv2.putText(img,text_num,(10,20), font, font_size, font_color, 1, cv2.LINE_AA)
return img