-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
33 lines (26 loc) · 853 Bytes
/
camera.py
File metadata and controls
33 lines (26 loc) · 853 Bytes
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
# camera.py
import ctypes
from contextlib import contextmanager
# load and configure the shared lib once
_lib = ctypes.CDLL("./libcamera.so")
_lib.capture_frame_cpp.restype = ctypes.c_char_p
_lib.capture_frame_cpp.argtypes = [] # takes no params
_lib.free_frame_cpp.restype = None
_lib.free_frame_cpp.argtypes = [ctypes.c_char_p]
class Camera:
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def capture_frame(self) -> str:
ptr = _lib.capture_frame_cpp()
if not ptr:
raise RuntimeError("capture_frame_cpp returned NULL")
try:
# Base64 is ASCII-safe
return ptr.decode("ascii")
finally:
_lib.free_frame_cpp(ptr)
def close(self):
# _lib.cleanup_camera()
pass