-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_base.py
46 lines (37 loc) · 1.01 KB
/
camera_base.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
import abc
import cv2
import typing
class CameraBase(abc.ABC):
def start(self) -> None:
if self.is_running():
return
try:
self._start()
except Exception as e:
print(f"Failed to start camera: {e}")
raise e
if not self.is_running():
raise Exception("Failed to start camera")
@abc.abstractmethod
def _start(self) -> None:
pass
def stop(self):
if not self.is_running():
return
self._stop()
if self.is_running():
raise Exception("Failed to stop camera")
@abc.abstractmethod
def _stop(self) -> None:
pass
@abc.abstractmethod
def read_frame(self) -> typing.Tuple[bool, cv2.typing.MatLike]:
pass
@abc.abstractmethod
def is_running(self) -> bool:
pass
def __enter__(self) -> "CameraBase":
self.start()
return self
def __exit__(self, exc_type, exc_value, traceback) -> None:
self.stop()