-
Notifications
You must be signed in to change notification settings - Fork 3
/
camera.py
36 lines (29 loc) · 988 Bytes
/
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
import cv2 as cv
from time import localtime, strftime
class Camera(object):
CAPTURES_DIR = "static/temp/"
RESIZE_RATIO = 1.0
def __init__(self):
self.video = cv.VideoCapture(0)
def __del__(self):
self.video.release()
def get_frame(self):
success, frame = self.video.read()
if not success:
return None
if (Camera.RESIZE_RATIO != 1):
frame = cv.resize(frame, None, fx=Camera.RESIZE_RATIO, \
fy=Camera.RESIZE_RATIO)
return frame
def get_feed(self):
frame = self.get_frame()
if frame is not None:
ret, jpeg = cv.imencode('.jpg', frame)
return jpeg.tobytes()
def capture(self):
frame = self.get_frame()
stamp = "temp_img"
filename = Camera.CAPTURES_DIR + stamp +".jpg"
if not cv.imwrite(filename, frame):
raise RuntimeError("Unable to capture image "+stamp)
return stamp