aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authormorefigs <morefigs@gmail.com>2019-02-11 17:07:41 +1100
committermorefigs <morefigs@gmail.com>2019-02-11 17:07:41 +1100
commita3ad21c6b05754315825f8ae62def71a0c73988a (patch)
treeb3f0c6f61f28b32c553f95bcf5238d0a4e0658ef /examples
parent367295804b27d3eeef27bb85826426828a6bda03 (diff)
downloadpymba-a3ad21c6b05754315825f8ae62def71a0c73988a.tar.gz
pymba-a3ad21c6b05754315825f8ae62def71a0c73988a.zip
added convenience functions for acquiring single images and streaming images indefinitely, with examples.
Diffstat (limited to 'examples')
-rw-r--r--examples/camera/opencv_acquire_image.py36
-rw-r--r--examples/camera/opencv_acquire_streaming_images.py39
-rw-r--r--examples/camera/opencv_capture_image.py34
-rw-r--r--examples/camera/opencv_capture_image_with_callback.py44
4 files changed, 75 insertions, 78 deletions
diff --git a/examples/camera/opencv_acquire_image.py b/examples/camera/opencv_acquire_image.py
new file mode 100644
index 0000000..e067959
--- /dev/null
+++ b/examples/camera/opencv_acquire_image.py
@@ -0,0 +1,36 @@
+import cv2
+from pymba import Vimba, Frame
+
+
+def process_frame(frame: Frame):
+ """
+ Processes the acquired frame.
+ """
+ print(f'frame {frame.data.frameID} callback')
+
+ # get a copy of the frame data
+ image = frame.buffer_data_numpy()
+
+ # display image
+ cv2.imshow('Image', image)
+
+ # wait for user to close window
+ cv2.waitKey(0)
+
+
+if __name__ == '__main__':
+
+ with Vimba() as vimba:
+ camera = vimba.camera(0)
+ camera.open()
+
+ camera.arm('SingleFrame')
+
+ # capture a single frame, more than once if desired
+ for i in range(3):
+ frame_ = camera.acquire_frame()
+ process_frame(frame_)
+
+ camera.disarm()
+
+ camera.close()
diff --git a/examples/camera/opencv_acquire_streaming_images.py b/examples/camera/opencv_acquire_streaming_images.py
new file mode 100644
index 0000000..a3efb6b
--- /dev/null
+++ b/examples/camera/opencv_acquire_streaming_images.py
@@ -0,0 +1,39 @@
+from time import sleep
+import cv2
+from pymba import Vimba, Frame
+
+
+def process_frame(frame: Frame):
+ """
+ Process the streaming frames. Consider sending the frame data to another thread/process if this is long running to
+ avoid dropping frames.
+ """
+ print(f'frame {frame.data.frameID} callback')
+
+ # get a copy of the frame data
+ image = frame.buffer_data_numpy()
+
+ # display image
+ cv2.imshow('Image', image)
+ cv2.waitKey(1)
+
+
+if __name__ == '__main__':
+
+ with Vimba() as vimba:
+ camera = vimba.camera(0)
+ camera.open()
+
+ # arm the camera and provide a function to be called upon frame ready
+ camera.arm('Continuous', process_frame)
+ camera.start_frame_acquisition()
+
+ # stream images for a while...
+ sleep(5)
+
+ # stop frame acquisition
+ # start_frame_acquisition can simply be called again if the camera is still armed
+ camera.stop_frame_acquisition()
+ camera.disarm()
+
+ camera.close()
diff --git a/examples/camera/opencv_capture_image.py b/examples/camera/opencv_capture_image.py
deleted file mode 100644
index c185f6d..0000000
--- a/examples/camera/opencv_capture_image.py
+++ /dev/null
@@ -1,34 +0,0 @@
-import cv2
-from pymba import Vimba
-
-
-if __name__ == '__main__':
-
- with Vimba() as vimba:
- camera = vimba.camera(0)
- camera.open()
-
- # setup camera and frame and capture a single image
- camera.AcquisitionMode = 'SingleFrame'
- frame = camera.new_frame()
- frame.announce()
- camera.start_capture()
- frame.queue_for_capture()
- camera.run_feature_command('AcquisitionStart')
- frame.wait_for_capture()
- camera.run_feature_command('AcquisitionStop')
-
- # get the image data as a numpy array
- image = frame.buffer_data_numpy()
-
- # display image
- cv2.imshow(camera.camera_id, image)
- # waits for user to close image
- cv2.waitKey(0)
-
- # stop capturing and clean up
- camera.end_capture()
- camera.flush_capture_queue()
- camera.revoke_all_frames()
-
- camera.close()
diff --git a/examples/camera/opencv_capture_image_with_callback.py b/examples/camera/opencv_capture_image_with_callback.py
deleted file mode 100644
index 67cabdf..0000000
--- a/examples/camera/opencv_capture_image_with_callback.py
+++ /dev/null
@@ -1,44 +0,0 @@
-from time import sleep
-import cv2
-from pymba import Vimba
-from pymba.frame import Frame
-
-
-def on_callback(completed_frame: Frame):
- print('Callback called!')
-
- # get the image data as a numpy array
- image = completed_frame.buffer_data_numpy()
-
- # display image
- cv2.imshow(camera.camera_id, image)
- # waits for user to close image
- cv2.waitKey(0)
-
-
-if __name__ == '__main__':
-
- with Vimba() as vimba:
- camera = vimba.camera(0)
- camera.open()
-
- # setup camera and frame and capture a single image
- camera.AcquisitionMode = 'SingleFrame'
- frame = camera.new_frame()
- frame.announce()
- camera.start_capture()
- frame.queue_for_capture(on_callback)
- camera.run_feature_command('AcquisitionStart')
- camera.run_feature_command('AcquisitionStop')
-
- # wait long enough for the frame callback to be called
- for _ in range(100):
- sleep(0.1)
- print('.', end='')
-
- # stop capturing and clean up
- camera.end_capture()
- camera.flush_capture_queue()
- camera.revoke_all_frames()
-
- camera.close()