-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideo1.py
More file actions
136 lines (98 loc) · 4.48 KB
/
Video1.py
File metadata and controls
136 lines (98 loc) · 4.48 KB
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
#código para verificação de rosto e contagem de pessoas (entrada em ônibus)
import cv2 as cv
import numpy as np
import logging
def center(x, y, w, h):
x1 = int(w / 2)
y1 = int(h / 2)
cx = x + x1
cy = y + y1
return cx,cy
cap = cv.VideoCapture('1.mp4') #Verificando rosto e contabilizador de rosto
bg = cv.createBackgroundSubtractorMOG2()
detects = []
post = 150
offset = 50
xy1 = (20, post)
xy2 = (300, post)
post5 = 120
offset5 = 30
xy5 = (1, post5)
xy6 = (300, post5)
total = 0
up = 0
down = 0
while 1:
ret, frame = cap.read()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
mask = bg.apply(gray)
retval, th = cv.threshold(mask, 200, 255, cv.THRESH_BINARY)
kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))
opening = cv.morphologyEx(th, cv.MORPH_OPEN, kernel, iterations = 2)
dilation = cv.dilate(opening, kernel, iterations = 8)
closing = cv.morphologyEx(dilation, cv.MORPH_CLOSE, kernel, iterations = 8)
contours, hierarchy = cv.findContours(closing, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
i = 0
for c in contours:
(x,y,w,h) = cv.boundingRect(c)
area = cv.contourArea(c)
if int(area) > 3000:
centro = center(x, y, w, h)
cv.putText(frame, str(i), (x+5, y+15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,255), 2)
cv.circle(frame, centro, 4, (0, 0, 255), -1)
cv.rectangle(frame, (x,y), (x+w , y+h), (0,255,0), 2)
if len(detects) <= i:
detects.append([])
if centro [1] > post-offset and centro[1] < post+offset:
detects[i].append(centro)
else:
detects[i].clear()
i += 1
if i == 0:
detects.clear()
i = 0
if len(contours) == 0:
detects.clear()
else:
for detect in detects:
for(c, l) in enumerate(detect):
if detect[c-1][1] < post and l[1] > post:
detect.clear()
up += 1
total += 1
cv.line(frame, xy1, xy2, (0,255,0), 5)
logging.basicConfig(filename="log.txt", level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') #Gerando log quando detectado movimentação de pessoa
logging.info("PESSOAS NO AMBIENTE: "+str(up-down)+"/50")
continue
if detect[c-1][1] > post and l[1] < post:
detect.clear()
down += 1
total += 1
cv.line(frame, xy1, xy2, (0,0,255), 5)
logging.basicConfig(filename="log.txt", level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') #Gerando log quando detectado movimentação de pessoa
logging.info("PESSOAS NO AMBIENTE: "+str(up-down)+"/50")
continue
if c > 0:
cv.line(frame, detect[c-1], l, (0,0,255), 1)
class_body = cv.CascadeClassifier('haarcascadefrontalface.xml')
if cap.isOpened():
ret, frame = cap.read()
frame = cv.resize(frame, None, fx=0.7, fy=0.7, interpolation = cv.INTER_LINEAR)
grayscale_img = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
bodies_detect = class_body.detectMultiScale(grayscale_img, 1.2 , 7)#ajuste fino
for (x,y,w,h) in bodies_detect:
cv.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
cv.imshow("frame", frame)
cv.line(frame, xy1, xy2, (255,0,0), 3)
#cv.line(frame,(yy[0], post-offset), (yy2[0], post-offset), (255,255,0), 2)
#cv.line(frame,(y1[0], post+offset), (y2[0], post+offset), (255,255,0), 2)
cv.putText(frame, "MOVIMENTACAO DE PESSOAS: "+str(total), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255),2) #quantidade de pessoas detectadas perto da linha de verificação
cv.putText(frame, "ENTRANDO: "+str(up), (10, 40), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0),2)
cv.putText(frame, "SAINDO: "+str(down), (10, 60), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255),2)
cv.putText(frame,"NO AMBIENTE: "+str(up-down)+"/50 PESSOAS", (10, 80), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255),2)
cv.imshow("frame", frame)
k = cv.waitKey(20)
if k == 27:
break
cap.release()
cv.destroyAllWindows()