Quick Start#
The goal is not only to show code that works but to give you an idea how to reuse and adapt the sample code in more complex scenarios.
In its simplest form, image acquisition with pypylon involves the following steps:
- Discovering the camera
- Opening the camera
- Starting image acquisition
- Retrieving images
- Processing the images
The following example demonstrates these steps. It has the following additional benefits:
- It allows you to verify that your installation works.
- You can test camera connectivity.
- It helps you to understand the acquisition lifecycle.
例:
from pypylon import pylon
with pylon.InstantCamera(pylon.FirstFound) as camera:
camera.StartGrabbingMax(1)
with camera.RetrieveResult(5000) as grab_result:
if grab_result.GrabSucceeded():
image = grab_result.Array
print(image.shape)
Let's break this sample down into its individual steps:
-
Creating and managing the camera
This line does two important things:
- It selects the first available camera, uses the transport layer factory to create a pylon device object, and wraps it in an
InstantCameraobject for easy use. - Using
withensures that resources are released automatically and that the camera is closed properly even if an error occurs.
- It selects the first available camera, uses the transport layer factory to create a pylon device object, and wraps it in an
-
Starting acquisition
This opens the camera, if it's not open already, starts the acquisition engine, and tells the camera to do the following:
- Acquire exactly one frame.
- Stop acquisition afterwards.
This is ideal for testing because it avoids infinite loops.
The camera can also be opened beforehand with a call to Open.
Opening creates the connection and readies the camera for the following tasks:
- Configuring parameters
- Starting acquisition
-
Retrieving the grab result
This call blocks until either of these events happens:
- A frame is available.
or - The timeout (5000 ms) is reached.
Internally, you are pulling an image from a buffer queue.
- A frame is available.
-
Validating the grab
Even if a grab result is returned, the acquisition may have failed (e.g., transport errors). Always check success before using the data.
-
Creating a copy of the pixel data (optional)
Grab results use buffers from a buffer pool. When you access
grab_result.Array, you get a copy of the grab result buffer data. Alternatively, you can useGetMemoryView()orGetArrayZeroCopy()to access the data without copying.The buffer from the grab result becomes invalid after leaving the
withblock because thegrab_resultis released. To use the pixel data outside thewithblock, you would need to make a copy of it. You can use theArrayfunction to create a copy of the pixel data. -
Using the image
This confirms the following:
- The image exists.
- The data has a valid shape.
Mental Model#
This visualization illustrates what happens internally.
Camera (hardware)
↓
pylon driver + buffers
↓
Transport layer (USB/GigE)
↓
RetrieveResult()
↓
NumPy array (your application)
Key idea:
- The camera runs asynchronously (producing frames).
- Your Python code consumes frames synchronously.
Buffers sit in between and decouple both worlds (camera and application).
Key Takeaways#
- Always check
GrabSucceeded(). - Always copy image data before leaving the result scope or releasing the grab result, e.g., using the
Arrayfunction. - Use
StartGrabbingMax(1)for simple tests. Alternatively, you can useGrabOne(5000).
StartGrabbingMax(1)grabs exactly one image.GrabOne(5000)grabs one image but times out after 5000 µs. - Use
withto ensure safe resource handling.
Once you have understood this pattern, you can build on it to write code for the following tasks:
- Building continuous acquisition loops
- Configuring triggered acquisition
- Designing a multi-camera setup