Skip to content

Commit d9c6be4

Browse files
committed
64 frames a second capture with 3 cameras
ok ok. it takes a bit of time for the worker threads to finish, but this is more like it.
1 parent dd0ad89 commit d9c6be4

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
import sys
2+
import time
3+
import PySpin
4+
import os
5+
6+
import numpy as np
7+
import cv2
8+
9+
from queue import Queue
10+
import threading
11+
12+
import argparse
13+
14+
# construct the argument parse and parse the arguments
15+
ap = argparse.ArgumentParser()
16+
ap.add_argument("-c", "--camera", type=str, default="0", help="camera by id")
17+
18+
args = vars(ap.parse_args())
19+
20+
def set_trigger_mode(cam, triggerSource):
21+
cam.TriggerMode.SetValue(PySpin.TriggerMode_Off)
22+
cam.TriggerSource.SetValue(triggerSource)
23+
cam.TriggerMode.SetValue(PySpin.TriggerMode_On)
24+
25+
def reset_trigger_mode_software(cam):
26+
cam.TriggerMode.SetValue(PySpin.TriggerMode_Off)
27+
print("reset trigger mode")
28+
29+
30+
def reset_trigger_mode_software(cam):
31+
cam.TriggerMode.SetValue(PySpin.TriggerMode_Off)
32+
print("reset trigger mode")
33+
34+
35+
#
36+
# setup
37+
#
38+
39+
master_id = "18284509"
40+
master = None
41+
42+
43+
system = PySpin.System.GetInstance()
44+
cam_list = system.GetCameras()
45+
46+
if cam_list.GetSize() == 0:
47+
print("no cameras found, aborting")
48+
system.ReleaseInstance()
49+
del system
50+
sys.exit()
51+
52+
for i in range(cam_list.GetSize()):
53+
cam = cam_list.GetByIndex(i)
54+
print("camera {} serial: {}".format(i, cam.GetUniqueID()))
55+
56+
57+
58+
cameras = []
59+
for i in range(cam_list.GetSize()):
60+
try:
61+
cam = cam_list.GetByIndex(i)
62+
cam_id = cam.GetUniqueID()
63+
cam.Init()
64+
65+
if cam_id == master_id:
66+
print("master: {}".format(cam_id))
67+
master = cam
68+
set_trigger_mode(cam, PySpin.TriggerSource_Software)
69+
else:
70+
print("follower: {}".format(cam_id))
71+
set_trigger_mode(cam, PySpin.TriggerSource_Line3)
72+
73+
cam.AcquisitionMode.SetValue(PySpin.AcquisitionMode_Continuous)
74+
cam.BeginAcquisition()
75+
cameras.append(cam)
76+
except PySpin.SpinnakerException as ex:
77+
print("Error: {}".format(ex))
78+
sys.exit()
79+
80+
81+
class ImageWorker(threading.Thread):
82+
def __init__(self):
83+
super(ImageWorker, self).__init__()
84+
self.images = Queue()
85+
self._stop_event = threading.Event()
86+
87+
def addImage(self, img):
88+
self.images.put(img)
89+
def stop(self):
90+
self._stop_event.set()
91+
def stopped(self):
92+
return self._stop_event.is_set()
93+
def run(self):
94+
print("run")
95+
while(1):
96+
if self.images.empty():
97+
if self.stopped():
98+
break
99+
else:
100+
time.sleep(0.10)
101+
else:
102+
item = self.images.get()
103+
104+
if item != None:
105+
filename, i = item
106+
107+
#i.save(filename)
108+
109+
image_converted = i.Convert(PySpin.PixelFormat_BGR8, PySpin.DIRECTIONAL_FILTER)
110+
111+
#writing with spinaker
112+
113+
image_converted.Save(filename)
114+
115+
#writing with opencv
116+
117+
#image_data = image_converted.GetData()
118+
#cvi = np.frombuffer(image_data, dtype=np.uint8)
119+
#cvi = cvi.reshape((i.GetHeight(),i.GetWidth(),3))
120+
#cv2.imwrite(filename, cvi)
121+
122+
123+
124+
125+
#
126+
# loop
127+
#
128+
129+
count = 0
130+
elapsed = 0
131+
132+
#os.makedirs("captures", exist_ok=True)
133+
134+
worker = ImageWorker()
135+
worker.start()
136+
137+
138+
blank_image = np.zeros((300,400,3), np.uint8)
139+
cv2.imshow("blank", blank_image)
140+
141+
print("start capturing")
142+
143+
while 1:
144+
key = cv2.waitKey(1)
145+
146+
start = time.time()
147+
148+
149+
150+
if key == 27: # ESC
151+
cv2.destroyAllWindows()
152+
break
153+
154+
155+
try:
156+
master.TriggerSoftware.Execute()
157+
count += 1
158+
except PySpin.SpinnakerException as ex:
159+
print("Error: {}".format(ex))
160+
161+
for cam in cameras:
162+
try:
163+
i = cam.GetNextImage()
164+
#print(i.GetWidth(), i.GetHeight(), i.GetBitsPerPixel())
165+
166+
if i.IsIncomplete():
167+
pass
168+
else:
169+
cam_id = cam.GetUniqueID()
170+
filename = "captures/cam_{}__{}.jpg".format(cam_id, count)
171+
worker.addImage( (filename, i) )
172+
# see documentation: enum ColorProcessingAlgorithm
173+
#image_converted = i.Convert(PySpin.PixelFormat_BGR8, PySpin.DIRECTIONAL_FILTER)
174+
#image_data = image_converted.GetData()
175+
#cvi = np.frombuffer(image_data, dtype=np.uint8)
176+
#cvi = cvi.reshape((i.GetHeight(),i.GetWidth(),3))
177+
#frame[cam_id] = cvi
178+
179+
#res = cv2.resize(cvi, (int(1280/size),int(1024/size)))
180+
#cv2.imshow("cam{}".format(cam_id),res)
181+
i.Release()
182+
del i
183+
except PySpin.SpinnakerException as ex:
184+
print("Error: {}".format(ex))
185+
186+
187+
188+
end = time.time()
189+
elapsed += end - start
190+
191+
if count % 10 == 0:
192+
print(worker.images.qsize() )
193+
print("fps {0:.3f}".format(10 /elapsed))
194+
195+
elapsed = 0
196+
197+
#
198+
# cleanup
199+
#
200+
201+
worker.stop()
202+
203+
while(1):
204+
print("{} images to be processed. waiting for thread to finish".format(worker.images.qsize()))
205+
time.sleep(0.5)
206+
if worker.images.empty():
207+
break
208+
209+
for cam in cameras:
210+
cam.EndAcquisition()
211+
reset_trigger_mode_software(cam)
212+
cam.DeInit()
213+
del cam
214+
del cameras
215+
del cam_list
216+
217+
system.ReleaseInstance()
218+
del system
219+

kinect-v2/README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ These examples use pyKinect2 to interface with the Kinect API, and only work on
77
Installation
88
------------
99

10+
Install the kinect v2 SDK https://www.microsoft.com/en-us/download/details.aspx?id=44561
1011
Install python, opencv and contrib (see top level readme) and install pyKinect2. <https://github.com/Kinect/pyKinect2>
1112

1213
note: (october 5 2018: if you install with pip, you need to overwrite the files with the ones from github....)

0 commit comments

Comments
 (0)