-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuvc_camera.py
56 lines (46 loc) · 1.4 KB
/
uvc_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
import cv2
import numpy as np
import time
import typing
class UVCCamera:
def __init__(
self,
cam_index=0,
mtx=None,
dist=None,
capture_size: typing.Tuple[int, int] = (640, 480),
):
super().__init__()
self.cam_index = cam_index
self.mtx = mtx
self.dist = dist
self.curr_color_frame: typing.Union[np.ndarray, None] = None
self.capture_size = capture_size
def capture(self):
self.cap = cv2.VideoCapture(self.cam_index, cv2.CAP_DSHOW) #windows
# self.cap = cv2.VideoCapture(self.cam_index) #linux
width, height = self.capture_size
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
def update_frame(self) -> bool:
ret, self.curr_color_frame = self.cap.read()
return ret
def color_frame(self) -> typing.Union[np.ndarray, None]:
return self.curr_color_frame
def release(self):
self.cap.release()
if __name__ == "__main__":
cam = UVCCamera(2)
cam.capture()
while True:
if not cam.update_frame():
continue
frame = cam.color_frame()
if frame is None:
time.sleep(0.01)
continue
print(frame.shape)
window_name = "preview"
cv2.imshow(window_name, frame)
if cv2.waitKey(1) == ord("q"):
break