-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathplot.py
137 lines (104 loc) · 5.91 KB
/
plot.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
136
#!/usr/bin/env python
'''
Contain functions to draw Bird Eye View for region of interest(ROI) and draw bounding boxes according to risk factor
for humans in a frame and draw lines between boxes according to risk factor between two humans.
'''
__title__ = "plot.py"
__Version__ = "1.0"
__copyright__ = "Copyright 2020 , Social Distancing AI"
__license__ = "MIT"
__author__ = "Deepak Birla"
__email__ = "[email protected]"
__date__ = "2020/05/29"
__python_version__ = "3.5.2"
# imports
import cv2
import numpy as np
# Function to draw Bird Eye View for region of interest(ROI). Red, Yellow, Green points represents risk to human.
# Red: High Risk
# Yellow: Low Risk
# Green: No Risk
def bird_eye_view(frame, distances_mat, bottom_points, scale_w, scale_h, risk_count):
h = frame.shape[0]
w = frame.shape[1]
red = (0, 0, 255)
green = (0, 255, 0)
yellow = (0, 255, 255)
white = (200, 200, 200)
blank_image = np.zeros((int(h * scale_h), int(w * scale_w), 3), np.uint8)
blank_image[:] = white
warped_pts = []
r = []
g = []
y = []
for i in range(len(distances_mat)):
if distances_mat[i][2] == 0:
if (distances_mat[i][0] not in r) and (distances_mat[i][0] not in g) and (distances_mat[i][0] not in y):
r.append(distances_mat[i][0])
if (distances_mat[i][1] not in r) and (distances_mat[i][1] not in g) and (distances_mat[i][1] not in y):
r.append(distances_mat[i][1])
blank_image = cv2.line(blank_image, (int(distances_mat[i][0][0] * scale_w), int(distances_mat[i][0][1] * scale_h)), (int(distances_mat[i][1][0] * scale_w), int(distances_mat[i][1][1]* scale_h)), red, 2)
for i in range(len(distances_mat)):
if distances_mat[i][2] == 1:
if (distances_mat[i][0] not in r) and (distances_mat[i][0] not in g) and (distances_mat[i][0] not in y):
y.append(distances_mat[i][0])
if (distances_mat[i][1] not in r) and (distances_mat[i][1] not in g) and (distances_mat[i][1] not in y):
y.append(distances_mat[i][1])
blank_image = cv2.line(blank_image, (int(distances_mat[i][0][0] * scale_w), int(distances_mat[i][0][1] * scale_h)), (int(distances_mat[i][1][0] * scale_w), int(distances_mat[i][1][1]* scale_h)), yellow, 2)
for i in range(len(distances_mat)):
if distances_mat[i][2] == 2:
if (distances_mat[i][0] not in r) and (distances_mat[i][0] not in g) and (distances_mat[i][0] not in y):
g.append(distances_mat[i][0])
if (distances_mat[i][1] not in r) and (distances_mat[i][1] not in g) and (distances_mat[i][1] not in y):
g.append(distances_mat[i][1])
for i in bottom_points:
blank_image = cv2.circle(blank_image, (int(i[0] * scale_w), int(i[1] * scale_h)), 5, green, 10)
for i in y:
blank_image = cv2.circle(blank_image, (int(i[0] * scale_w), int(i[1] * scale_h)), 5, yellow, 10)
for i in r:
blank_image = cv2.circle(blank_image, (int(i[0] * scale_w), int(i[1] * scale_h)), 5, red, 10)
#pad = np.full((100,blank_image.shape[1],3), [110, 110, 100], dtype=np.uint8)
#cv2.putText(pad, "-- HIGH RISK : " + str(risk_count[0]) + " people", (50, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
#cv2.putText(pad, "-- LOW RISK : " + str(risk_count[1]) + " people", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 1)
#cv2.putText(pad, "-- SAFE : " + str(risk_count[2]) + " people", (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
#blank_image = np.vstack((blank_image,pad))
return blank_image
# Function to draw bounding boxes according to risk factor for humans in a frame and draw lines between
# boxes according to risk factor between two humans.
# Red: High Risk
# Yellow: Low Risk
# Green: No Risk
def social_distancing_view(frame, distances_mat, boxes, risk_count):
red = (0, 0, 255)
green = (0, 255, 0)
yellow = (0, 255, 255)
for i in range(len(boxes)):
x,y,w,h = boxes[i][:]
frame = cv2.rectangle(frame,(x,y),(x+w,y+h),green,2)
for i in range(len(distances_mat)):
per1 = distances_mat[i][0]
per2 = distances_mat[i][1]
closeness = distances_mat[i][2]
if closeness == 1:
x,y,w,h = per1[:]
frame = cv2.rectangle(frame,(x,y),(x+w,y+h),yellow,2)
x1,y1,w1,h1 = per2[:]
frame = cv2.rectangle(frame,(x1,y1),(x1+w1,y1+h1),yellow,2)
frame = cv2.line(frame, (int(x+w/2), int(y+h/2)), (int(x1+w1/2), int(y1+h1/2)),yellow, 2)
for i in range(len(distances_mat)):
per1 = distances_mat[i][0]
per2 = distances_mat[i][1]
closeness = distances_mat[i][2]
if closeness == 0:
x,y,w,h = per1[:]
frame = cv2.rectangle(frame,(x,y),(x+w,y+h),red,2)
x1,y1,w1,h1 = per2[:]
frame = cv2.rectangle(frame,(x1,y1),(x1+w1,y1+h1),red,2)
frame = cv2.line(frame, (int(x+w/2), int(y+h/2)), (int(x1+w1/2), int(y1+h1/2)),red, 2)
pad = np.full((140,frame.shape[1],3), [110, 110, 100], dtype=np.uint8)
cv2.putText(pad, "Bounding box shows the level of risk to the person.", (50, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (100, 100, 0), 2)
cv2.putText(pad, "-- HIGH RISK : " + str(risk_count[0]) + " people", (50, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 1)
cv2.putText(pad, "-- LOW RISK : " + str(risk_count[1]) + " people", (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 1)
cv2.putText(pad, "-- SAFE : " + str(risk_count[2]) + " people", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 1)
frame = np.vstack((frame,pad))
return frame