-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_opencv.py
33 lines (26 loc) · 901 Bytes
/
camera_opencv.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
import os
import cv2
from base_camera import BaseCamera
class Camera(BaseCamera):
video_source = 0
def __init__(self):
if os.environ.get('OPENCV_CAMERA_SOURCE'):
Camera.set_video_source(int(os.environ['OPENCV_CAMERA_SOURCE']))
super(Camera, self).__init__()
@staticmethod
def set_video_source(source):
Camera.video_source = source
@staticmethod
def frames():
camera = cv2.VideoCapture(Camera.video_source)
if not camera.isOpened():
raise RuntimeError('Could not start camera.')
while True:
# read current frame
_, img = camera.read()
w = 1280
h = 720
factor = 0.5
img = cv2.resize(img, (int(w*factor), int(h*factor)))
# encode as a jpeg image and return it
yield cv2.imencode('.jpg', img)[1].tobytes()