|
18 | 18 | ap.add_argument("-f", "--force", action="store_true", help="force overwrite in folder") |
19 | 19 | ap.add_argument("--fps", type=int, default=20, help="set framerate") |
20 | 20 |
|
| 21 | +ap.add_argument("--openpose", action="store_true", help="name images for openpose (e.g. 000000000000_rendered, ..._1, ..._2, ...)") |
| 22 | + |
21 | 23 | args = vars(ap.parse_args()) |
22 | 24 |
|
| 25 | +save_for_openpose = args['openpose'] |
| 26 | + |
| 27 | +fps = args["fps"] |
| 28 | + |
23 | 29 | # make folder |
24 | 30 | target_folder = args['folder'] |
25 | 31 | if os.path.isdir(target_folder): |
26 | | - if args['force'] == False: |
| 32 | + if not args['force']: |
27 | 33 | print("{}: error: folder {} exists. Use --force to overwrite files.".format(os.path.basename(sys.argv[0]), target_folder)) |
28 | 34 | sys.exit() |
29 | 35 | else: |
30 | 36 | os.makedirs(target_folder) |
31 | 37 |
|
32 | 38 |
|
33 | 39 |
|
| 40 | +def set_fps(cam, fps): |
| 41 | + print("current acquisitionFrameRate", cam.AcquisitionFrameRate.GetValue() ) |
| 42 | + cam.AcquisitionFrameRateEnable.SetValue(True) |
| 43 | + print("acquisitionFrameRate Enable", cam.AcquisitionFrameRateEnable.GetValue() ) |
| 44 | + cam.AcquisitionFrameRate.SetValue(fps) |
| 45 | + print("acquisitionFrameRate set to", cam.AcquisitionFrameRate.GetValue() ) |
| 46 | + |
34 | 47 | def set_trigger_mode(cam, triggerSource): |
35 | 48 | cam.TriggerMode.SetValue(PySpin.TriggerMode_Off) |
36 | 49 | cam.TriggerSource.SetValue(triggerSource) |
@@ -78,7 +91,8 @@ def reset_trigger_mode_software(cam): |
78 | 91 | if cam_id == master_id: |
79 | 92 | print("master: {}".format(cam_id)) |
80 | 93 | master = cam |
81 | | - set_trigger_mode(cam, PySpin.TriggerSource_Software) |
| 94 | + #set_trigger_mode(cam, PySpin.TriggerSource_Software) |
| 95 | + set_fps(cam, fps) |
82 | 96 | else: |
83 | 97 | print("follower: {}".format(cam_id)) |
84 | 98 | set_trigger_mode(cam, PySpin.TriggerSource_Line3) |
@@ -141,74 +155,89 @@ def run(self): |
141 | 155 |
|
142 | 156 | count = 0 |
143 | 157 |
|
144 | | -worker = ImageWorker() |
145 | | -worker.start() |
146 | | - |
147 | | -fps = args["fps"] |
148 | | -fps_report_frequency = fps*2 |
149 | | - |
150 | | -start = last = time.time() |
151 | | -last_fps = 0 |
152 | | - |
153 | | -while 1: |
154 | | - if cv2.waitKey(1) != -1: |
155 | | - break |
| 158 | +workers = [] |
156 | 159 |
|
157 | | - # wait until |
158 | | - while time.time() < (start + 1.0/fps): |
159 | | - time.sleep(0.01) |
160 | | - |
161 | | - start = time.time() |
162 | | - last_fps += 1.0 / (start-last) |
163 | | - last = time.time() |
164 | | - |
165 | | - if count % fps_report_frequency == 0: |
166 | | - print(worker.images.qsize() ) |
167 | | - print("fps {0:.3f}".format( last_fps / fps_report_frequency)) |
168 | | - last_fps = 0 |
169 | | - |
170 | | - try: |
171 | | - master.TriggerSoftware.Execute() |
172 | | - count += 1 |
173 | | - except PySpin.SpinnakerException as ex: |
174 | | - print("Error: {}".format(ex)) |
175 | | - |
176 | | - for cam in cameras: |
177 | | - try: |
178 | | - i = cam.GetNextImage() |
179 | | - #print(i.GetWidth(), i.GetHeight(), i.GetBitsPerPixel()) |
180 | | - |
181 | | - if i.IsIncomplete(): |
182 | | - pass |
183 | | - else: |
184 | | - cam_id = cam.GetUniqueID() |
185 | | - filename = "captures/cam_{}__{}.jpg".format(cam_id, count) |
186 | | - worker.addImage( (filename, i) ) |
187 | | - i.Release() |
188 | | - del i |
189 | | - except PySpin.SpinnakerException as ex: |
190 | | - print("Error: {}".format(ex)) |
| 160 | +for i in range(len(cameras)): |
| 161 | + worker = ImageWorker() |
| 162 | + worker.start() |
| 163 | + workers.append(worker) |
191 | 164 |
|
| 165 | +count = 0 |
| 166 | +fps_report_frequency = fps*2 |
| 167 | +start_time = time.time() |
| 168 | + |
| 169 | +try: |
| 170 | + while 1: |
| 171 | + if cv2.waitKey(1) != -1: |
| 172 | + break |
| 173 | + |
| 174 | + if count % fps_report_frequency == 0: |
| 175 | + fps = fps_report_frequency / (time.time() - start_time) # / fps_report_frequency |
| 176 | + print("fps {:.3f} image count {}, in buffer {}".format(fps, count,workers[0].images.qsize() )) |
| 177 | + start_time = time.time() |
| 178 | + |
| 179 | + for n in range(len(cameras)): |
| 180 | + cam = cameras[n] |
| 181 | + try: |
| 182 | + i = cam.GetNextImage() |
| 183 | + #print(i.GetWidth(), i.GetHeight(), i.GetBitsPerPixel()) |
| 184 | + |
| 185 | + if i.IsIncomplete(): |
| 186 | + pass |
| 187 | + else: |
| 188 | + cam_id = cam.GetUniqueID() |
| 189 | + |
| 190 | + if save_for_openpose: |
| 191 | + if n == 0: |
| 192 | + filename = "{}/{:012}_rendered.jpg".format(target_folder, count) |
| 193 | + else: |
| 194 | + filename = "{}/{:012}_rendered_{}.jpg".format(target_folder, count, n) |
| 195 | + else: |
| 196 | + filename = "{}/cam_{}__{:06}.jpg".format(target_folder, cam_id, count) |
| 197 | + workers[n].addImage( (filename, i) ) |
| 198 | + |
| 199 | + if cam_id == master_id: |
| 200 | + count += 1 |
| 201 | + |
| 202 | + i.Release() |
| 203 | + del i |
| 204 | + except PySpin.SpinnakerException as ex: |
| 205 | + print("Error: {}".format(ex)) |
| 206 | +except KeyboardInterrupt: |
| 207 | + pass |
192 | 208 | # |
193 | 209 | # cleanup |
194 | 210 | # |
195 | 211 |
|
196 | | -worker.stop() |
| 212 | +for w in workers: |
| 213 | + w.stop() |
197 | 214 |
|
198 | 215 | while(1): |
199 | | - print("{} images to be processed. waiting for thread to finish".format(worker.images.qsize())) |
| 216 | + report = "" |
| 217 | + for w in workers: |
| 218 | + report = report + " {}".format(w.images.qsize()) |
| 219 | + |
| 220 | + print("{} images to be processed. waiting for thread to finish".format(report)) |
200 | 221 | time.sleep(0.5) |
201 | | - if worker.images.empty(): |
| 222 | + |
| 223 | + done = True |
| 224 | + for w in workers: |
| 225 | + if not w.images.empty(): |
| 226 | + done = False |
| 227 | + if done: |
202 | 228 | break |
203 | 229 |
|
| 230 | + |
| 231 | +master = None |
| 232 | + |
204 | 233 | for cam in cameras: |
205 | 234 | cam.EndAcquisition() |
206 | 235 | reset_trigger_mode_software(cam) |
207 | 236 | cam.DeInit() |
208 | 237 | del cam |
| 238 | + |
209 | 239 | del cameras |
210 | 240 | del cam_list |
211 | 241 |
|
212 | 242 | system.ReleaseInstance() |
213 | 243 | del system |
214 | | - |
|
0 commit comments