-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_logic.py
223 lines (168 loc) · 7.06 KB
/
code_logic.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
########################################################################################
# !/usr/bin/env python #
# coding: utf-8 #
# my logiv ------------ #
# compare neighbour and select the smallest length one #
# then compare the distance between them if it eced 2/3 of smallest heith tgen it is a #
########################################################################################
import cv2
import numpy as np
angle_factor = 0.8
sw = 0.3
sh = 1
frame_num = 0
solid_back_color = (50, 50, 50)
backgroud = cv2.imread('./img.png')
wght = "./YoloFiles/yolov3.weights"
cnfg = "./YoloFiles/yolov3.cfg"
LblPath = "./YoloFiles/coco.names"
background = cv2.imread('./icons/AITFSC.png')
Lbls = open(LblPath).read().strip().split("\n")
net = cv2.dnn.readNetFromDarknet(cnfg, wght)
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
boxes = []
def box_values(i):
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
cen = [int(x + w / 2), int(y + h / 2)]
return x,w,y,h,cen
def Bird_eye_transform(H,W,cen):
#src = np.float32([[952,669],[1222,559],[95,286],[404,286]])
src = np.float32([[1075,669],[1278,496],[4,286],[294,226]])
dst = np.float32([[0, H], [W, H], [0, 0], [W, 0]])
M = cv2.getPerspectiveTransform(src, dst)
pts = np.array([[cen]], dtype="float32")
warped_pt = cv2.perspectiveTransform(pts, M)[0][0]
warped_pt_scaled = [int(warped_pt[0] * 0.3), int(warped_pt[1] * 1)]
return warped_pt_scaled
def euclid_dist(A,B):
A__B = ( (A[0] - B[0])**2 + (A[1] - B[1])**2 )**0.5
return A__B
def closeness(OBJ1,OBJ2):
A_B = euclid_dist(OBJ1[2],OBJ2[2])
if (OBJ1[1] < OBJ2[1]): #compare the length of both ojcet detected
assum_W = OBJ1[0] # width is assumed to be shortest on compare
assum_H = OBJ1[1] # height is assumed to be shortest on compare
else:
assum_W = OBJ2[0] # width is assumed to be shortest on compare
assum_H = OBJ2[1] # height is assumed to be shortest on
def Tan_Sin(Tan):
Sin = abs(Tan/((1+Tan**2)**0.5))
return Sin
def Tan_Cos(Tan):
Cos = abs(1/((1+Tan**2)**0.5))
return Cos
Tan = 0
try:
Tan=( OBJ2[2][1] - OBJ1[2][1] ) / ( OBJ2[2][0] - OBJ1[2][0] )
except ZeroDivisionError:
T = 1.633123935319537e+16
Sin = Tan_Sin(Tan)
Cos = Tan_Cos(Tan)
d_hor = Cos*A_B
d_ver = Sin*A_B
vc_calib_hor = assum_W*1.3
vc_calib_ver = assum_H*0.4*angle_factor
c_calib_hor = assum_W *1.7
c_calib_ver = assum_H*0.2*angle_factor
if (0<d_hor<vc_calib_hor and 0<d_ver<vc_calib_ver):
return 1 #at risk red
elif 0<d_hor<c_calib_hor and 0<d_ver<c_calib_ver:
return 2 # yellow maintain distance
else:
return 0 #safe
"""
distance = assum_H*2/3
if (0<A_B<distance):
return 1
elif(0<distance<A_B<assum_H):
return 2
else:
return 0
"""
def yolo(frame):
(H, W) = frame.shape[:2]
#frm = np.zeros((int(H*sh),int(W*sw),3),np.uint8)
#frm[:] = solid_back_color
frm = background.copy()
#bckgrnd[]
#img3[100:400,100:400,:] = img2[100:400,100:400,:]
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416),swapRB=True, crop=False)
net.setInput(blob)
layerOutputs = net.forward(ln)
confidences = []
classIDs = []
global boxes
boxes.clear()
for outputs in layerOutputs:
for detection in outputs:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if Lbls[classID] == "person":
if confidence > 0.5 :
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.5,0.5)
if len(idxs) > 0:
idf = idxs.flatten()
centroid = []
obj_info = []
status = []
close_pair = []
s_close_pair = []
for i in idf:
_,w,_,h,cen = box_values(i)
centroid.append(cen)
obj_info.append([w, h, cen])
status.append(0)
for i in range(len(centroid)):
for j in range(len(centroid)):
Value = closeness(obj_info[i],obj_info[j])
if Value == 1:
close_pair.append([centroid[i], centroid[j]])
status[i] = 1
status[j] = 1
elif Value == 2:
s_close_pair.append([centroid[i], centroid[j]])
if status[i] != 1:
status[i] = 2
if status[j] != 1:
status[j] = 2
total_p = len(centroid)
low_risk_p = status.count(2)
high_risk_p = status.count(1)
safe_p = status.count(0)
kk = 0
for i in idf:
x,w,y,h,cen = box_values(i)
warped_pt_scaled = Bird_eye_transform(H,W,cen)
if status[kk] == 1:
cv2.rectangle(frame, (x, y), (x + w, y + h), (66, 66, 255), 2)
cv2.circle(frm,(warped_pt_scaled[0]*2, warped_pt_scaled[1]),
10,(66, 66, 255),-1)
elif status[kk] == 0:
cv2.rectangle(frame, (x, y), (x + w, y + h), (128, 255, 0), 2)
cv2.circle(frm,(warped_pt_scaled[0]*2, warped_pt_scaled[1]),
10,(128, 255, 0),-1)
else:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 120, 255), 2)
#cv2.circle(frm,(warped_pt_scaled[0], warped_pt_scaled[1]),10,(0, 120, 255),-1)
kk += 1
for h in close_pair:
cv2.line(frame, tuple(h[0]), tuple(h[1]), (66, 66, 255), 2)
centr_pnt1 = Bird_eye_transform(H,W*2,h[0])
centr_pnt2 = Bird_eye_transform(H,W*2,h[1])
cv2.line(frm, tuple(centr_pnt1), tuple(centr_pnt2), (66, 66, 255), 5)
for b in s_close_pair:
cv2.line(frame, tuple(b[0]), tuple(b[1]), (128, 255, 0), 2)
#centr_pnt1 = Bird_eye_transform(H,W,b[0])
#centr_pnt2 = Bird_eye_transform(H,W,b[1])
#cv2.line(frm, tuple(centr_pnt1), tuple(centr_pnt2), (0, 255, 255), 5)
return frame,frm,total_p, low_risk_p, high_risk_p, safe_p