-
Notifications
You must be signed in to change notification settings - Fork 0
/
euclid.py
168 lines (129 loc) · 4.29 KB
/
euclid.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
import numpy as np
import scipy
from scipy.spatial import distance
import subprocess
import cv2
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
class Dist:
def __init__(self):
self.count_data = []
#self.cloud = Cloud.Cloud()
self.date = str(datetime.now())
def persons_find(self,outputs):
'''
Parameters
----------
outputs : detectron2 outputs object
Output instance of the detectron2 predictor.
Returns
-------
num : int
Count of people inside the frane.
person : list
List of persons and coordinates.
'''
classes=outputs['instances'].pred_classes.cpu().numpy()
ind = np.where(classes==0)[0]
bbox=outputs['instances'].pred_boxes.tensor.cpu().numpy()
person = bbox[ind]
num = len(person)
return num, person
def mid_point(self,img,person,idx):
'''
Parameters
----------
img : uint8
Frames in the video.
person : list
List of persons and coordinates.
idx : int,
id of the person object.
Returns
-------
mid : tuple of int
midpoint coordinates for each person in frame.
'''
x1,y1,x2,y2 = person[idx]
#compute bottom center of bbox
x_mid = int((x1+x2)/2)
y_mid = int(y2)
mid = (x_mid,y_mid)
_ = cv2.circle(img, mid, 5, (255, 0, 0), -1)
cv2.putText(img, str(idx), mid, cv2.FONT_HERSHEY_SIMPLEX,1, (255, 255, 255), 2, cv2.LINE_AA)
return mid
def compute_distance(self,outputs, img):
'''
Parameters
----------
outputs : detectron2 outputs object
Output instance of the detectron2 predictor.
img : uint8
Frames from the video.
Returns
-------
dist : list
Distance between each person within the threshold.
'''
self.num, self.person = self.persons_find( outputs)
midpoints = [self.mid_point(img,self.person,i) for i in range(len(self.person))]
dist = np.zeros((self.num,self.num))
for i in range(self.num):
for j in range(i+1,self.num):
if i!=j:
dst = distance.euclidean(midpoints[i], midpoints[j])
dist[i][j]=dst
return dist
def find_closest(self, outputs, img,thresh=150):
'''
Parameters
----------
outputs : detectron2 outputs object
Output instance of the detectron2 predictor.
img : uint8
Frames from the video.
thresh : int,
value for minimum threshold distance between two people. The default is 150.
Returns
-------
p1 : int
Primary Person id.
p2 : int
Secondary Person id.
d : float
Distance between persons in arguments.
'''
p1, p2, d = [],[],[]
dist = self.compute_distance(outputs, img)
for i in range(self.num):
for j in range(i,self.num):
if( (i!=j) & (dist[i][j]<=thresh)):
p1.append(i)
p2.append(j)
d.append(dist[i][j])
return p1,p2,d
def change_2_red(self,img, p1,p2, count, directory):
'''
Parameters
----------
img : uint8
Frames from the video.
p1 : int
Primary Person id.
p2 : int
Secondary Person id.
sum : int
Count of frames saved.
Returns
-------
None.
'''
risky = np.unique(p1+p2)
for i in risky:
x1,y1,x2,y2 = self.person[i]
_ = cv2.rectangle(img, (x1, y1), (x2, y2), (0,0,255), 2)
cv2.imwrite('/var/www/html/output_social_dist/'+directory+'/frame'+str(count)+'.png', img)
dict1 = {'Frame_name':'/'+directory+'/frame'+str(count)+'.png','count':len(p1)}
self.count_data.append(dict1)