-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebcamVideoStream.py
More file actions
146 lines (130 loc) · 5.42 KB
/
WebcamVideoStream.py
File metadata and controls
146 lines (130 loc) · 5.42 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
137
138
139
140
141
142
143
144
145
146
from __future__ import print_function
from threading import Thread
import threading
import numpy as np
import cv2
import sys
import datetime
import time
import People
import math
personSize = 400
persons = []
pid = 1
entered = 0
exited = 0
def draw_detections(img, rects, thickness = 2):
for x, y, w, h in rects:
if w > 400 and h > 400:
pad_w, pad_h = int(0.15*w), int(0.05*h)
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), thickness)
class WebcamVideoStream(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
codec = 1196444237.0 # MJPG
#print 'fourcc:', decode_fourcc(codec)
self.video.set(cv2.CAP_PROP_FOURCC, codec)
self.video.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
self.video.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
self.video.set(cv2.CAP_PROP_FPS, 30)
self.w = self.video.get(3) #CV_CAP_PROP_FRAME_WIDTH
self.h = self.video.get(4) #CV_CAP_PROP_FRAME_HEIGHT
self.rangeLeft = int(1*(self.w/6))
self.rangeRight = int(5*(self.w/6))
self.midLine = int(2.5*(self.w/6))
_, self.rawImage = self.video.read()
self.firstFrame = cv2.cvtColor(self.rawImage, cv2.COLOR_BGR2GRAY)
ret, jpeg = cv2.imencode('.jpg', self.rawImage)
self.frameDetections = jpeg.tobytes()
self.contours= []
# initialize the variable used to indicate if the thread should
# be stopped
self.stopped = False
def __del__(self):
self.video.release()
def start(self):
# start the thread to read frames from the video stream
t = Thread(target=self.update, args=())
t.daemon = True
t.start()
t2 = Thread(target=self.updateContours, args=())
t2.daemon = True
t2.start()
return self
def update(self):
# keep looping infinitely until the thread is stopped
count = 1
while True:
# if the thread indicator variable is set, stop the thread
if self.stopped:
return
# otherwise, read the next frame from the stream
(self.grabbed, self.rawImage) = self.video.read()
img = self.rawImage.copy()
#draw rectangles around the people
draw_detections(img,self.contours)
#visually show the counters
cv2.putText(img, "Entered: " + str(entered) ,(10,20),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),2,cv2.LINE_AA)
cv2.putText(img, "Exited: " + str(exited) ,(10,50),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),2,cv2.LINE_AA)
ret, jpeg = cv2.imencode('.jpg', img)
self.frameDetections = jpeg.tobytes()
def updateContours(self):
# keep looping infinitely until the thread is stopped
global personSize
while True:
# if the thread indicator variable is set, stop the thread
if self.stopped:
return
#get the current frame and look for people
total = datetime.datetime.now()
img = cv2.cvtColor(self.rawImage, cv2.COLOR_BGR2GRAY)
total = datetime.datetime.now()
frameDelta = cv2.absdiff(self.firstFrame, img)
ret, thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)
(allContours, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
personContours = []
for c in allContours:
# only look at contours larger than a certain size
#if cv2.contourArea(c) > 1:
#print(cv2.contourArea(c))
if cv2.contourArea(c) > personSize:
personContours.append(cv2.boundingRect(c))
self.contours = personContours
# track the people in the frame
self.people_tracking(self.contours)
def readDetections(self):
# return the frame with people detections
return self.frameDetections
def stop(self):
# indicate that the thread should be stopped
self.stopped = True
def people_tracking(self, rects):
global pid
global entered
global exited
for x, y, w, h in rects:
new = True
xCenter = x + w/2
yCenter = y + h/2
inActiveZone= xCenter in range(self.rangeLeft,self.rangeRight)
for index, p in enumerate(persons):
dist = math.sqrt((xCenter - p.getX())**2 + (yCenter - p.getY())**2)
if dist <= w/2 and dist <= h/2:
if inActiveZone:
new = False
if p.getX() < self.midLine and xCenter >= self.midLine:
print("[INFO] person going left " + str(p.getId()))
entered += 1
if p.getX() > self.midLine and xCenter <= self.midLine:
print("[INFO] person going right " + str(p.getId()))
exited += 1
p.updateCoords(xCenter,yCenter)
break
else:
print("[INFO] person removed " + str(p.getId()))
persons.pop(index)
if new == True and inActiveZone:
print("[INFO] new person " + str(pid))
p = People.Person(pid, xCenter, yCenter)
persons.append(p)
pid += 1