-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcamera.py
49 lines (43 loc) · 1.42 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
#!/usr/bin/python
import os, io, threading, picamera
from picamera import PiCamera
class Camera(object):
running = True
current_frame = None
thread = None
camera = None
def __init__(self, resolution="VGA",quality=50,framerate=60):
self.resolution = resolution
self.quality = quality
self.framerate = framerate
self.newframe_notify = threading.Event()
def outputs(self):
stream = io.BytesIO()
while self.running:
yield stream
stream.seek(0)
self.current_frame = stream.getvalue()
self.newframe_notify.set()
stream.seek(0)
stream.truncate()
self.current_frame = None
def start(self):
if self.thread: return
self.running = True
self.thread = threading.Thread(name="CameraThread", target=self._run)
self.thread.daemon = True
self.thread.start()
def stop(self):
if not self.thread: return
self.running = False
self.thread.join()
self.thread = None
def _run(self):
try:
self.camera = picamera.PiCamera()
self.camera.resolution = self.resolution
self.camera.framerate = self.framerate
self.camera.capture_sequence(self.outputs(), 'jpeg', use_video_port=True, quality=self.quality)
finally:
self.camera.close()
print('Camera closed!')