-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.py
104 lines (68 loc) · 3.42 KB
/
plot1.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
# -*- coding: utf-8 -*-
"""plot1.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1CAt5OmcmhN2jNkvEnJHtPQJJU-kxYo5E
"""
#!/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.
'''
# imports
import cv2
import numpy as np
# Function to draw Bird Eye View for region of interest(ROI). Red, Green points represents risk to human.
# Red: High 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)
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 = []
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):
r.append(distances_mat[i][0])
if (distances_mat[i][1] not in r) and (distances_mat[i][1] not in g):
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] == 2:
if (distances_mat[i][0] not in r) and (distances_mat[i][0] not in g) :
g.append(distances_mat[i][0])
if (distances_mat[i][1] not in r) and (distances_mat[i][1] not in g) :
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 r:
blank_image = cv2.circle(blank_image, (int(i[0] * scale_w), int(i[1] * scale_h)), 5, red, 10)
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
# Green: No Risk
def social_distancing_view(frame, distances_mat, boxes, risk_count):
red = (0, 0, 255)
green = (0, 255, 0)
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 == 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)
cv2.putText(frame, "Social Distancing Violations" + str(risk_count[0]) , (10, frame.shape[0] - 25),cv2.FONT_HERSHEY_SIMPLEX, 0.85, (0, 0, 255), 1)
return frame