9
9
import numpy as np
10
10
import ctypes
11
11
import gi
12
- gi .require_version ('Aravis' , '0.8' )
12
+
13
+ gi .require_version ("Aravis" , "0.8" )
13
14
from gi .repository import Aravis
14
15
15
16
__author__ = "Olivier Roulet-Dubonnet, Morten Lind"
16
17
__copyright__ = "Copyright 2011-2013, Sintef Raufoss Manufacturing"
17
18
__license__ = "GPLv3"
18
19
__version__ = "0.5"
19
20
21
+
20
22
class AravisException (Exception ):
21
23
pass
22
24
25
+
23
26
class Camera (object ):
24
27
"""
25
- Create a Camera object.
28
+ Create a Camera object.
26
29
name is the camera ID in aravis.
27
30
If name is None, the first found camera is used.
28
31
If no camera is found an AravisException is raised.
29
32
"""
33
+
30
34
def __init__ (self , name = None , loglevel = logging .WARNING ):
31
35
self .logger = logging .getLogger (self .__class__ .__name__ )
32
- if len (logging .root .handlers ) == 0 : # dirty hack
36
+ if len (logging .root .handlers ) == 0 : # dirty hack
33
37
logging .basicConfig ()
34
38
self .logger .setLevel (loglevel )
35
39
self .name = name
@@ -50,15 +54,17 @@ def __init__(self, name=None, loglevel=logging.WARNING):
50
54
self ._last_payload = 0
51
55
52
56
def __getattr__ (self , name ):
53
- if hasattr (self .cam , name ): # expose methods from the aravis camera object which is also relatively high level
57
+ if hasattr (
58
+ self .cam , name
59
+ ): # expose methods from the aravis camera object which is also relatively high level
54
60
return getattr (self .cam , name )
55
- #elif hasattr(self.dev, name): #epose methods from the aravis device object, this might be confusing
61
+ # elif hasattr(self.dev, name): #epose methods from the aravis device object, this might be confusing
56
62
# return getattr(self.dev, name)
57
63
else :
58
64
raise AttributeError (name )
59
65
60
66
def __dir__ (self ):
61
- tmp = list (self .__dict__ .keys ()) + self .cam .__dir__ ()# + self.dev.__dir__()
67
+ tmp = list (self .__dict__ .keys ()) + self .cam .__dir__ () # + self.dev.__dir__()
62
68
return tmp
63
69
64
70
def load_config (self , path ):
@@ -85,7 +91,9 @@ def get_feature_type(self, name):
85
91
genicam = self .dev .get_genicam ()
86
92
node = genicam .get_node (name )
87
93
if not node :
88
- raise AravisException ("Feature {} does not seem to exist in camera" .format (name ))
94
+ raise AravisException (
95
+ "Feature {} does not seem to exist in camera" .format (name )
96
+ )
89
97
return node .get_node_name ()
90
98
91
99
def get_feature (self , name ):
@@ -134,7 +142,9 @@ def get_feature_vals(self, name):
134
142
if ntype == "Enumeration" :
135
143
return self .dev .get_available_enumeration_feature_values_as_strings (name )
136
144
else :
137
- raise AravisException ("{} is not an enumeration but a {}" .format (name , ntype ))
145
+ raise AravisException (
146
+ "{} is not an enumeration but a {}" .format (name , ntype )
147
+ )
138
148
139
149
def read_register (self , address ):
140
150
return self .dev .read_register (address )
@@ -150,7 +160,9 @@ def create_buffers(self, nb=10, payload=None):
150
160
self .stream .push_buffer (Aravis .Buffer .new_allocate (payload ))
151
161
152
162
def pop_frame (self , timestamp = False ):
153
- while True : #loop in python in order to allow interrupt, have the loop in C might hang
163
+ while (
164
+ True
165
+ ): # loop in python in order to allow interrupt, have the loop in C might hang
154
166
if timestamp :
155
167
ts , frame = self .try_pop_frame (timestamp )
156
168
else :
@@ -186,7 +198,7 @@ def _array_from_buffer_address(self, buf):
186
198
if not buf :
187
199
return None
188
200
pixel_format = buf .get_image_pixel_format ()
189
- bits_per_pixel = pixel_format >> 16 & 0xff
201
+ bits_per_pixel = pixel_format >> 16 & 0xFF
190
202
if bits_per_pixel == 8 :
191
203
INTP = ctypes .POINTER (ctypes .c_uint8 )
192
204
else :
@@ -208,42 +220,41 @@ def __str__(self):
208
220
209
221
def __repr__ (self ):
210
222
return self .__str__ ()
211
-
223
+
212
224
def start_acquisition (self , nb_buffers = 10 ):
213
225
self .logger .info ("starting acquisition" )
214
226
payload = self .cam .get_payload ()
215
227
if payload != self ._last_payload :
216
- #FIXME should clear buffers
217
- self .create_buffers (nb_buffers , payload )
228
+ # FIXME should clear buffers
229
+ self .create_buffers (nb_buffers , payload )
218
230
self ._last_payload = payload
219
231
self .cam .start_acquisition ()
220
232
221
233
def start_acquisition_trigger (self , nb_buffers = 1 ):
222
- self .set_feature ("AcquisitionMode" , "Continuous" ) #no acquisition limits
223
- self .set_feature ("TriggerSource" , "Software" ) #wait for trigger t acquire image
224
- self .set_feature ("TriggerMode" , "On" ) #Not documented but necesary
234
+ self .set_feature ("AcquisitionMode" , "Continuous" ) # no acquisition limits
235
+ self .set_feature (
236
+ "TriggerSource" , "Software"
237
+ ) # wait for trigger t acquire image
238
+ self .set_feature ("TriggerMode" , "On" ) # Not documented but necesary
225
239
self .start_acquisition (nb_buffers )
226
240
227
241
def start_acquisition_continuous (self , nb_buffers = 20 ):
228
- self .set_feature ("AcquisitionMode" , "Continuous" ) # no acquisition limits
229
- #self.set_feature("TriggerSource", "Freerun") #as fast as possible
230
- #self.set_string_feature("TriggerSource", "FixedRate")
231
- #self.set_feature("TriggerMode", "On") #Not documented but necesary
242
+ self .set_feature ("AcquisitionMode" , "Continuous" ) # no acquisition limits
243
+ # self.set_feature("TriggerSource", "Freerun") #as fast as possible
244
+ # self.set_string_feature("TriggerSource", "FixedRate")
245
+ # self.set_feature("TriggerMode", "On") #Not documented but necesary
232
246
self .start_acquisition (nb_buffers )
233
247
234
248
def stop_acquisition (self ):
235
249
self .cam .stop_acquisition ()
236
250
237
251
def shutdown (self ):
238
- # Delete the objects on shutdown: socket will be closed!
252
+ # Delete the objects on shutdown: socket will be closed!
239
253
del self .stream
240
254
del self .dev
241
255
del self .cam
242
256
243
257
244
-
245
-
246
-
247
258
def get_device_ids ():
248
259
Aravis .update_device_list ()
249
260
n = Aravis .get_n_devices ()
@@ -252,60 +263,59 @@ def get_device_ids():
252
263
253
264
def show_frame (frame ):
254
265
import cv2
266
+
255
267
cv2 .imshow ("capture" , frame )
256
268
cv2 .waitKey (0 )
257
269
270
+
258
271
def save_frame (frame , path = "frame.png" ):
259
272
print ("Saving frame to " , path )
260
273
np .save (path , frame )
261
274
275
+
262
276
def sfn (cam , path = "frame.png" ):
263
277
from PIL import Image
278
+
264
279
cam .start_acquisition ()
265
280
frame = cam .pop_frame ()
266
281
cam .stop_acquisition ()
267
282
im = Image .fromarray (frame )
268
283
print ("Saving image to " , path )
269
284
im .save (path )
270
285
286
+
271
287
def get_frame (cam ):
272
288
cam .start_acquisition ()
273
289
frame = cam .pop_frame ()
274
290
cam .stop_acquisition ()
275
291
return frame
276
292
277
293
278
-
279
-
280
-
281
-
282
-
283
294
if __name__ == "__main__" :
284
- #cam = Camera("Prosilica-02-2110A-06145")
285
- #cam = Camera("AT-Automation Technology GmbH-20805103")
295
+ # cam = Camera("Prosilica-02-2110A-06145")
296
+ # cam = Camera("AT-Automation Technology GmbH-20805103")
286
297
cam = Camera (None )
287
298
try :
288
- #Aravis.enable_interface ("Fake")
289
- #x, y, width, height = cam.get_region()
299
+ # Aravis.enable_interface ("Fake")
300
+ # x, y, width, height = cam.get_region()
290
301
print ("Camera model: " , cam .get_model_name ())
291
302
print ("Vendor Name: " , cam .get_vendor_name ())
292
303
print ("Device id: " , cam .get_device_id ())
293
- #print("Image size: ", width, ",", height)
294
- print ("Sensor size: " , cam .get_sensor_size ())
304
+ # print("Image size: ", width, ",", height)
305
+ print ("Sensor size: " , cam .get_sensor_size ())
295
306
print ("Exposure: " , cam .get_exposure_time ())
296
307
print ("Frame rate: " , cam .get_frame_rate ())
297
308
print ("Payload: " , cam .get_payload ())
298
309
print ("AcquisitionMode: " , cam .get_feature ("AcquisitionMode" ))
299
310
print ("Acquisition vals: " , cam .get_feature_vals ("AcquisitionMode" ))
300
- #print("TriggerMode: ", cam.get_feature("TriggerMode"))
301
- #print("Bandwidth: ", cam.get_feature("StreamBytesPerSecond"))
311
+ # print("TriggerMode: ", cam.get_feature("TriggerMode"))
312
+ # print("Bandwidth: ", cam.get_feature("StreamBytesPerSecond"))
302
313
print ("PixelFormat: " , cam .get_feature ("PixelFormat" ))
303
- #print("ExposureAuto: ", cam.get_feature("ExposureAuto"))
314
+ # print("ExposureAuto: ", cam.get_feature("ExposureAuto"))
304
315
print ("PacketSize: " , cam .get_feature ("GevSCPSPacketSize" ))
305
316
306
-
307
317
from IPython import embed
318
+
308
319
embed ()
309
320
finally :
310
321
cam .shutdown ()
311
-
0 commit comments