-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
125 lines (109 loc) · 4.07 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import libcamera
from os import path
from libcamera import controls
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import FfmpegOutput
from time import time
from settings import Settings
from util import pathCheck
sensor_formats = {
"imx708": [
(4608, 2592),
(2304, 1296),
(1536, 864)
],
"imx477": [
(4056, 3040),
(2028, 1080)
],
"unknown": [
(2592, 1944),
(1296, 972)
]
}
class Camera:
def __init__(self, camera: Picamera2, settings: Settings, sensor: str="unknown"):
self.camera = camera
if sensor in sensor_formats:
self.sensor = sensor
else:
print("unknown sensor")
self.sensor = "unknown"
try:
self.camera.set_controls({"AfMode": controls.AfModeEnum.Manual})
self.autofocusEnabled = True
except:
self.autofocusEnabled = False
self.settings = settings
self.stillConfig = camera.create_video_configuration(
main={"size": sensor_formats[self.sensor][0]},
lores={"size": (480, 270)},
transform=libcamera.Transform(),
colour_space=libcamera.ColorSpace.Sycc(),
buffer_count=2,
display="lores",
encode="main",
queue=True
)
self.videoConfig = camera.create_video_configuration(
main={"size": (1920, 1080)},
lores={"size": (480, 270)},
transform=libcamera.Transform(),
colour_space=libcamera.ColorSpace.Sycc(),
buffer_count=4,
display="lores",
encode="main",
queue=True
)
self.camera.configure(self.stillConfig)
self.__setImagePath("imagePath", settings.get("imagePath"))
settings.subscribe("imagePath", self.__setImagePath)
self.__setVideoPath("videoPath", settings.get("videoPath"))
settings.subscribe("videoPath", self.__setVideoPath)
def __setImagePath(self, key: str, imagePath: str):
self.imagePath = path.expanduser(imagePath)
def __setVideoPath(self, key:str, videoPath: str):
self.videoPath = path.expanduser(videoPath)
def start(self):
self.camera.start()
#True for continuous False for Manual
def switchAutofocusMode(self, state: bool=False) -> bool:
if not self.autofocusEnabled: return False
if state: self.camera.set_controls({"AfMode": controls.AfModeEnum.Continuous})
else: self.camera.set_controls({"AfMode": controls.AfModeEnum.Manual})
return True
def autofocus(self) -> bool:
if not self.autofocusEnabled: return False
return self.camera.autofocus_cycle(wait=True)
def capture(self):
pathCheck(self.imagePath)
filePath = path.join(self.imagePath, str(int(time())) + ".jpg")
i = 0
while path.exists(filePath):
i += 1
filePath = path.join(self.imagePath, str(int(time())) + f"_{i}.jpg")
self.camera.capture_file(filePath)
def __record(self, callback=None):
def startRecord(job):
self.camera.wait(job)
self.switchAutofocusMode(state=True)
videoPath = path.join(self.videoPath, str(int(time())) + ".mp4")
encoder = H264Encoder(10000000)
encoder.output = FfmpegOutput(videoPath)
self.camera.start_encoder(encoder)
if callback is not None: callback()
return startRecord
def __stopRecord(self, callback):
def stop(job):
self.camera.wait(job)
self.switchAutofocusMode(state=False)
if callback is not None: callback()
return stop
def record(self, state=True, callback=None):
if state:
pathCheck(self.videoPath)
self.camera.switch_mode(self.videoConfig, signal_function=self.__record(callback))
else:
self.camera.stop_encoder()
self.camera.switch_mode(self.stillConfig, signal_function=self.__stopRecord(callback))