-
Notifications
You must be signed in to change notification settings - Fork 0
/
alumia_piCameraPhd.py
367 lines (248 loc) · 11.9 KB
/
alumia_piCameraPhd.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from picamera2 import Picamera2
from picamera2.encoders import JpegEncoder, H264Encoder
from picamera2.outputs import FileOutput
from libcamera import Transform
from alumia_mjpegServer import StreamingServer, StreamingHandler, StreamingOutput
import socket
from threading import Thread, Event
import queue
import os
import cv2
from numpy import linspace
LORES_RES = (320,180)
class PiCameraStream(object):
def __init__(self, root_win_folder, root_mon_folder, resolution: tuple, quality: int, encoder_type: str, sub_folder="/video/"):
self.camera = Picamera2()
self.kill = Event()
self.current_fps = 30
self.current_exp = 0 # tenho de perceber qual o inicial
self.cam_config = self.camera.create_video_configuration()
self.cam_config['lores'] = {"size": LORES_RES, 'format': 'YUV420'} #180?
self.main_resolution = resolution
self.main_quality = quality
self.encoder = self.select_encoder(encoder_type=encoder_type, quality=quality)
self.camera.configure(self.cam_config)
self.set_cam_param(temp_change=False, resolution=resolution, quality=quality)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.thread = 0 #used in http video server
self.server = 0 #used in http video server
#init the thread used to write the low res file
self.lores_queue = queue.Queue(maxsize=1)
self.lores_event = Event()
self.lores_thread = Thread(target=self.lores_write, args=())
self.lores_thread.daemon = True
self.lores_thread.start()
#STATE
self.current_snippet = -1
self.current_main = -1
self.stream_state = False
self.record_state = False
self.root_win_folder = root_win_folder
self.root_mon_folder = root_mon_folder
self.date_folder = ""
self.perf_folder = ""
self.sub_folder = sub_folder
self.total_win_path = ""
self.total_mon_path = ""
self.camera.start()
def change_work_folder(self, date_folder, perf_folder):
#WIN FOLDER
partial_path = self.root_win_folder + date_folder + "/" + perf_folder
if os.path.exists(partial_path):
self.date_folder = date_folder
self.perf_folder = perf_folder
full_path = partial_path + self.sub_folder
if not os.path.exists(full_path):
os.mkdir(full_path)
self.total_win_path = full_path
self.get_last_video_number()
#MONITOR FOLDER
if not os.path.exists(self.root_mon_folder + date_folder):
os.mkdir(self.root_mon_folder + date_folder)
total_path = self.root_mon_folder + date_folder + "/" + perf_folder + "/"
if not os.path.exists(total_path):
os.mkdir(total_path)
self.total_mon_path = total_path
def get_last_video_number(self):
all_files = os.scandir(self.total_win_path)
large_n_main = -1
large_n_snippet = -1
for entry in all_files:
if entry.name[-5:] == "mjpeg" and int(entry.name[:-6]) >= large_n_main:
large_n_main = int(entry.name[:-6])
all_files = os.scandir(self.total_win_path)
# after finding the largest main, search for the largest snippet
if large_n_main != -1:
for entry in all_files:
if entry.name[-3:] == "avi" and int(entry.name[:-4].split("_")[1]) >= large_n_snippet:
large_n_snippet = int(entry.name[:-4].split("_")[1])
self.current_main = large_n_main
self.current_snippet = large_n_snippet
def select_encoder(self, encoder_type: str, quality: int):
if encoder_type == "jpeg":
encoder = JpegEncoder(q=quality)
elif encoder_type == "h264":
encoder = H264Encoder(q=quality)
return [encoder, encoder_type]
def init_perf(self):
if self.record_state == False and self.stream_state == False:
self.get_last_video_number()
self.record_main()
self.record_state = True
def end_perf(self):
if self.record_state == True:
self.stop_recording()
self.record_state = False
def record_main(self):
self.current_main += 1
print("Recording main", self.current_main)
output_name = self.total_win_path+str(self.current_main)+".mjpeg"
self.camera.start_encoder(self.encoder[0], output=output_name)
def record_snippet(self):
self.current_snippet += 1
self.lores_queue.put(str(self.current_main) + "_" + str(self.current_snippet))
self.lores_event.set()
self.record_state = True
def record_stream(self, encoder, output):
encoder.output = FileOutput(output)
self.camera.start_encoder(encoder)
def lores_write(self):
while not self.kill.is_set():
self.lores_event.wait()
video_name = self.lores_queue.get()
if video_name == "end_thread":
continue
print("lores", video_name)
video_name = self.total_mon_path + video_name + "_lores"+".avi"
video_out = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc(*'MJPG'), float(self.current_fps), LORES_RES)
while self.lores_event.is_set():
low_res_frame = self.camera.capture_array(name="lores")
low_res_frame = cv2.cvtColor(low_res_frame, cv2.COLOR_YUV420p2RGB)
video_out.write(low_res_frame)
video_out.release()
def stop_snippet(self):
self.lores_event.clear()
def stop_recording(self):
self.camera.stop_encoder()
def init_stream(self, stream_quality=75, stream_resolution=(1920,1080)):
#don't start if camera is already recording
if self.record_state == False:
output = StreamingOutput()
encoder = JpegEncoder(q=stream_quality)
self.record_stream(encoder=encoder, output=output)
address = ('', 8000)
#this stream handler hack is needed because "output" should be defined inside the do_GET method
stream_handler = StreamingHandler
stream_handler.define_output(stream_handler, output)
self.server = StreamingServer(address, stream_handler)
self.thread = Thread(None, self.server.run)
self.thread.start()
self.stream_state = True
print("Video stream is online!")
def close_stream(self):
self.server.shutdown()
self.stop_recording()
self.thread.join()
self.set_cam_param(temp_change=False, quality=self.main_quality)
self.stream_state = False
print("Stream offline.")
def get_cam_params(self):
print("\n\n-----//-----PARAMS CORRESPONDING TO VIDEO CONFIGURATION-----//-----\n\n",self.cam_config)
#caputure_metadata should only got if camera is recording
#print("\n\n-----//-----PARAMS CORRESPONDING TO CAMERA CONTROLS-----//-----\n\n",self.camera.capture_metadata())
return self.cam_config #self.camera.capture_metadata()
def set_cam_param(self, temp_change=True, fps=None, resolution=(-1,-1),
hflip = -1, exposure_time = -1, quality=-1, agc=-1, gain=-1, awb=-1):
if fps != None:
if fps == 1:
if self.current_fps < 30:
self.current_fps += 1
picam_fps = int((1/self.current_fps)*1e6)
if temp_change == False:
self.cam_config["controls"]["FrameDurationLimits"] = (picam_fps,picam_fps)
self.camera.set_controls({"FrameDurationLimits": (picam_fps,picam_fps)})
elif fps == -1:
if self.current_fps > 1:
self.current_fps -= 1
picam_fps = int((1/self.current_fps)*1e6)
if temp_change == False:
self.cam_config["controls"]["FrameDurationLimits"] = (picam_fps,picam_fps)
self.camera.set_controls({"FrameDurationLimits": (picam_fps,picam_fps)})
if resolution[0] != -1:
if temp_change == False:
self.main_resolution = resolution
self.cam_config["main"]["size"] = resolution
if self.camera.started:
self.camera.stop()
self.camera.configure(self.cam_config)
self.camera.start()
else:
self.camera.configure(self.cam_config)
if hflip != -1:
if hflip == True:
self.cam_config["transform"] = Transform(hflip=True)
if self.camera.started:
self.camera.stop()
self.camera.configure(self.cam_config)
self.camera.start()
else:
self.camera.configure(self.cam_config)
else:
self.cam_config["transform"] = Transform()
if self.camera.started:
self.camera.stop()
self.camera.configure(self.cam_config)
self.camera.start()
else:
self.camera.configure(self.cam_config)
if exposure_time != None:
if exposure_time == 1:
if self.current_exp < 10:
self.current_exp += 1
max_value = int((1/self.current_fps)*1e6)
exp_values = linspace(10000, max_value, num=11, dtype=int)
if temp_change == False:
self.cam_config["controls"]["ExposureTime"] = exp_values[self.current_exp]
self.camera.set_controls({"ExposureTime": exp_values[self.current_exp]})
elif exposure_time == -1:
if self.current_exp > 0:
self.current_exp -= 1
max_value = int((1/self.current_fps)*1e6)
exp_values = linspace(10000, max_value, num=11, dtype=int)
if temp_change == False:
self.cam_config["controls"]["ExposureTime"] = exp_values[self.current_exp]
self.camera.set_controls({"ExposureTime": exp_values[self.current_exp]})
if quality != -1:
if temp_change == False:
self.main_quality = quality
if self.encoder[1] == "jpeg":
self.encoder[0] = JpegEncoder(q=quality)
elif self.encoder[1] == "h264":
self.encoder[0] = H264Encoder(quality=quality)
if agc != -1:
if agc == 0:
self.cam_config["controls"]["AeEnable"] = False
self.camera.set_controls({"AeEnable": False})
else:
self.cam_config["controls"]["AeEnable"] = True
self.camera.set_controls({"AeEnable": True})
if gain != -1:
self.cam_config["controls"]["AnalogueGain"] = gain
self.camera.set_controls({"AnalogueGain": gain})
if awb != -1:
if awb == 1:
self.cam_config["controls"]["AwbEnable"] = True
self.camera.set_controls({"AwbEnable": True})
else:
self.cam_config["controls"]["AwbEnable"] = False
self.camera.set_controls({"AwbEnable": False})
def close_cam(self):
self.camera.stop()
#kill lores thread
self.kill.set()
self.lores_queue.put("end_thread")
#self.lores_queue_win.put("end_thread")
self.lores_event.set()
#self.lores_event_win.set()
#self.lores_thread.join()
#self.lores_thread_win.join()