How to Get Started with the Pypylon Python Wrapper for Basler Cameras

Dec 21, 2023 | Data Science

Welcome to the exciting world of intuitive image capture with Basler cameras! This guide will walk you through the installation and usage of the Pypylon Python wrapper, allowing you to seamlessly interact with Basler’s powerful camera technology. Let’s dive in!

Getting Started: Installation

  • First, it is strongly recommended to install the pylon Camera Software Suite. This installation ensures you have all the necessary camera drivers and support.
  • Next, install Pypylon by running the following command in your terminal:
    pip3 install pypylon
  • Explore samples/grab.py to see how to capture images from the camera using Pypylon.

Basic Usage

Let’s break down a simple Python snippet that demonstrates how to access and capture images from a Basler camera:

from pylon import pylon

camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
camera.Open()

# Demonstrate feature access
new_width = camera.Width.Value - camera.Width.Inc
if new_width >= camera.Width.Min:
    camera.Width.Value = new_width

numberOfImagesToGrab = 100
camera.StartGrabbingMax(numberOfImagesToGrab)

while camera.IsGrabbing():
    grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
    if grabResult.GrabSucceeded():
        # Access the image data
        print("SizeX: ", grabResult.Width)
        print("SizeY: ", grabResult.Height)
        img = grabResult.Array
        print("Gray value of first pixel: ", img[0, 0])
    grabResult.Release()

camera.Close()

Understanding the Code: An Analogy

Imagine you have a digital camera in a busy photography studio. The code above is akin to a photographer setting up their camera:

  • Grabbing the Camera: The line where we create an InstantCamera represents the photographer picking up the camera from the shelf.
  • Opening the Camera: When the camera is opened with camera.Open(), it’s like the photographer clicking the camera on.
  • Adjusting Settings: Modifying the width is similar to adjusting the zoom lens to capture a better frame.
  • Starting the Capture: By calling camera.StartGrabbingMax, the photographer starts snapping photos one by one.
  • Capturing Each Image: The while loop is the photographer reviewing each snapshot they just took, validating if the picture turned out sharp before moving forward.
  • Closing the Camera: Finally, camera.Close() is akin to the photographer carefully putting the camera back on the shelf after the shoot is done.

Working with Pylon Data Processing

Pypylon also provides support for the pylon Data Processing API, allowing intricate image processing capabilities:

Troubleshooting Tips

Encountering issues? Here are a few troubleshooting tips:

  • If you face USB 3.0 camera connectivity issues on Linux, ensure appropriate udev rules are installed. Obtaining these through the official pylon package is recommended.
  • For troubleshooting installation problems, make sure you’re using a compatible version of Python and pip.
  • In case you run into deprecated assignment warnings, update your code to use the new assignment style (e.g., cam.Gain.Value = 42).

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox