-
Notifications
You must be signed in to change notification settings - Fork 21
/
camera.py
37 lines (30 loc) · 992 Bytes
/
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
import cv2
from threading import Thread
class Camera(object):
def __init__(self, index):
self.cap = cv2.VideoCapture(index, cv2.CAP_V4L2)
if not self.cap.isOpened():
print('Failed to open camera {0}'.format(index))
exit(-1)
# self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
# self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
self.status = False
self.frame = None
def update(self):
while True:
try:
if self.cap.isOpened():
(self.status, self.frame) = self.cap.read()
else:
break
except cv2.error as e:
print(e)
break
def get_frame(self):
return self.frame, self.status
def close(self):
self.cap.release()
self.thread.join()