-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathcamera.py
More file actions
64 lines (51 loc) · 1.44 KB
/
Copy pathcamera.py
File metadata and controls
64 lines (51 loc) · 1.44 KB
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
59
60
61
62
63
64
"""A threaded cv2 camera.
Largely from pyimagesearch.com
"""
import threading
import cv2
class CameraStream(object):
def __init__(self, src=0):
self.stream = cv2.VideoCapture(src)
if not self.stream.isOpened():
src = 1 - src
self.stream = cv2.VideoCapture(src)
if not self.stream.isOpened():
sys.exit("Error: Camera didn't open for capture.")
# Setup frame dims.
self.stream.set(cv2.CV_CAP_PROP_FRAME_WIDTH, 320)
self.stream.set(cv2.CV_CAP_PROP_FRAME_HEIGHT, 240)
self.grabbed, self.frame = self.stream.read()
self.stopped = False
def start(self):
"""Start the thread to read frames from the video stream."""
t = threading.Thread(target=self.update, args=())
t.daemon = True
t.start()
return self
def update(self):
"""Grab frames until told to stop."""
while not self.stopped:
self.grabbed, self.frame = self.stream.read()
def read(self):
return self.frame
def stop(self):
self.stopped = True
'''
import glob,os,sys
# Replays a recorded lap
class VirtualCameraStream(object):
def __init__(self, src=0):
self.stopped = False
self.files = glob.glob(os.getenv('IMAGES_DIR')+'/*.png')
self.index = 0
def start(self):
self.index = 0
return self
def read(self):
img = cv2.imread(self.files[self.index])
self.index += 1
self.index = self.index % len(self.files)
return img
def stop(self):
self.stopped = True
'''