-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiCameraOpenCV.py
92 lines (66 loc) · 2.59 KB
/
PiCameraOpenCV.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
from picamera2 import Picamera2 # required for camera module v3
import cv2 as cv
class PiCameraOpenCV():
def __init__(self,camera_settings):
self.controls = None
self.is_open = True
self.active_config = camera_settings['Camera_Active_Config']
try:
self.cap = Picamera2()
main = self._configure_res_mode(camera_settings,'main')
lores = self._configure_res_mode(camera_settings,'lores')
raw = self._configure_res_mode(camera_settings,'raw')
self.config = self.cap.create_video_configuration(raw=raw,main=main,lores=lores)
#request optimal image size
self.cap.align_configuration(self.config)
self.cap.configure(self.config)
if 'Camera_Controls' in camera_settings:
self.controls = camera_settings["Camera_Controls"]
for control in self.controls:
self.cap.set_controls(control)
self.cap.start()
except:
self.is_open = False
return
def grab_meta_data(self):
return self.cap.capture_metadata();
#private method
def _configure_res_mode(self,camera_settings,mode):
mode_setting = {}
if mode in camera_settings:
if "Horz_Res" in camera_settings[mode]:
if camera_settings[mode]["Horz_Res"] != 0:
mode_setting['size'] = (camera_settings[mode]["Horz_Res"],camera_settings[mode]["Vert_Res"])
if "fmt" in camera_settings[mode]:
mode_setting['format'] = camera_settings[mode]["fmt"]
return mode_setting
def get_mode(self):
return self.active_config
# 'main' or 'lores'
def set_mode(self,main_or_lores):
self.active_config = main_or_lores
return
def read(self):
dst = None
if self.is_open:
dst = self.cap.capture_array(self.active_config)
if(self.active_config.find('lores')!=-1):
dst=cv.cvtColor(dst, cv.COLOR_YUV2BGR_I420)
return self.is_open,dst
def isOpened(self):
return self.is_open
def release(self):
if self.is_open:
self.cap.close()
self.is_open = False
return
# purely for compatibility at the moment
def set(self,var,val):
if var == cv.CAP_PROP_FRAME_WIDTH:
# do nothing
pass
elif var==cv.CAP_PROP_FRAME_HEIGHT:
pass
elif var == cv.CAP_PROP_FPS:
pass
return