Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples: grab multiple image frames #802

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions samples/grabMultipleFrames.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'''
Use pypylon to grab multiple image frames with hardware triggers.
Test camera: acA640-750um
Version: pypylon == 4.1.0
Maximum framerate: 1000fps
'''
from pypylon import pylon
import traceback
import numpy as np

# your recording frames
numFrames = 1000
# Initialize camera instance and open the camera
cam = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
cam.Open()
# Print the model name of the camera.
print("Using camera device: ", cam.GetDeviceInfo().GetModelName())

# class of image handler
class ImageHandler(pylon.ImageEventHandler):
def __init__(self):
super().__init__()
self.count = 0
self.img_sum = []

def OnImagesSkipped(self, camera, countOfSkippedImages):
print(countOfSkippedImages, " images have been skipped.")

def OnImageGrabbed(self, camera, grabResult):
""" we get called on every image
!! this code is run in a pylon thread context
always wrap your code in the try .. except to capture
errors inside the grabbing as this can't be properly reported from
the background thread to the foreground python code
"""
try:
if grabResult.GrabSucceeded():
# check image contents
img = grabResult.Array
# Stack images to 3D array
self.img_sum.append(img)
self.count += 1
grabResult.Release()
else:
raise RuntimeError("Grab Failed")
except Exception as e:
traceback.print_exc()

# Control the parameter of the Basler camera
# Get clean powerup state
cam.UserSetSelector.Value = "Default"
cam.UserSetLoad.Execute()
# Set the io section
cam.LineSelector.Value = "Line1"
cam.LineMode.Value = "Input"
# Setup the trigger/ acquisition controls
cam.TriggerSelector.Value = "FrameStart"
cam.TriggerSource.Value = "Line1"
cam.TriggerMode.Value = "On"
cam.TriggerActivation.Value = "RisingEdge"
print("The resulting Trigger Activation is: " + str(cam.TriggerActivation.Value))

# Instantiate callback handler of camera image
handler = ImageHandler()
# handler registration for camera
cam.RegisterImageEventHandler(handler , pylon.RegistrationMode_ReplaceAll, pylon.Cleanup_None)
# start capturing frames
cam.StartGrabbingMax(numFrames + 1, pylon.GrabStrategy_LatestImages, pylon.GrabLoop_ProvidedByInstantCamera)
# other hardware running time here...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a quick test provide a dummy loop:

while cam.IsGrabbing():
    time.sleep(0.1)

cam.StopGrabbing()
cam.DeregisterImageEventHandler(handler)
cam.TriggerMode.Value = "Off"
cam.Close()
print("Recorded frames in the camera is: ", str(handler.count))
cameraImages = np.array(handler.img_sum).transpose(1,2,0)
# check if captured camera frames are the same with the patterns
assert cameraImages.shape[2] == numFrames