-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflir_capture_multiple_video.py
177 lines (143 loc) · 4.42 KB
/
flir_capture_multiple_video.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
import sys
import time
import PySpin
import numpy as np
import cv2
import SaveToAvi
def set_trigger_mode(cam, triggerSource):
cam.TriggerMode.SetValue(PySpin.TriggerMode_Off)
cam.TriggerSource.SetValue(triggerSource)
cam.TriggerMode.SetValue(PySpin.TriggerMode_On)
def reset_trigger_mode_software(cam):
cam.TriggerMode.SetValue(PySpin.TriggerMode_Off)
print("reset trigger mode")
def mode_status():
if sw_vid:
print("record video")
elif not sw_vid:
if sw_con:
print("record continuous photo")
else:
print("record single photo")
#
# setup
#
system = PySpin.System.GetInstance()
cam_list = system.GetCameras()
master_id = "18284509"
master = None
print("number of cameras {}".format(cam_list.GetSize()))
if cam_list.GetSize() == 0:
print("no cameras found, aborting")
system.ReleaseInstance()
del system
sys.exit()
cameras = []
for i in range(cam_list.GetSize()):
try:
cam = cam_list.GetByIndex(i)
cam_id = cam.GetUniqueID()
cam.Init()
if cam_id == master_id:
print("master: {}".format(cam_id))
master = cam
set_trigger_mode(cam, PySpin.TriggerSource_Software)
else:
print("follower: {}".format(cam_id))
set_trigger_mode(cam, PySpin.TriggerSource_Line3)
cam.AcquisitionMode.SetValue(PySpin.AcquisitionMode_Continuous)
cam.BeginAcquisition()
cameras.append(cam)
except PySpin.SpinnakerException as ex:
print("Error: {}".format(ex))
#
# loop
#
count = 0
sw_rec = False
sw_con = False
sw_vid = False
size = 4
frame = {}
from path import Path
directory = Path("./")
camera_dict = { "18284509": "", "18284511": "_1", "18284512": "_2"}
print("start loop")
while 1:
key = cv2.waitKey(1)
if key == 27: # ESC
cv2.destroyAllWindows()
break
elif key == 32: # SPACE
if sw_con:
sw_rec = not sw_rec
if sw_rec:
print("start recording")
elif not sw_rec:
print("finish recording")
elif not sw_vid:
print("take picture:{}".format(count))
for key, value in frame.items():
cv2.imwrite("frame_{:012}{}.jpg".format(count,camera_dict[key]), value)
count = count + 1
elif key == 99: # C
sw_con = not sw_con
mode_status()
elif key == 114: # R
count = 0
print("reset count")
files = directory.walkfiles("*.jpg")
files = directory.walkfiles("*.avi")
for file in files:
file.remove()
print("reset .jpg .avi")
elif key == 118: # V
sw_vid = not sw_vid
mode_status()
try:
master.TriggerSoftware.Execute()
except PySpin.SpinnakerException as ex:
print("Error: {}".format(ex))
for cam in cameras:
try:
i = cam.GetNextImage()
#print(i.GetWidth(), i.GetHeight(), i.GetBitsPerPixel())
if i.IsIncomplete():
pass
else:
cam_id = cam.GetUniqueID()
# see documentation: enum ColorProcessingAlgorithm
image_converted = i.Convert(PySpin.PixelFormat_BGR8, PySpin.DIRECTIONAL_FILTER)
image_data = image_converted.GetData()
cvi = np.frombuffer(image_data, dtype=np.uint8)
cvi = cvi.reshape((i.GetHeight(),i.GetWidth(),3))
frame[cam_id] = cvi
if sw_vid:
print("recording video")
#..............................................
if not sw_rec:
res = cv2.resize(cvi, (int(1280/size),int(1024/size)))
cv2.imshow("cam{}".format(cam_id),res)
if sw_rec:
if not sw_vid:
print("take picture:{}".format(count))
for key, value in frame.items():
cv2.imwrite("frame_{:012}{}.jpg".format(count,camera_dict[key]), value)
count = count + 1
i.Release()
del i
except PySpin.SpinnakerException as ex:
print("Error: {}".format(ex))
#
# cleanup
#
del master
for cam in cameras:
cam.EndAcquisition()
reset_trigger_mode_software(cam)
cam.DeInit()
del cam
del cameras
del cam_list
system.ReleaseInstance()
del system