Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions Number of crowd
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import cv2
import numpy as np
from time import sleep

l_min = 80

a_min = 80


offset = 6


pos_l = 650


delay = 60


detec = []
car = 0


def p_centro(x, y, w, h):
x1 = int(w / 2)
y1 = int(h / 2)
cx = x + x1
cy = y + y1
return cx, cy

cap = cv2.VideoCapture('video.mp4')
subtracao = cv2.bgsegm.createBackgroundSubtractorMOG()

while True:

ret, frame1 = cap.read()

t = float(1 / delay)

sleep(t)

grey = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)

blur = cv2.GaussianBlur(grey, (3, 3), 5)

img_sub = subtracao.apply(blur)
dilat = cv2.dilate(img_sub, np.ones((5, 5)))
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
dilatada = cv2.morphologyEx(dilat, cv2.MORPH_CLOSE, kernel)
dilatada = cv2.morphologyEx(dilatada, cv2.MORPH_CLOSE, kernel)
contor, h = cv2.findContours(dilatada, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cv2.line(frame1, (25, pos_l), (1200, pos_l), (255, 127, 0), 3)
for (i, c) in enumerate(contor):
(x, y, w, h) = cv2.boundingRect(c)
validate = (w >= l_min) and (h >= a_min)
if not validate:
continue


cv2.rectangle(frame1, (x, y), (x + w, y + h), (0, 255, 0), 2)
centro = p_centro(x, y, w, h)
detec.append(centro)
cv2.circle(frame1, centro, 4, (0, 0, 255), -1)


for (x, y) in detec:
if y < (pos_l + offset) and y > (pos_l - offset):
car += 1
cv2.line(frame1, (25, pos_l), (1200, pos_l), (0, 127, 255), 3)
detec.remove((x, y))
print("car_detected : " + str(car))


cv2.putText(frame1, "AUTO COUNT : " + str(car), (450, 70), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)

cv2.imshow("Video Original", frame1)

if cv2.waitKey(1) == 7:
break


cv2.destroyAllWindows()
cap.release()