AVT相机arm版本SDK
This commit is contained in:
163
Vimba_6_0/VimbaPython/Examples/action_commands.py
Normal file
163
Vimba_6_0/VimbaPython/Examples/action_commands.py
Normal file
@@ -0,0 +1,163 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from typing import Tuple
|
||||
from vimba import *
|
||||
|
||||
|
||||
def print_preamble():
|
||||
print('/////////////////////////////////////////')
|
||||
print('/// Vimba API Action Commands Example ///')
|
||||
print('/////////////////////////////////////////\n')
|
||||
|
||||
|
||||
def print_usage():
|
||||
print('Usage:')
|
||||
print(' python action_commands.py <camera_id> <interface_id>')
|
||||
print(' python action_commands.py [/h] [-h]')
|
||||
print()
|
||||
print('Parameters:')
|
||||
print(' camera_id ID of the camera to be used')
|
||||
print(' interface_id ID of network interface to send out Action Command')
|
||||
print(' \'ALL\' enables broadcast on all interfaces')
|
||||
print()
|
||||
|
||||
|
||||
def abort(reason: str, return_code: int = 1, usage: bool = False):
|
||||
print(reason + '\n')
|
||||
|
||||
if usage:
|
||||
print_usage()
|
||||
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
def parse_args() -> Tuple[str, str]:
|
||||
args = sys.argv[1:]
|
||||
|
||||
for arg in args:
|
||||
if arg in ('/h', '-h'):
|
||||
print_usage()
|
||||
sys.exit(0)
|
||||
|
||||
if len(args) != 2:
|
||||
abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True)
|
||||
|
||||
return (args[0], args[1])
|
||||
|
||||
|
||||
def get_input() -> str:
|
||||
prompt = 'Press \'a\' to send action command. Press \'q\' to stop example. Enter:'
|
||||
print(prompt, flush=True)
|
||||
return input()
|
||||
|
||||
|
||||
def get_camera(camera_id: str) -> Camera:
|
||||
with Vimba.get_instance() as vimba:
|
||||
try:
|
||||
return vimba.get_camera_by_id(camera_id)
|
||||
|
||||
except VimbaCameraError:
|
||||
abort('Failed to access Camera {}. Abort.'.format(camera_id))
|
||||
|
||||
|
||||
def get_command_sender(interface_id):
|
||||
# If given interface_id is ALL, ActionCommand shall be sent from all Ethernet Interfaces.
|
||||
# This is achieved by run ActionCommand on the Vimba instance.
|
||||
if interface_id == 'ALL':
|
||||
return Vimba.get_instance()
|
||||
|
||||
with Vimba.get_instance() as vimba:
|
||||
# A specific Interface was given. Lookup via given Interface id and verify that
|
||||
# it is an Ethernet Interface. Running ActionCommand will be only send from this Interface.
|
||||
try:
|
||||
inter = vimba.get_interface_by_id(interface_id)
|
||||
|
||||
except VimbaInterfaceError:
|
||||
abort('Failed to access Interface {}. Abort.'.format(interface_id))
|
||||
|
||||
if inter.get_type() != InterfaceType.Ethernet:
|
||||
abort('Given Interface {} is no Ethernet Interface. Abort.'.format(interface_id))
|
||||
|
||||
return inter
|
||||
|
||||
|
||||
def frame_handler(cam: Camera, frame: Frame):
|
||||
if frame.get_status() == FrameStatus.Complete:
|
||||
print('Frame(ID: {}) has been received.'.format(frame.get_id()), flush=True)
|
||||
|
||||
cam.queue_frame(frame)
|
||||
|
||||
|
||||
def main():
|
||||
print_preamble()
|
||||
camera_id, interface_id = parse_args()
|
||||
|
||||
with Vimba.get_instance():
|
||||
cam = get_camera(camera_id)
|
||||
sender = get_command_sender(interface_id)
|
||||
|
||||
with cam, sender:
|
||||
# Prepare Camera for ActionCommand - Trigger
|
||||
device_key = 1
|
||||
group_key = 1
|
||||
group_mask = 1
|
||||
|
||||
cam.GVSPAdjustPacketSize.run()
|
||||
while not cam.GVSPAdjustPacketSize.is_done():
|
||||
pass
|
||||
|
||||
cam.TriggerSelector.set('FrameStart')
|
||||
cam.TriggerSource.set('Action0')
|
||||
cam.TriggerMode.set('On')
|
||||
cam.ActionDeviceKey.set(device_key)
|
||||
cam.ActionGroupKey.set(group_key)
|
||||
cam.ActionGroupMask.set(group_mask)
|
||||
|
||||
# Enter Streaming mode and wait for user input.
|
||||
try:
|
||||
cam.start_streaming(frame_handler)
|
||||
|
||||
while True:
|
||||
ch = get_input()
|
||||
|
||||
if ch == 'q':
|
||||
break
|
||||
|
||||
elif ch == 'a':
|
||||
sender.ActionDeviceKey.set(device_key)
|
||||
sender.ActionGroupKey.set(group_key)
|
||||
sender.ActionGroupMask.set(group_mask)
|
||||
sender.ActionCommand.run()
|
||||
|
||||
finally:
|
||||
cam.stop_streaming()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
136
Vimba_6_0/VimbaPython/Examples/asynchronous_grab.py
Normal file
136
Vimba_6_0/VimbaPython/Examples/asynchronous_grab.py
Normal file
@@ -0,0 +1,136 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from typing import Optional, Tuple
|
||||
from vimba import *
|
||||
|
||||
|
||||
def print_preamble():
|
||||
print('///////////////////////////////////////////')
|
||||
print('/// Vimba API Asynchronous Grab Example ///')
|
||||
print('///////////////////////////////////////////\n')
|
||||
|
||||
|
||||
def print_usage():
|
||||
print('Usage:')
|
||||
print(' python asynchronous_grab.py [/x] [-x] [camera_id]')
|
||||
print(' python asynchronous_grab.py [/h] [-h]')
|
||||
print()
|
||||
print('Parameters:')
|
||||
print(' /x, -x If set, use AllocAndAnnounce mode of buffer allocation')
|
||||
print(' camera_id ID of the camera to use (using first camera if not specified)')
|
||||
print()
|
||||
|
||||
|
||||
def abort(reason: str, return_code: int = 1, usage: bool = False):
|
||||
print(reason + '\n')
|
||||
|
||||
if usage:
|
||||
print_usage()
|
||||
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
def parse_args() -> Tuple[Optional[str], AllocationMode]:
|
||||
args = sys.argv[1:]
|
||||
argc = len(args)
|
||||
|
||||
allocation_mode = AllocationMode.AnnounceFrame
|
||||
cam_id = ""
|
||||
for arg in args:
|
||||
if arg in ('/h', '-h'):
|
||||
print_usage()
|
||||
sys.exit(0)
|
||||
elif arg in ('/x', '-x'):
|
||||
allocation_mode = AllocationMode.AllocAndAnnounceFrame
|
||||
elif not cam_id:
|
||||
cam_id = arg
|
||||
|
||||
if argc > 2:
|
||||
abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True)
|
||||
|
||||
return (cam_id if cam_id else None, allocation_mode)
|
||||
|
||||
|
||||
def get_camera(camera_id: Optional[str]) -> Camera:
|
||||
with Vimba.get_instance() as vimba:
|
||||
if camera_id:
|
||||
try:
|
||||
return vimba.get_camera_by_id(camera_id)
|
||||
|
||||
except VimbaCameraError:
|
||||
abort('Failed to access Camera \'{}\'. Abort.'.format(camera_id))
|
||||
|
||||
else:
|
||||
cams = vimba.get_all_cameras()
|
||||
if not cams:
|
||||
abort('No Cameras accessible. Abort.')
|
||||
|
||||
return cams[0]
|
||||
|
||||
|
||||
def setup_camera(cam: Camera):
|
||||
with cam:
|
||||
# Try to adjust GeV packet size. This Feature is only available for GigE - Cameras.
|
||||
try:
|
||||
cam.GVSPAdjustPacketSize.run()
|
||||
|
||||
while not cam.GVSPAdjustPacketSize.is_done():
|
||||
pass
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
pass
|
||||
|
||||
|
||||
def frame_handler(cam: Camera, frame: Frame):
|
||||
print('{} acquired {}'.format(cam, frame), flush=True)
|
||||
|
||||
cam.queue_frame(frame)
|
||||
|
||||
|
||||
def main():
|
||||
print_preamble()
|
||||
cam_id, allocation_mode = parse_args()
|
||||
|
||||
with Vimba.get_instance():
|
||||
with get_camera(cam_id) as cam:
|
||||
|
||||
setup_camera(cam)
|
||||
print('Press <enter> to stop Frame acquisition.')
|
||||
|
||||
try:
|
||||
# Start Streaming with a custom a buffer of 10 Frames (defaults to 5)
|
||||
cam.start_streaming(handler=frame_handler, buffer_count=10, allocation_mode=allocation_mode)
|
||||
input()
|
||||
|
||||
finally:
|
||||
cam.stop_streaming()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
177
Vimba_6_0/VimbaPython/Examples/asynchronous_grab_opencv.py
Normal file
177
Vimba_6_0/VimbaPython/Examples/asynchronous_grab_opencv.py
Normal file
@@ -0,0 +1,177 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
import threading
|
||||
import sys
|
||||
import cv2
|
||||
from typing import Optional
|
||||
from vimba import *
|
||||
|
||||
|
||||
def print_preamble():
|
||||
print('///////////////////////////////////////////////////////')
|
||||
print('/// Vimba API Asynchronous Grab with OpenCV Example ///')
|
||||
print('///////////////////////////////////////////////////////\n')
|
||||
|
||||
|
||||
def print_usage():
|
||||
print('Usage:')
|
||||
print(' python asynchronous_grab_opencv.py [camera_id]')
|
||||
print(' python asynchronous_grab_opencv.py [/h] [-h]')
|
||||
print()
|
||||
print('Parameters:')
|
||||
print(' camera_id ID of the camera to use (using first camera if not specified)')
|
||||
print()
|
||||
|
||||
|
||||
def abort(reason: str, return_code: int = 1, usage: bool = False):
|
||||
print(reason + '\n')
|
||||
|
||||
if usage:
|
||||
print_usage()
|
||||
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
def parse_args() -> Optional[str]:
|
||||
args = sys.argv[1:]
|
||||
argc = len(args)
|
||||
|
||||
for arg in args:
|
||||
if arg in ('/h', '-h'):
|
||||
print_usage()
|
||||
sys.exit(0)
|
||||
|
||||
if argc > 1:
|
||||
abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True)
|
||||
|
||||
return None if argc == 0 else args[0]
|
||||
|
||||
|
||||
def get_camera(camera_id: Optional[str]) -> Camera:
|
||||
with Vimba.get_instance() as vimba:
|
||||
if camera_id:
|
||||
try:
|
||||
return vimba.get_camera_by_id(camera_id)
|
||||
|
||||
except VimbaCameraError:
|
||||
abort('Failed to access Camera \'{}\'. Abort.'.format(camera_id))
|
||||
|
||||
else:
|
||||
cams = vimba.get_all_cameras()
|
||||
if not cams:
|
||||
abort('No Cameras accessible. Abort.')
|
||||
|
||||
return cams[0]
|
||||
|
||||
|
||||
def setup_camera(cam: Camera):
|
||||
with cam:
|
||||
# Enable auto exposure time setting if camera supports it
|
||||
try:
|
||||
cam.ExposureAuto.set('Continuous')
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
pass
|
||||
|
||||
# Enable white balancing if camera supports it
|
||||
try:
|
||||
cam.BalanceWhiteAuto.set('Continuous')
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
pass
|
||||
|
||||
# Try to adjust GeV packet size. This Feature is only available for GigE - Cameras.
|
||||
try:
|
||||
cam.GVSPAdjustPacketSize.run()
|
||||
|
||||
while not cam.GVSPAdjustPacketSize.is_done():
|
||||
pass
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
pass
|
||||
|
||||
# Query available, open_cv compatible pixel formats
|
||||
# prefer color formats over monochrome formats
|
||||
cv_fmts = intersect_pixel_formats(cam.get_pixel_formats(), OPENCV_PIXEL_FORMATS)
|
||||
color_fmts = intersect_pixel_formats(cv_fmts, COLOR_PIXEL_FORMATS)
|
||||
|
||||
if color_fmts:
|
||||
cam.set_pixel_format(color_fmts[0])
|
||||
|
||||
else:
|
||||
mono_fmts = intersect_pixel_formats(cv_fmts, MONO_PIXEL_FORMATS)
|
||||
|
||||
if mono_fmts:
|
||||
cam.set_pixel_format(mono_fmts[0])
|
||||
|
||||
else:
|
||||
abort('Camera does not support a OpenCV compatible format natively. Abort.')
|
||||
|
||||
|
||||
class Handler:
|
||||
def __init__(self):
|
||||
self.shutdown_event = threading.Event()
|
||||
|
||||
def __call__(self, cam: Camera, frame: Frame):
|
||||
ENTER_KEY_CODE = 13
|
||||
|
||||
key = cv2.waitKey(1)
|
||||
if key == ENTER_KEY_CODE:
|
||||
self.shutdown_event.set()
|
||||
return
|
||||
|
||||
elif frame.get_status() == FrameStatus.Complete:
|
||||
print('{} acquired {}'.format(cam, frame), flush=True)
|
||||
|
||||
msg = 'Stream from \'{}\'. Press <Enter> to stop stream.'
|
||||
cv2.imshow(msg.format(cam.get_name()), frame.as_opencv_image())
|
||||
|
||||
cam.queue_frame(frame)
|
||||
|
||||
|
||||
def main():
|
||||
print_preamble()
|
||||
cam_id = parse_args()
|
||||
|
||||
with Vimba.get_instance():
|
||||
with get_camera(cam_id) as cam:
|
||||
|
||||
# Start Streaming, wait for five seconds, stop streaming
|
||||
setup_camera(cam)
|
||||
handler = Handler()
|
||||
|
||||
try:
|
||||
# Start Streaming with a custom a buffer of 10 Frames (defaults to 5)
|
||||
cam.start_streaming(handler=handler, buffer_count=10)
|
||||
handler.shutdown_event.wait()
|
||||
|
||||
finally:
|
||||
cam.stop_streaming()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
51
Vimba_6_0/VimbaPython/Examples/create_trace_log.py
Normal file
51
Vimba_6_0/VimbaPython/Examples/create_trace_log.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
|
||||
|
||||
from vimba import *
|
||||
|
||||
|
||||
def main():
|
||||
print('////////////////////////////////////////////')
|
||||
print('/// VimbaPython Create Trace Log Example ///')
|
||||
print('////////////////////////////////////////////\n')
|
||||
|
||||
# Enable logging mechanism, creating a trace log. The log file is
|
||||
# stored at the location this script was executed from.
|
||||
vimba = Vimba.get_instance()
|
||||
vimba.enable_log(LOG_CONFIG_TRACE_FILE_ONLY)
|
||||
|
||||
# While entering this scope, feature, camera and interface discovery occurs.
|
||||
# All function calls to VimbaC are captured in the log file.
|
||||
with vimba:
|
||||
pass
|
||||
|
||||
vimba.disable_log()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
147
Vimba_6_0/VimbaPython/Examples/event_handling.py
Normal file
147
Vimba_6_0/VimbaPython/Examples/event_handling.py
Normal file
@@ -0,0 +1,147 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from typing import Optional
|
||||
from vimba import *
|
||||
|
||||
|
||||
def print_preamble():
|
||||
print('////////////////////////////////////////')
|
||||
print('/// Vimba API Event Handling Example ///')
|
||||
print('////////////////////////////////////////\n')
|
||||
|
||||
|
||||
def print_usage():
|
||||
print('Usage:')
|
||||
print(' python event_handling.py [camera_id]')
|
||||
print(' python event_handling.py [/h] [-h]')
|
||||
print()
|
||||
print('Parameters:')
|
||||
print(' camera_id ID of the camera to use (using first camera if not specified)')
|
||||
print()
|
||||
|
||||
|
||||
def abort(reason: str, return_code: int = 1, usage: bool = False):
|
||||
print(reason + '\n')
|
||||
|
||||
if usage:
|
||||
print_usage()
|
||||
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
def parse_args() -> Optional[str]:
|
||||
args = sys.argv[1:]
|
||||
argc = len(args)
|
||||
|
||||
for arg in args:
|
||||
if arg in ('/h', '-h'):
|
||||
print_usage()
|
||||
sys.exit(0)
|
||||
|
||||
if argc > 1:
|
||||
abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True)
|
||||
|
||||
return args[0] if argc == 1 else None
|
||||
|
||||
|
||||
def get_camera(cam_id: Optional[str]):
|
||||
with Vimba.get_instance() as vimba:
|
||||
# Lookup Camera if it was specified.
|
||||
if cam_id:
|
||||
try:
|
||||
cam = vimba.get_camera_by_id(cam_id)
|
||||
|
||||
except VimbaCameraError:
|
||||
abort('Failed to access Camera {}. Abort.'.format(cam_id))
|
||||
|
||||
# If no camera was specified, use first detected camera.
|
||||
else:
|
||||
cams = vimba.get_all_cameras()
|
||||
if not cams:
|
||||
abort('No Camera detected. Abort.')
|
||||
|
||||
cam = cams[0]
|
||||
|
||||
# This example works only with GigE Cameras. Verify that Camera is connected to an
|
||||
# Ethernet Interface.
|
||||
inter = vimba.get_interface_by_id(cam.get_interface_id())
|
||||
|
||||
if inter.get_type() != InterfaceType.Ethernet:
|
||||
abort('Example supports only GigE Cameras. Abort.')
|
||||
|
||||
return cam
|
||||
|
||||
|
||||
def setup_camera(cam: Camera):
|
||||
with cam:
|
||||
# Try to adjust GeV packet size. This Feature is only available for GigE - Cameras.
|
||||
try:
|
||||
cam.GVSPAdjustPacketSize.run()
|
||||
|
||||
while not cam.GVSPAdjustPacketSize.is_done():
|
||||
pass
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
pass
|
||||
|
||||
|
||||
def feature_changed_handler(feature):
|
||||
msg = 'Feature \'{}\' changed value to \'{}\''
|
||||
print(msg.format(str(feature.get_name()), str(feature.get())), flush=True)
|
||||
|
||||
|
||||
def main():
|
||||
print_preamble()
|
||||
cam_id = parse_args()
|
||||
|
||||
with Vimba.get_instance():
|
||||
with get_camera(cam_id) as cam:
|
||||
setup_camera(cam)
|
||||
|
||||
# Disable all events notifications
|
||||
for event in cam.EventSelector.get_available_entries():
|
||||
cam.EventSelector.set(event)
|
||||
cam.EventNotification.set('Off')
|
||||
|
||||
# Enable event notifications on 'AcquisitionStart'
|
||||
cam.EventSelector.set('AcquisitionStart')
|
||||
cam.EventNotification.set('On')
|
||||
|
||||
# Register callable on all Features in the '/EventControl/EventData' - Category
|
||||
feats = cam.get_features_by_category('/EventControl/EventData')
|
||||
|
||||
for feat in feats:
|
||||
feat.register_change_handler(feature_changed_handler)
|
||||
|
||||
# Acquire a single Frame to trigger events.
|
||||
cam.get_frame()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
139
Vimba_6_0/VimbaPython/Examples/list_ancillary_data.py
Normal file
139
Vimba_6_0/VimbaPython/Examples/list_ancillary_data.py
Normal file
@@ -0,0 +1,139 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from typing import Optional
|
||||
from vimba import *
|
||||
|
||||
|
||||
def print_preamble():
|
||||
print('//////////////////////////////////////////////////////')
|
||||
print('/// Vimba API List Ancillary Data Features Example ///')
|
||||
print('//////////////////////////////////////////////////////\n')
|
||||
|
||||
|
||||
def print_usage():
|
||||
print('Usage:')
|
||||
print(' python list_ancillary_data.py [camera_id]')
|
||||
print(' python list_ancillary_data.py [/h] [-h]')
|
||||
print()
|
||||
print('Parameters:')
|
||||
print(' camera_id ID of the camera to use (using first camera if not specified)')
|
||||
print()
|
||||
|
||||
|
||||
def abort(reason: str, return_code: int = 1, usage: bool = False):
|
||||
print(reason + '\n')
|
||||
|
||||
if usage:
|
||||
print_usage()
|
||||
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
def parse_args() -> Optional[str]:
|
||||
args = sys.argv[1:]
|
||||
argc = len(args)
|
||||
|
||||
for arg in args:
|
||||
if arg in ('/h', '-h'):
|
||||
print_usage()
|
||||
sys.exit(0)
|
||||
|
||||
if len(args) > 1:
|
||||
abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True)
|
||||
|
||||
return args[0] if argc == 1 else None
|
||||
|
||||
|
||||
def get_camera(camera_id: Optional[str]) -> Camera:
|
||||
with Vimba.get_instance() as vimba:
|
||||
if camera_id:
|
||||
try:
|
||||
return vimba.get_camera_by_id(camera_id)
|
||||
|
||||
except VimbaCameraError:
|
||||
abort('Failed to access Camera \'{}\'. Abort.'.format(camera_id))
|
||||
|
||||
else:
|
||||
cams = vimba.get_all_cameras()
|
||||
if not cams:
|
||||
abort('No Cameras accessible. Abort.')
|
||||
|
||||
return cams[0]
|
||||
|
||||
|
||||
def setup_camera(cam: Camera):
|
||||
with cam:
|
||||
# Try to adjust GeV packet size. This Feature is only available for GigE - Cameras.
|
||||
try:
|
||||
cam.GVSPAdjustPacketSize.run()
|
||||
|
||||
while not cam.GVSPAdjustPacketSize.is_done():
|
||||
pass
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
pass
|
||||
|
||||
# Try to enable ChunkMode
|
||||
try:
|
||||
cam.ChunkModeActive.set(True)
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
abort('Failed to enable ChunkMode on Camera \'{}\'. Abort.'.format(cam.get_id()))
|
||||
|
||||
|
||||
def main():
|
||||
print_preamble()
|
||||
cam_id = parse_args()
|
||||
|
||||
with Vimba.get_instance():
|
||||
with get_camera(cam_id) as cam:
|
||||
setup_camera(cam)
|
||||
|
||||
# Capture single Frame and print all contained ancillary data
|
||||
frame = cam.get_frame()
|
||||
anc_data = frame.get_ancillary_data()
|
||||
if anc_data:
|
||||
with anc_data:
|
||||
print('Print ancillary data contained in Frame:')
|
||||
|
||||
for feat in anc_data.get_all_features():
|
||||
print('Feature Name : {}'.format(feat.get_name()))
|
||||
print('Display Name : {}'.format(feat.get_display_name()))
|
||||
print('Tooltip : {}'.format(feat.get_tooltip()))
|
||||
print('Description : {}'.format(feat.get_description()))
|
||||
print('SFNC Namespace : {}'.format(feat.get_sfnc_namespace()))
|
||||
print('Value : {}'.format(feat.get()))
|
||||
print()
|
||||
|
||||
else:
|
||||
abort('Frame {} does not contain AncillaryData. Abort'.format(frame.get_id()))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
57
Vimba_6_0/VimbaPython/Examples/list_cameras.py
Normal file
57
Vimba_6_0/VimbaPython/Examples/list_cameras.py
Normal file
@@ -0,0 +1,57 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
|
||||
from vimba import *
|
||||
|
||||
|
||||
def print_preamble():
|
||||
print('//////////////////////////////////////')
|
||||
print('/// Vimba API List Cameras Example ///')
|
||||
print('//////////////////////////////////////\n')
|
||||
|
||||
|
||||
def print_camera(cam: Camera):
|
||||
print('/// Camera Name : {}'.format(cam.get_name()))
|
||||
print('/// Model Name : {}'.format(cam.get_model()))
|
||||
print('/// Camera ID : {}'.format(cam.get_id()))
|
||||
print('/// Serial Number : {}'.format(cam.get_serial()))
|
||||
print('/// Interface ID : {}\n'.format(cam.get_interface_id()))
|
||||
|
||||
|
||||
def main():
|
||||
print_preamble()
|
||||
with Vimba.get_instance() as vimba:
|
||||
cams = vimba.get_all_cameras()
|
||||
|
||||
print('Cameras found: {}'.format(len(cams)))
|
||||
|
||||
for cam in cams:
|
||||
print_camera(cam)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
119
Vimba_6_0/VimbaPython/Examples/list_features.py
Normal file
119
Vimba_6_0/VimbaPython/Examples/list_features.py
Normal file
@@ -0,0 +1,119 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from typing import Optional
|
||||
from vimba import *
|
||||
|
||||
|
||||
def print_preamble():
|
||||
print('///////////////////////////////////////')
|
||||
print('/// Vimba API List Features Example ///')
|
||||
print('///////////////////////////////////////\n')
|
||||
|
||||
|
||||
def print_usage():
|
||||
print('Usage:')
|
||||
print(' python list_features.py [camera_id]')
|
||||
print(' python list_features.py [/h] [-h]')
|
||||
print()
|
||||
print('Parameters:')
|
||||
print(' camera_id ID of the camera to use (using first camera if not specified)')
|
||||
print()
|
||||
|
||||
|
||||
def abort(reason: str, return_code: int = 1, usage: bool = False):
|
||||
print(reason + '\n')
|
||||
|
||||
if usage:
|
||||
print_usage()
|
||||
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
def parse_args() -> Optional[str]:
|
||||
args = sys.argv[1:]
|
||||
argc = len(args)
|
||||
|
||||
for arg in args:
|
||||
if arg in ('/h', '-h'):
|
||||
print_usage()
|
||||
sys.exit(0)
|
||||
|
||||
if argc > 1:
|
||||
abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True)
|
||||
|
||||
return None if argc == 0 else args[0]
|
||||
|
||||
|
||||
def print_feature(feature):
|
||||
try:
|
||||
value = feature.get()
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
value = None
|
||||
|
||||
print('/// Feature name : {}'.format(feature.get_name()))
|
||||
print('/// Display name : {}'.format(feature.get_display_name()))
|
||||
print('/// Tooltip : {}'.format(feature.get_tooltip()))
|
||||
print('/// Description : {}'.format(feature.get_description()))
|
||||
print('/// SFNC Namespace : {}'.format(feature.get_sfnc_namespace()))
|
||||
print('/// Unit : {}'.format(feature.get_unit()))
|
||||
print('/// Value : {}\n'.format(str(value)))
|
||||
|
||||
|
||||
def get_camera(camera_id: Optional[str]) -> Camera:
|
||||
with Vimba.get_instance() as vimba:
|
||||
if camera_id:
|
||||
try:
|
||||
return vimba.get_camera_by_id(camera_id)
|
||||
|
||||
except VimbaCameraError:
|
||||
abort('Failed to access Camera \'{}\'. Abort.'.format(camera_id))
|
||||
|
||||
else:
|
||||
cams = vimba.get_all_cameras()
|
||||
if not cams:
|
||||
abort('No Cameras accessible. Abort.')
|
||||
|
||||
return cams[0]
|
||||
|
||||
|
||||
def main():
|
||||
print_preamble()
|
||||
cam_id = parse_args()
|
||||
|
||||
with Vimba.get_instance():
|
||||
with get_camera(cam_id) as cam:
|
||||
|
||||
print('Print all features of camera \'{}\':'.format(cam.get_id()))
|
||||
for feature in cam.get_all_features():
|
||||
print_feature(feature)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
125
Vimba_6_0/VimbaPython/Examples/load_save_settings.py
Normal file
125
Vimba_6_0/VimbaPython/Examples/load_save_settings.py
Normal file
@@ -0,0 +1,125 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from typing import Optional
|
||||
from vimba import *
|
||||
|
||||
|
||||
def print_preamble():
|
||||
print('////////////////////////////////////////////')
|
||||
print('/// Vimba API Load Save Settings Example ///')
|
||||
print('////////////////////////////////////////////\n')
|
||||
|
||||
|
||||
def print_usage():
|
||||
print('Usage:')
|
||||
print(' python load_save_settings.py [camera_id]')
|
||||
print(' python load_save_settings.py [/h] [-h]')
|
||||
print()
|
||||
print('Parameters:')
|
||||
print(' camera_id ID of the camera to use (using first camera if not specified)')
|
||||
print()
|
||||
|
||||
|
||||
def abort(reason: str, return_code: int = 1, usage: bool = False):
|
||||
print(reason + '\n')
|
||||
|
||||
if usage:
|
||||
print_usage()
|
||||
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
def parse_args() -> Optional[str]:
|
||||
args = sys.argv[1:]
|
||||
argc = len(args)
|
||||
|
||||
for arg in args:
|
||||
if arg in ('/h', '-h'):
|
||||
print_usage()
|
||||
sys.exit(0)
|
||||
|
||||
if argc > 1:
|
||||
abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True)
|
||||
|
||||
return None if argc == 0 else args[0]
|
||||
|
||||
|
||||
def get_camera(camera_id: Optional[str]) -> Camera:
|
||||
with Vimba.get_instance() as vimba:
|
||||
if camera_id:
|
||||
try:
|
||||
return vimba.get_camera_by_id(camera_id)
|
||||
|
||||
except VimbaCameraError:
|
||||
abort('Failed to access Camera \'{}\'. Abort.'.format(camera_id))
|
||||
|
||||
else:
|
||||
cams = vimba.get_all_cameras()
|
||||
if not cams:
|
||||
abort('No Cameras accessible. Abort.')
|
||||
|
||||
return cams[0]
|
||||
|
||||
|
||||
def main():
|
||||
print_preamble()
|
||||
cam_id = parse_args()
|
||||
|
||||
with Vimba.get_instance():
|
||||
print("--> Vimba has been started")
|
||||
|
||||
with get_camera(cam_id) as cam:
|
||||
print("--> Camera has been opened (%s)" % cam.get_id())
|
||||
|
||||
# Save camera settings to file.
|
||||
settings_file = '{}_settings.xml'.format(cam.get_id())
|
||||
cam.save_settings(settings_file, PersistType.All)
|
||||
print("--> Feature values have been saved to '%s'" % settings_file)
|
||||
|
||||
# Restore settings to initial value.
|
||||
try:
|
||||
cam.UserSetSelector.set('Default')
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
abort('Failed to set Feature \'UserSetSelector\'')
|
||||
|
||||
try:
|
||||
cam.UserSetLoad.run()
|
||||
print("--> All feature values have been restored to default")
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
abort('Failed to run Feature \'UserSetLoad\'')
|
||||
|
||||
# Load camera settings from file.
|
||||
cam.load_settings(settings_file, PersistType.All)
|
||||
print("--> Feature values have been loaded from given file '%s'" % settings_file)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
299
Vimba_6_0/VimbaPython/Examples/multithreading_opencv.py
Normal file
299
Vimba_6_0/VimbaPython/Examples/multithreading_opencv.py
Normal file
@@ -0,0 +1,299 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
|
||||
import copy
|
||||
import cv2
|
||||
import threading
|
||||
import queue
|
||||
import numpy
|
||||
|
||||
from typing import Optional
|
||||
from vimba import *
|
||||
|
||||
|
||||
FRAME_QUEUE_SIZE = 10
|
||||
FRAME_HEIGHT = 480
|
||||
FRAME_WIDTH = 480
|
||||
|
||||
|
||||
def print_preamble():
|
||||
print('////////////////////////////////////////////')
|
||||
print('/// Vimba API Multithreading Example ///////')
|
||||
print('////////////////////////////////////////////\n')
|
||||
print(flush=True)
|
||||
|
||||
|
||||
def add_camera_id(frame: Frame, cam_id: str) -> Frame:
|
||||
# Helper function inserting 'cam_id' into given frame. This function
|
||||
# manipulates the original image buffer inside frame object.
|
||||
cv2.putText(frame.as_opencv_image(), 'Cam: {}'.format(cam_id), org=(0, 30), fontScale=1,
|
||||
color=255, thickness=1, fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL)
|
||||
return frame
|
||||
|
||||
|
||||
def resize_if_required(frame: Frame) -> numpy.ndarray:
|
||||
# Helper function resizing the given frame, if it has not the required dimensions.
|
||||
# On resizing, the image data is copied and resized, the image inside the frame object
|
||||
# is untouched.
|
||||
cv_frame = frame.as_opencv_image()
|
||||
|
||||
if (frame.get_height() != FRAME_HEIGHT) or (frame.get_width() != FRAME_WIDTH):
|
||||
cv_frame = cv2.resize(cv_frame, (FRAME_WIDTH, FRAME_HEIGHT), interpolation=cv2.INTER_AREA)
|
||||
cv_frame = cv_frame[..., numpy.newaxis]
|
||||
|
||||
return cv_frame
|
||||
|
||||
|
||||
def create_dummy_frame() -> numpy.ndarray:
|
||||
cv_frame = numpy.zeros((50, 640, 1), numpy.uint8)
|
||||
cv_frame[:] = 0
|
||||
|
||||
cv2.putText(cv_frame, 'No Stream available. Please connect a Camera.', org=(30, 30),
|
||||
fontScale=1, color=255, thickness=1, fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL)
|
||||
|
||||
return cv_frame
|
||||
|
||||
|
||||
def try_put_frame(q: queue.Queue, cam: Camera, frame: Optional[Frame]):
|
||||
try:
|
||||
q.put_nowait((cam.get_id(), frame))
|
||||
|
||||
except queue.Full:
|
||||
pass
|
||||
|
||||
|
||||
def set_nearest_value(cam: Camera, feat_name: str, feat_value: int):
|
||||
# Helper function that tries to set a given value. If setting of the initial value failed
|
||||
# it calculates the nearest valid value and sets the result. This function is intended to
|
||||
# be used with Height and Width Features because not all Cameras allow the same values
|
||||
# for height and width.
|
||||
feat = cam.get_feature_by_name(feat_name)
|
||||
|
||||
try:
|
||||
feat.set(feat_value)
|
||||
|
||||
except VimbaFeatureError:
|
||||
min_, max_ = feat.get_range()
|
||||
inc = feat.get_increment()
|
||||
|
||||
if feat_value <= min_:
|
||||
val = min_
|
||||
|
||||
elif feat_value >= max_:
|
||||
val = max_
|
||||
|
||||
else:
|
||||
val = (((feat_value - min_) // inc) * inc) + min_
|
||||
|
||||
feat.set(val)
|
||||
|
||||
msg = ('Camera {}: Failed to set value of Feature \'{}\' to \'{}\': '
|
||||
'Using nearest valid value \'{}\'. Note that, this causes resizing '
|
||||
'during processing, reducing the frame rate.')
|
||||
Log.get_instance().info(msg.format(cam.get_id(), feat_name, feat_value, val))
|
||||
|
||||
|
||||
# Thread Objects
|
||||
class FrameProducer(threading.Thread):
|
||||
def __init__(self, cam: Camera, frame_queue: queue.Queue):
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
self.log = Log.get_instance()
|
||||
self.cam = cam
|
||||
self.frame_queue = frame_queue
|
||||
self.killswitch = threading.Event()
|
||||
|
||||
def __call__(self, cam: Camera, frame: Frame):
|
||||
# This method is executed within VimbaC context. All incoming frames
|
||||
# are reused for later frame acquisition. If a frame shall be queued, the
|
||||
# frame must be copied and the copy must be sent, otherwise the acquired
|
||||
# frame will be overridden as soon as the frame is reused.
|
||||
if frame.get_status() == FrameStatus.Complete:
|
||||
|
||||
if not self.frame_queue.full():
|
||||
frame_cpy = copy.deepcopy(frame)
|
||||
try_put_frame(self.frame_queue, cam, frame_cpy)
|
||||
|
||||
cam.queue_frame(frame)
|
||||
|
||||
def stop(self):
|
||||
self.killswitch.set()
|
||||
|
||||
def setup_camera(self):
|
||||
set_nearest_value(self.cam, 'Height', FRAME_HEIGHT)
|
||||
set_nearest_value(self.cam, 'Width', FRAME_WIDTH)
|
||||
|
||||
# Try to enable automatic exposure time setting
|
||||
try:
|
||||
self.cam.ExposureAuto.set('Once')
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
self.log.info('Camera {}: Failed to set Feature \'ExposureAuto\'.'.format(
|
||||
self.cam.get_id()))
|
||||
|
||||
self.cam.set_pixel_format(PixelFormat.Mono8)
|
||||
|
||||
def run(self):
|
||||
self.log.info('Thread \'FrameProducer({})\' started.'.format(self.cam.get_id()))
|
||||
|
||||
try:
|
||||
with self.cam:
|
||||
self.setup_camera()
|
||||
|
||||
try:
|
||||
self.cam.start_streaming(self)
|
||||
self.killswitch.wait()
|
||||
|
||||
finally:
|
||||
self.cam.stop_streaming()
|
||||
|
||||
except VimbaCameraError:
|
||||
pass
|
||||
|
||||
finally:
|
||||
try_put_frame(self.frame_queue, self.cam, None)
|
||||
|
||||
self.log.info('Thread \'FrameProducer({})\' terminated.'.format(self.cam.get_id()))
|
||||
|
||||
|
||||
class FrameConsumer(threading.Thread):
|
||||
def __init__(self, frame_queue: queue.Queue):
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
self.log = Log.get_instance()
|
||||
self.frame_queue = frame_queue
|
||||
|
||||
def run(self):
|
||||
IMAGE_CAPTION = 'Multithreading Example: Press <Enter> to exit'
|
||||
KEY_CODE_ENTER = 13
|
||||
|
||||
frames = {}
|
||||
alive = True
|
||||
|
||||
self.log.info('Thread \'FrameConsumer\' started.')
|
||||
|
||||
while alive:
|
||||
# Update current state by dequeuing all currently available frames.
|
||||
frames_left = self.frame_queue.qsize()
|
||||
while frames_left:
|
||||
try:
|
||||
cam_id, frame = self.frame_queue.get_nowait()
|
||||
|
||||
except queue.Empty:
|
||||
break
|
||||
|
||||
# Add/Remove frame from current state.
|
||||
if frame:
|
||||
frames[cam_id] = frame
|
||||
|
||||
else:
|
||||
frames.pop(cam_id, None)
|
||||
|
||||
frames_left -= 1
|
||||
|
||||
# Construct image by stitching frames together.
|
||||
if frames:
|
||||
cv_images = [resize_if_required(frames[cam_id]) for cam_id in sorted(frames.keys())]
|
||||
cv2.imshow(IMAGE_CAPTION, numpy.concatenate(cv_images, axis=1))
|
||||
|
||||
# If there are no frames available, show dummy image instead
|
||||
else:
|
||||
cv2.imshow(IMAGE_CAPTION, create_dummy_frame())
|
||||
|
||||
# Check for shutdown condition
|
||||
if KEY_CODE_ENTER == cv2.waitKey(10):
|
||||
cv2.destroyAllWindows()
|
||||
alive = False
|
||||
|
||||
self.log.info('Thread \'FrameConsumer\' terminated.')
|
||||
|
||||
|
||||
class MainThread(threading.Thread):
|
||||
def __init__(self):
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
self.frame_queue = queue.Queue(maxsize=FRAME_QUEUE_SIZE)
|
||||
self.producers = {}
|
||||
self.producers_lock = threading.Lock()
|
||||
|
||||
def __call__(self, cam: Camera, event: CameraEvent):
|
||||
# New camera was detected. Create FrameProducer, add it to active FrameProducers
|
||||
if event == CameraEvent.Detected:
|
||||
with self.producers_lock:
|
||||
self.producers[cam.get_id()] = FrameProducer(cam, self.frame_queue)
|
||||
self.producers[cam.get_id()].start()
|
||||
|
||||
# An existing camera was disconnected, stop associated FrameProducer.
|
||||
elif event == CameraEvent.Missing:
|
||||
with self.producers_lock:
|
||||
producer = self.producers.pop(cam.get_id())
|
||||
producer.stop()
|
||||
producer.join()
|
||||
|
||||
def run(self):
|
||||
log = Log.get_instance()
|
||||
consumer = FrameConsumer(self.frame_queue)
|
||||
|
||||
vimba = Vimba.get_instance()
|
||||
vimba.enable_log(LOG_CONFIG_INFO_CONSOLE_ONLY)
|
||||
|
||||
log.info('Thread \'MainThread\' started.')
|
||||
|
||||
with vimba:
|
||||
# Construct FrameProducer threads for all detected cameras
|
||||
for cam in vimba.get_all_cameras():
|
||||
self.producers[cam.get_id()] = FrameProducer(cam, self.frame_queue)
|
||||
|
||||
# Start FrameProducer threads
|
||||
with self.producers_lock:
|
||||
for producer in self.producers.values():
|
||||
producer.start()
|
||||
|
||||
# Start and wait for consumer to terminate
|
||||
vimba.register_camera_change_handler(self)
|
||||
consumer.start()
|
||||
consumer.join()
|
||||
vimba.unregister_camera_change_handler(self)
|
||||
|
||||
# Stop all FrameProducer threads
|
||||
with self.producers_lock:
|
||||
# Initiate concurrent shutdown
|
||||
for producer in self.producers.values():
|
||||
producer.stop()
|
||||
|
||||
# Wait for shutdown to complete
|
||||
for producer in self.producers.values():
|
||||
producer.join()
|
||||
|
||||
log.info('Thread \'MainThread\' terminated.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print_preamble()
|
||||
main = MainThread()
|
||||
main.start()
|
||||
main.join()
|
||||
117
Vimba_6_0/VimbaPython/Examples/synchronous_grab.py
Normal file
117
Vimba_6_0/VimbaPython/Examples/synchronous_grab.py
Normal file
@@ -0,0 +1,117 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from typing import Optional
|
||||
from vimba import *
|
||||
|
||||
|
||||
def print_preamble():
|
||||
print('//////////////////////////////////////////')
|
||||
print('/// Vimba API Synchronous Grab Example ///')
|
||||
print('//////////////////////////////////////////\n')
|
||||
|
||||
|
||||
def print_usage():
|
||||
print('Usage:')
|
||||
print(' python synchronous_grab.py [camera_id]')
|
||||
print(' python synchronous_grab.py [/h] [-h]')
|
||||
print()
|
||||
print('Parameters:')
|
||||
print(' camera_id ID of the camera to use (using first camera if not specified)')
|
||||
print()
|
||||
|
||||
|
||||
def abort(reason: str, return_code: int = 1, usage: bool = False):
|
||||
print(reason + '\n')
|
||||
|
||||
if usage:
|
||||
print_usage()
|
||||
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
def parse_args() -> Optional[str]:
|
||||
args = sys.argv[1:]
|
||||
argc = len(args)
|
||||
|
||||
for arg in args:
|
||||
if arg in ('/h', '-h'):
|
||||
print_usage()
|
||||
sys.exit(0)
|
||||
|
||||
if argc > 1:
|
||||
abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True)
|
||||
|
||||
return None if argc == 0 else args[0]
|
||||
|
||||
|
||||
def get_camera(camera_id: Optional[str]) -> Camera:
|
||||
with Vimba.get_instance() as vimba:
|
||||
if camera_id:
|
||||
try:
|
||||
return vimba.get_camera_by_id(camera_id)
|
||||
|
||||
except VimbaCameraError:
|
||||
abort('Failed to access Camera \'{}\'. Abort.'.format(camera_id))
|
||||
|
||||
else:
|
||||
cams = vimba.get_all_cameras()
|
||||
if not cams:
|
||||
abort('No Cameras accessible. Abort.')
|
||||
|
||||
return cams[0]
|
||||
|
||||
|
||||
def setup_camera(cam: Camera):
|
||||
with cam:
|
||||
# Try to adjust GeV packet size. This Feature is only available for GigE - Cameras.
|
||||
try:
|
||||
cam.GVSPAdjustPacketSize.run()
|
||||
|
||||
while not cam.GVSPAdjustPacketSize.is_done():
|
||||
pass
|
||||
|
||||
except (AttributeError, VimbaFeatureError):
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
print_preamble()
|
||||
cam_id = parse_args()
|
||||
|
||||
with Vimba.get_instance():
|
||||
with get_camera(cam_id) as cam:
|
||||
setup_camera(cam)
|
||||
|
||||
# Acquire 10 frame with a custom timeout (default is 2000ms) per frame acquisition.
|
||||
for frame in cam.get_frame_generator(limit=10, timeout_ms=3000):
|
||||
print('Got {}'.format(frame), flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
328
Vimba_6_0/VimbaPython/Examples/user_set.py
Normal file
328
Vimba_6_0/VimbaPython/Examples/user_set.py
Normal file
@@ -0,0 +1,328 @@
|
||||
"""BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2019, Allied Vision Technologies GmbH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from typing import Optional, Dict, Any
|
||||
from vimba import *
|
||||
|
||||
|
||||
def print_preamble():
|
||||
print('//////////////////////////////////')
|
||||
print('/// Vimba API User Set Example ///')
|
||||
print('//////////////////////////////////\n')
|
||||
|
||||
|
||||
def print_usage():
|
||||
print('Usage:')
|
||||
print(' python user_set.py [camera_id] [/i:Index] [/{h|s|l|i|m|d|or|os|n}]')
|
||||
print()
|
||||
print('Parameters:')
|
||||
print(' camera_id ID of the camera to use (using first camera if not specified)')
|
||||
print(' /i:index User set index')
|
||||
print(' /h Print help')
|
||||
print(' /s Save user set to flash')
|
||||
print(' /l Load user set from flash (default if not specified)')
|
||||
print(' /i Get selected user set index')
|
||||
print(' /m Make user set default')
|
||||
print(' /d Is user set default')
|
||||
print(' /or Get user set operation default.')
|
||||
print(' /os Get user set operation status.')
|
||||
print(' /n Get user set count')
|
||||
print()
|
||||
print('Examples:')
|
||||
print(' To load user set 0 (factory set) from flash in order to activate it call:')
|
||||
print(' UserSet /i:0 /l')
|
||||
print()
|
||||
print(' To save the current settings to user set 1 call:')
|
||||
print(' UserSet /i:1 /s')
|
||||
print()
|
||||
|
||||
|
||||
def abort(reason: str, return_code: int = 1, usage: bool = False):
|
||||
print(reason + '\n')
|
||||
|
||||
if usage:
|
||||
print_usage()
|
||||
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
def parse_args() -> Dict[str, Any]:
|
||||
args = sys.argv[1:]
|
||||
argc = len(args)
|
||||
|
||||
result: Dict[str, Any] = {}
|
||||
|
||||
if (argc <= 0) or (4 <= argc):
|
||||
abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True)
|
||||
|
||||
for arg in args:
|
||||
if arg in ('/h'):
|
||||
print_usage()
|
||||
sys.exit(0)
|
||||
|
||||
# Examine command parameters
|
||||
if arg in ('/s', '/l', '/i', '/m', '/d', '/or', '/os', '/n'):
|
||||
if result.get('mode') is None:
|
||||
result['mode'] = arg
|
||||
|
||||
else:
|
||||
abort(reason="Multiple Commands specified. Abort.", return_code=2, usage=True)
|
||||
|
||||
# Examine specified index
|
||||
elif arg.startswith('/i:'):
|
||||
_, set_id = arg.split(':')
|
||||
|
||||
if not set_id:
|
||||
abort(reason="No index specified after /i:. Abort.", return_code=2, usage=True)
|
||||
|
||||
try:
|
||||
set_id_int = int(set_id)
|
||||
|
||||
except ValueError:
|
||||
abort(reason="Number in /i:<no> is no Integer. Abort.", return_code=2, usage=True)
|
||||
|
||||
if set_id_int < 0:
|
||||
abort(reason="Number in /i:<no> is negative. Abort.", return_code=2, usage=True)
|
||||
|
||||
if result.get('set_id') is not None:
|
||||
abort(reason="Multiple /i:<no> specified. Abort.", return_code=2, usage=True)
|
||||
|
||||
result['set_id'] = set_id_int
|
||||
|
||||
# Examine camera id
|
||||
elif result.get('camera_id') is None:
|
||||
result['camera_id'] = arg
|
||||
|
||||
else:
|
||||
abort(reason="Invalid arguments. Abort.", return_code=2, usage=True)
|
||||
|
||||
# Apply defaults
|
||||
if not result.get('mode'):
|
||||
result['mode'] = '/l'
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def get_camera(cam_id: Optional[str]):
|
||||
with Vimba.get_instance() as vimba:
|
||||
# Lookup Camera if it was specified.
|
||||
if cam_id:
|
||||
try:
|
||||
return vimba.get_camera_by_id(cam_id)
|
||||
|
||||
except VimbaCameraError:
|
||||
abort('Failed to access Camera {}. Abort.'.format(cam_id))
|
||||
|
||||
# If no camera was specified, use first detected camera.
|
||||
else:
|
||||
cams = vimba.get_all_cameras()
|
||||
if not cams:
|
||||
abort('No Camera detected. Abort.')
|
||||
|
||||
return cams[0]
|
||||
|
||||
|
||||
def select_user_set(camera: Camera, set_id: int):
|
||||
try:
|
||||
camera.get_feature_by_name('UserSetSelector').set(set_id)
|
||||
|
||||
except VimbaFeatureError:
|
||||
abort('Failed to select user set with \'{}\'. Abort.'.format(set_id))
|
||||
|
||||
|
||||
def load_from_flash(cam: Camera, set_id: int):
|
||||
with cam:
|
||||
print('Loading user set \'{}\' from flash.'.format(set_id))
|
||||
|
||||
select_user_set(cam, set_id)
|
||||
|
||||
try:
|
||||
cmd = cam.get_feature_by_name('UserSetLoad')
|
||||
cmd.run()
|
||||
|
||||
while not cmd.is_done():
|
||||
pass
|
||||
|
||||
except VimbaFeatureError:
|
||||
abort('Failed to load user set \'{}\' from flash. Abort.'.format(set_id))
|
||||
|
||||
print('Loaded user set \'{}\' loaded from flash successfully.'.format(set_id))
|
||||
|
||||
|
||||
def save_to_flash(cam: Camera, set_id: int):
|
||||
with cam:
|
||||
print('Saving user set \'{}\' to flash.'.format(set_id))
|
||||
|
||||
select_user_set(cam, set_id)
|
||||
|
||||
try:
|
||||
cmd = cam.get_feature_by_name('UserSetSave')
|
||||
cmd.run()
|
||||
|
||||
while not cmd.is_done():
|
||||
pass
|
||||
|
||||
except VimbaFeatureError:
|
||||
abort('Failed to save user set \'{}\' to flash. Abort.'.format(set_id))
|
||||
|
||||
print('Saved user set \'{}\' to flash.'.format(set_id))
|
||||
|
||||
|
||||
def get_active_user_set(cam: Camera, _: int):
|
||||
with cam:
|
||||
print('Get selected user set id.')
|
||||
|
||||
try:
|
||||
value = cam.get_feature_by_name('UserSetSelector').get()
|
||||
|
||||
except VimbaFeatureError:
|
||||
abort('Failed to get user set id. Abort.')
|
||||
|
||||
print('The selected user set id is \'{}\'.'.format(int(value)))
|
||||
|
||||
|
||||
def get_number_of_user_sets(cam: Camera, _: int):
|
||||
with cam:
|
||||
print('Get total number of user sets.')
|
||||
|
||||
try:
|
||||
feat = cam.get_feature_by_name('UserSetSelector')
|
||||
value = len(feat.get_available_entries())
|
||||
|
||||
except VimbaFeatureError:
|
||||
abort('Failed to get total number of user sets. Abort.')
|
||||
|
||||
print('The total number of user sets is \'{}\''.format(value))
|
||||
|
||||
|
||||
def set_default_user_set(cam: Camera, set_id: int):
|
||||
with cam:
|
||||
print('Set user set \'{}\' as default.'.format(set_id))
|
||||
|
||||
# Try to set mode via UserSetDefaultSelector feature
|
||||
try:
|
||||
feat = cam.get_feature_by_name('UserSetDefaultSelector')
|
||||
|
||||
try:
|
||||
feat.set(set_id)
|
||||
|
||||
except VimbaFeatureError:
|
||||
abort('Failed to set user set id \'{}\' as default user set'.format(set_id))
|
||||
|
||||
except VimbaFeatureError:
|
||||
# Try to set mode via UserSetMakeDefault command
|
||||
select_user_set(cam, set_id)
|
||||
|
||||
try:
|
||||
cmd = cam.get_feature_by_name('UserSetMakeDefault')
|
||||
cmd.run()
|
||||
|
||||
while not cmd.is_done():
|
||||
pass
|
||||
|
||||
except VimbaFeatureError:
|
||||
abort('Failed to set user set id \'{}\' as default user set'.format(set_id))
|
||||
|
||||
print('User set \'{}\' is the new default user set.'.format(set_id))
|
||||
|
||||
|
||||
def is_default_user_set(cam: Camera, set_id: int):
|
||||
with cam:
|
||||
print('Is user set \'{}\' the default user set?'.format(set_id))
|
||||
|
||||
try:
|
||||
default_id = int(cam.get_feature_by_name('UserSetDefaultSelector').get())
|
||||
|
||||
except VimbaFeatureError:
|
||||
abort('Failed to get default user set id. Abort.')
|
||||
|
||||
msg = 'User set \'{}\' {} the default user set.'
|
||||
print(msg.format(set_id, 'is' if set_id == default_id else 'is not'))
|
||||
|
||||
|
||||
def get_operation_result(cam: Camera, set_id: int):
|
||||
with cam:
|
||||
print('Get user set operation result.')
|
||||
|
||||
try:
|
||||
result = cam.get_feature_by_name('UserSetOperationResult').get()
|
||||
|
||||
except VimbaFeatureError:
|
||||
abort('Failed to get user set operation result. Abort.')
|
||||
|
||||
print('Operation result was {}.'.format(result))
|
||||
|
||||
|
||||
def get_operation_status(cam: Camera, set_id: int):
|
||||
with cam:
|
||||
print('Get user set operation status.')
|
||||
|
||||
try:
|
||||
result = cam.get_feature_by_name('UserSetOperationStatus').get()
|
||||
|
||||
except VimbaFeatureError:
|
||||
abort('Failed to get user set operation status. Abort.')
|
||||
|
||||
print('Operation status was {}.'.format(result))
|
||||
|
||||
|
||||
def main():
|
||||
print_preamble()
|
||||
args = parse_args()
|
||||
|
||||
with Vimba.get_instance():
|
||||
cam = get_camera(args.get('camera_id'))
|
||||
print('Using Camera with ID \'{}\''.format(cam.get_id()))
|
||||
|
||||
with cam:
|
||||
mode = args['mode']
|
||||
|
||||
try:
|
||||
set_id = args.get('set_id', int(cam.get_feature_by_name('UserSetSelector').get()))
|
||||
|
||||
except VimbaFeatureError:
|
||||
abort('Failed to get id of current user set. Abort.')
|
||||
|
||||
# Mode -> Function Object mapping
|
||||
mode_to_fn = {
|
||||
'/l': load_from_flash,
|
||||
'/s': save_to_flash,
|
||||
'/i': get_active_user_set,
|
||||
'/n': get_number_of_user_sets,
|
||||
'/m': set_default_user_set,
|
||||
'/d': is_default_user_set,
|
||||
'/or': get_operation_result,
|
||||
'/os': get_operation_status
|
||||
}
|
||||
|
||||
fn = mode_to_fn[mode]
|
||||
fn(cam, set_id)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user