-
Notifications
You must be signed in to change notification settings - Fork 1
/
webcam.py
51 lines (38 loc) · 1.11 KB
/
webcam.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
import threading
import time
import cv2
import config
# Rate at which the webcam will be polled for new images.
CAPTURE_HZ = 30.0
class OpenCVCapture(object):
def __init__(self, device_id=0):
# Open the camera with webcam ID.
self._camera = cv2.VideoCapture(0)
if not self._camera.isOpened():
self._camera.open(0)
self._capture_frame = None
self._capture_lock = threading.Lock()
self._capture_thread = threading.Thread(target=self._grab_frames)
self._capture_thread.daemon = True
self._capture_thread.start()
def _grab_frames(self):
while True:
retval, frame = self._camera.read()
with self._capture_lock:
self._capture_frame = None
if retval:
self._capture_frame = frame
time.sleep(1.0/CAPTURE_HZ)
def read(self):
#Read single frame- return as OpenCv image - numpy array
frame = None
with self._capture_lock:
frame = self._capture_frame
while frame == None:
time.sleep(0)
with self._capture_lock:
frame = self._capture_frame
# for testing saving image.
cv2.imwrite(config.DEBUG_IMAGE, frame)
# Return the capture image data.
return frame