-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeopleCounterOp.py
183 lines (141 loc) · 4.46 KB
/
PeopleCounterOp.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
import numpy as np
import cv2
import Person
import time
import threading
from ws4py.client.threadedclient import WebSocketClient
import json
cnt_up = 0
flag = False
cap = cv2.VideoCapture("sample-video.avi")
cap.set(3, 320) # Width
cap.set(4, 240) # Height
w = cap.get(3)
h = cap.get(4)
frameArea = h * w
areaTH = frameArea / 250
print 'Area Threshold', areaTH
# Position of the Lines
line_up = int(2.5 * (h / 5))
line_down = int(3 * (h / 5))
up_limit = int(2 * (h / 5))
down_limit = int(3.5 * (h / 5))
fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows=True)
kernelOp = np.ones((3, 3), np.uint8)
kernelCl = np.ones((11, 11), np.uint8)
# Variables
font = cv2.FONT_HERSHEY_SIMPLEX
persons = []
pid = 1
def dataSender():
global cnt_up
global ws
if flag:
threading.Timer(2.0, dataSender).start()
if cnt_up > 0:
# Send Data to the server
print cnt_up
ws.send_message({
"action": "add",
"amount": cnt_up,
"datetime": time.strftime("%Y-%m-%d %H:00:00")
})
cnt_up = 0
else:
ws.close()
class DummyClient(WebSocketClient):
def opened(self):
data = json.dumps({
"action": "setname",
"username": "hardwareDevice"
})
self.send(data)
print "Connecting to the Server..."
def closed(self, code, reason=None):
print "Closed down", code, reason
def received_message(self, m):
data = json.loads(str(m))
if data["action"] == "setname":
if data["success"]:
print "Device Successfully Connected"
dataSender()
capture()
else:
print "There is a problem connecting to the Server..."
def send_message(self, msg):
msg = json.dumps(msg)
self.send(msg)
def capture():
# Initialize all of the global variables
global cap
global fgbg
global kernelCl
global kernelCl
global up_limit
global down_limit
global line_up
global line_down
global cnt_up
global pid
global persons
global font
global flag
# Real-time Capturing
while cap.isOpened():
flag = True
ret, frame = cap.read()
fgmask2 = fgbg.apply(frame)
try:
ret, imBin2 = cv2.threshold(fgmask2, 200, 255, cv2.THRESH_BINARY)
# Opening
mask2 = cv2.morphologyEx(imBin2, cv2.MORPH_OPEN, kernelOp)
# Closing
mask2 = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kernelCl)
except:
break
_, contours0, hierarchy = cv2.findContours(mask2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours0:
area = cv2.contourArea(cnt)
if area > areaTH:
M = cv2.moments(cnt)
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
x, y, w, h = cv2.boundingRect(cnt)
new = True
if cy in range(up_limit, down_limit):
for i in persons:
if abs(cx - i.getX()) <= w and abs(cy - i.getY()) <= h:
new = False
i.updateCoords(cx, cy)
# Check if a person is going up
if i.going_UP(line_down, line_up):
# Add to count
cnt_up += 1
break
if i.getState() == '1' and i.getDir() == 'up' and i.getY() < up_limit:
i.setDone()
if i.timedOut():
index = persons.index(i)
persons.pop(index)
del i
if new:
p = Person.MyPerson(pid, cx, cy)
persons.append(p)
pid += 1
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('Frame1', frame)
cv2.imshow('Frame', mask2)
# Press ESC to terminate the program
k = cv2.waitKey(1) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
flag = False
if __name__ == '__main__':
try:
ws = DummyClient('ws://127.0.0.1:2000/')
ws.connect()
ws.run_forever()
except KeyboardInterrupt:
ws.close()