forked from HackerShackOfficial/Smart-Security-Camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
70 lines (59 loc) 路 2.05 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
import cv2
from imutils.video.pivideostream import PiVideoStream
import imutils
import time
import numpy as np
import pprint
class VideoCamera(object):
def __init__(self, flip = True):
self.vs = PiVideoStream((640,480)).start()
self.vs.camera.vflip = True
# self.vs.camera.framerate = 30
#self.vs.camera.exposure_mode = 'night'
#self.vs.camera.vflip = True
self.flip = flip
self.vs.camera.start_preview()
time.sleep(2.0)
def __del__(self):
self.vs.stop()
def flip_if_needed(self, frame):
# return frame
if not 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.tostring().tobytes()
def get_object(self, classifiers):
found_objects = False
xLocation = 0
yLocation = 0
frame = self.flip_if_needed(self.vs.read()).copy()
# print(frame[1].size
height, width = frame.shape[:2]
#print width
#print height
# small = cv2.resize(frame, (width/2, height/2), interpolation = cv2.INTER_AREA)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
classifier_used = -1
for i in range(len(classifiers)):
objects = classifiers[i].detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(64, 64),
flags=cv2.CASCADE_SCALE_IMAGE
)
if len(objects) > 0:
found_objects = True
print 'found with %s' % i
classifier_used = i
break
# Draw a rectangle around the objects
for (x, y, w, h) in objects:
cv2.circle(frame, (x + w / 2, y + h / 2), 10, (0, 255, 0), 2)
xLocation = x
yLocation = y
ret, jpeg = cv2.imencode('.jpg', frame)
return (jpeg.tobytes(), found_objects, xLocation, yLocation, classifier_used, width, height)