-
Notifications
You must be signed in to change notification settings - Fork 0
/
D_main.py
238 lines (195 loc) · 7.29 KB
/
D_main.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import cv2 # OpenCV
import numpy as np # Tabalha com a parte cientifica (Vetores e Matrizes)
import time
import dlib
import time
import threading
import math
tempoGB = 0
statusSemaforo = 'null'
extent = False
count = 0
semaforoAberto = True
def thread_delay(null):
global tempoGB
global count
global semaforoAberto
count = 0
while count < tempoGB:
print("TEMPO:->:", count)
time.sleep(1)
count += 1
if count == tempoGB:
tempoGB = 0
if semaforoAberto == False:
semaforoAberto = True
else:
semaforoAberto = False
print('SEMAFORO', '->', semaforoAberto)
return 0
def Calculating_time_extent(classes):
global tempoGB
global extent
global semaforoAberto
if tempoGB == 0 and tempoGB < 20:
tempoGB = 20
extent = False
t1 = threading.Thread(target=thread_delay, args=(0,))
t1.start()
print("TEMPO: PESSOA ", tempoGB)
# Text = 'TEMPO PARA PESSOA: '
# cv2.putText(frame, Text+str(tempoGB)+'s', (0, 25),
# cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 150, 0), 3)
if classes == "chair" and extent == False and tempoGB < 30:
tempoGB += 10
print("TEMPO: CADEIRANTE ", tempoGB)
extent = True
# cv2.putText(frame, Text+str(tempoGB)+'s', (0, 25),
# cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 150, 0), 3)
def imageShow(img):
cv2.imshow('TELA DE CAPTURA', img)
# print(img.shape) # FORMATO DA IMAGEM
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# CONSTUÇÃO DO BLOB
def blobImage(net, img, ln):
start = time.time()
blob = cv2.dnn.blobFromImage(
img, 1/255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
layer_Outputs = net.forward(ln)
end = time.time()
# print("[TEMPO DE DETECÇÃO] {:.2f} seconds".format(end - start))
return net, img, layer_Outputs
# REALIZAR DETECÇÃO
def detectionImage(_detection, _threshold, _AllBoxes, _AllConfidences, _AllClassesID, _img):
(H, W) = _img.shape[:2]
scores = _detection[5:]
classeID = np.argmax(scores)
confidence = scores[classeID]
if confidence > _threshold:
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))
_AllBoxes.append([x, y, int(width), int(height)])
_AllConfidences.append(float(confidence))
_AllClassesID.append(classeID)
return _AllBoxes, _AllConfidences, _AllClassesID
# TAMANHO DO VIDEO
def reSizeX(_width, _height, _widthMax=600):
if(_width > _widthMax):
newSize = _width / _height
video_width = _widthMax
video_height = int(video_width/newSize)
else:
video_width = _width
video_height = _height
return video_width, video_height
# CRIAR CAIXAS
def createBoxes(_img, i, _confidences, _boxes, _COLORS, _LABELS, _AllClassesID):
(x, y) = (_boxes[i][0], _boxes[i][1])
(w, h) = (_boxes[i][2], _boxes[i][3])
color = [int(c) for c in _COLORS[_AllClassesID[i]]]
text = "{}: {:.4f}".format(
_LABELS[_AllClassesID[i]], _confidences[i])
fundo = np.full((_img.shape), (0, 0, 0), dtype=np.uint8)
cv2.putText(fundo, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (255, 255, 255), 2)
fx, fy, fw, fh = cv2.boundingRect(fundo[:, :, 2])
cv2.rectangle(_img, (x, y), (x + w, y + h), color, 2)
cv2.rectangle(_img, (fx, fy), (fx+fw, fy+fh), color, -1)
cv2.rectangle(_img, (fx, fy), (fx+fw, fy+fh), color, 3)
cv2.putText(_img, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 0, 0), 1)
return _img, x, y, w, h
# CAPTURA FRAME A FRAME
def DetectionX():
# ARQUIVOS
input_file = 'arquivos/lv1.mp4'
weights_path = 'arquivos/yolov4-tiny.weights'
cfg_path = 'arquivos/yolov4-tiny.cfg'
names_path = 'arquivos/coco.names'
# CONFIG PRECISÃO
threshold = 0.3 # Nivel de confiança?
threshold_NMS = 0.4
font_smal, font_big = 0.4, 0.6
font_tipe = cv2.FONT_HERSHEY_SIMPLEX
fontLine = 2 # inteiro
amostrar_exibir = 20
amostra_atual = 0
# CARREGANDO NOME DAS CLASSES
with open(names_path, 'r') as names:
LABELS = [cname.strip() for cname in names.readlines()]
# CARREGANDO ARQUIVOS
net = cv2.dnn.readNetFromDarknet(cfg_path, weights_path)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
# CORES DAS CLASSES
np.random.seed(50)
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype='uint8')
# CAMADAS DE SAIDA
ln = net.getLayerNames()
ln = [ln[i-1] for i in net.getUnconnectedOutLayers()]
# CARREGAR VIDEO
cap = cv2.VideoCapture(0)
connected, video = cap.read()
video_height = video.shape[0]
video_width = video.shape[1]
# CLIQUE [1] EXIT
video_width, video_height = reSizeX(video_width, video_height)
while(cv2.waitKey(1) < 0):
_connected, _frame = cap.read()
if not _connected and type(_frame) == type(None):
print("VIDEO NULL")
break
t = time.time()
_frame = cv2.resize(_frame, (video_width, video_height))
try:
(H, W) = _frame.shape[:2]
except:
print('VIDEO SHAPE ERRO')
continue
net, _frame, layerOutputs = blobImage(net, _frame, ln)
AllBoxes = []
AllConfidences = []
AllClassesID = []
for output in layerOutputs:
for detection in output:
AllBoxes, AllConfidences, AllClassesID = detectionImage(
detection, threshold, AllBoxes, AllConfidences, AllClassesID, _frame)
objects = cv2.dnn.NMSBoxes(AllBoxes, AllConfidences,
threshold, threshold_NMS)
if len(objects) > 0:
for i in objects.flatten():
Calculating_time_extent(LABELS[AllClassesID[i]])
if semaforoAberto:
_frame, x, y, w, h = createBoxes(
_frame, i, AllConfidences, AllBoxes, COLORS, LABELS, AllClassesID)
cv2.putText(_frame, "PROCSSAMENTO {:.2f}s".format(time.time()-t),
(20, video_height-20), font_tipe, font_big, (255, 255, 255), fontLine, lineType=cv2.LINE_AA)
cv2.putText(_frame, "TEMPO: "+str(count)+'s', (0, 25),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 150, 0), 3)
cv2.putText(_frame, "SEMAFORO: "+str(semaforoAberto), (0, 60),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
# t1 = threading.Thread(target=thread_delay, args=(5,))
# t1.start()
# print(tempoGB)
if semaforoAberto:
semaforoImage = cv2.imread("arquivos/sTrue.png")
cv2.putText(_frame, "N DETECCOES: "+str(len(AllBoxes)), (0, 100),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
else:
semaforoImage = cv2.imread("arquivos/sFalse.png")
cv2.imshow('SEMAFORO', semaforoImage)
imageShow(_frame)
def main():
print("MAIN!")
DetectionX()
# t1 = threading.Thread(target=thread_delay, args=(5,))
t2 = threading.Thread(target=DetectionX, args=('',))
# t1.start()
t2.start()
print("terminou: ")
cv2.destroyAllWindows()
if __name__ == "__main__":
main()