-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcamera.py
executable file
·109 lines (84 loc) · 3.41 KB
/
camera.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
import threading
import time
import cv2
import imutils
import numpy as np
from imutils.video import VideoStream
from basicmotiondetector import BasicMotionDetector
def synchronized(func):
func.__lock__ = threading.Lock()
def synced_func(*args, **kws):
with func.__lock__:
return func(*args, **kws)
return synced_func
class Camera(object):
motionDetector = BasicMotionDetector(accumWeight=0.5, deltaThresh=5, minArea=500)
def __init__(self, flip=False, src=0):
self.vs = VideoStream(src=src)
self.flip = flip
time.sleep(2.0)
def __del__(self):
self.vs.stop()
def flip_if_needed(self, frame):
if self.flip:
return np.flip(frame, 0)
return frame
def get_frame(self):
frame = self.flip_if_needed(self.vs.read())
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()
def get_object(self, classifier):
found_objects = False
frame = self.flip_if_needed(self.vs.read()).copy()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
objects = self.get_objects(classifier, gray)
if len(objects) > 0:
found_objects = True
# Draw a rectangle around the objects
for (x, y, w, h) in objects:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes(), found_objects
def get_objects(self, classifier, gray):
objects = classifier.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
return objects
def get_object_with_basic_motion_detection(self):
frames = []
found_objects = False
frame = self.vs.read()
frame = imutils.resize(frame, width=500)
# convert the frame to grayscale, blur it slightly, update
# the motionDetector detector
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
locations_containing_motion = Camera.motionDetector.update(gray)
# we should allow the motionDetector detector to "run" for a bit
# and accumulate a set of frames to form a nice average
if Camera.motionDetector.scanned_frames_counter < 32:
frames.append(frame)
else:
# otherwise, check to see if motionDetector was detected
if len(locations_containing_motion) > 0:
found_objects = True
# initialize the minimum and maximum (x, y)-coordinates,
# respectively
(minX, minY) = (np.inf, np.inf)
(maxX, maxY) = (-np.inf, -np.inf)
# loop over the locations of motionDetector and accumulate the
# minimum and maximum locations of the bounding boxes
for l in locations_containing_motion:
(x, y, w, h) = cv2.boundingRect(l)
(minX, maxX) = (min(minX, x), max(maxX, x + w))
(minY, maxY) = (min(minY, y), max(maxY, y + h))
# draw the bounding box
cv2.rectangle(frame, (minX, minY), (maxX, maxY),
(0, 0, 255), 3)
frames.append(frame)
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes(), found_objects