forked from miguelgrinberg/flask-video-streaming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera_v4l2.py
35 lines (30 loc) · 1.08 KB
/
camera_v4l2.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
import io
from PIL import Image
import select
import v4l2capture
from base_camera import BaseCamera
class Camera(BaseCamera):
"""Requires python-v4l2capture module: https://github.com/gebart/python-v4l2capture"""
video_source = "/dev/video0"
@staticmethod
def frames():
video = v4l2capture.Video_device(Camera.video_source)
# Suggest an image size. The device may choose and return another if unsupported
size_x = 1280
size_y = 720
size_x, size_y = video.set_format(size_x, size_y)
video.create_buffers(1)
video.queue_all_buffers()
video.start()
bio = io.BytesIO()
try:
while True:
select.select((video,), (), ()) # Wait for the device to fill the buffer.
image_data = video.read_and_queue()
image = Image.frombytes("RGB", (size_x, size_y), image_data)
image.save(bio, format="jpeg")
yield bio.getvalue()
bio.seek(0)
bio.truncate()
finally:
video.close()