-
Notifications
You must be signed in to change notification settings - Fork 3
/
camera.py
58 lines (40 loc) · 1.45 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
import multiprocessing as mp
from multiprocessing import JoinableQueue, Event
from queue import Empty
from picamera import PiCamera
import logging
logger = logging.getLogger(__name__)
class Camera(object):
"""Control the camera using the Picamera module (threaded)"""
def __init__(self):
self.file_queue = JoinableQueue(1)
self.quit_event = Event()
def _worker(self):
logger.info("Initialising camera...")
camera = PiCamera()
camera.rotation = 180
camera.zoom = (0.05, 0.0, 0.75, 0.95)
camera.resolution = (1024, 768)
while True:
try: # Timeout raises queue.Empty
file_path = self.file_queue.get(block=True, timeout=0.1)
logger.debug("Capturing image")
camera.capture(file_path, resize=(240, 240))
self.file_queue.task_done()
except Empty:
if self.quit_event.is_set():
logger.debug("Quitting camera thread...")
break
def start(self, file_path):
logger.debug("Calling start")
self.file_queue.put(file_path, block=True)
def join(self):
logger.debug("Calling join")
self.file_queue.join()
def launch(self):
logger.debug("Initialising worker")
self.p = mp.Process(target=self._worker, daemon=True)
self.p.start()
def quit(self):
self.quit_event.set()
self.p.join()