跳转到内容
STAGING SERVER
DEVELOPMENT SERVER

Python 程序员指南#

本节提供有关 Basler Stereo ace 相机的特定信息。它无意全面概述官方的 Basler pylon Python 语言绑定。

官方的 Basler pylon Python 语言绑定为 pypylon 项目。pypylon 是托管在 GitHub 上的开源项目。您需要 26.06 或更高版本才能使用 Basler Stereo ace 相机。

如果您是 pypylon 的新手,Basler 建议您先阅读 pypylon 文档,熟悉 pypylon API。

Python 编程示例#

运行示例的先决条件#

  • pylon 26.06 或更高版本
  • Python 3.9 或更高版本
  • pypylon 26.06 或更高版本

每个示例都有一个 requirements.txt 文件。根据示例的不同,它包含 pypylon 以及诸如以下的可选依赖项: NumPy, OpenCV或 Open3D.

要安装所需的库,请导航至 Python 示例目录并安装您要运行的示例所需的依赖项:

python3 -m pip install -r SimpleGrab/requirements.txt
python3 -m pip install -r OpenDeviceByIpAddress/requirements.txt
python3 -m pip install -r LeftAndRightIntensity/requirements.txt
python3 -m pip install -r TriggerLowLatency/requirements.txt
python3 -m pip install -r ShowPointCloud/requirements.txt

运行示例#

针对 Stereo ace 的 pylon Supplementary Package 包含一些 Python 编程示例,用于演示如何使用 Python 和 pypylon 访问 Stereo ace 相机。

The Python samples are located in the C:\Program Files\Basler\pylon\Development\Samples\Stereo_ace\Python folder.

The Python samples are located in the /opt/pylon/share/pylon/Samples/Stereo_ace/Python folder.

The Python samples are located in the /opt/pylon/share/pylon/Samples/Stereo_ace/Python folder.

信息

在构建示例之前,请将包含示例的文件夹复制到您具有写入权限的任意位置。

在 Python 示例目录中,使用 Python 逐个运行示例。例如:

python3 SimpleGrab/SimpleGrab.py
python3 OpenDeviceByIpAddress/OpenDeviceByIpAddress.py <ip-address>
python3 LeftAndRightIntensity/LeftAndRightIntensity.py
python3 TriggerLowLatency/TriggerLowLatency.py
python3 ShowPointCloud/ShowPointCloud.py

将 <ip-address> 附带其中一个 Stereo ace 相机的 IP 地址。

如果在运行示例时遇到问题,请查看故障排除主题。

示例列表#

  • SimpleGrab:演示了如何从 Stereo ace 相机采集图像并访问强度和视差数据。
  • ShowPointCloud:演示了如何从视差数据计算点云并使用 Open3D 进行可视化。
  • OpenDeviceByIpAddress:演示了如何通过其中一个 IP 地址打开 Stereo ace 相机。
  • TriggerLowLatency:演示了如何通过将相机上的强度和深度分辨率设置为 High(2x2 Binning)来降低延迟。
  • LeftAndRightIntensity:演示了如何从 Stereo ace 相机采集左、右强度图像。

有关更多详细信息,请参阅 Python 示例目录中的示例代码。

如何在 Stereo ace 相机上使用 pypylon#

打开和访问 Stereo ace 相机#

以下示例演示了如何使用 pypylon 打开第一个可用的 Stereo ace 相机:

from pypylon import pylon

# Open the first available Stereo ace camera.
di = pylon.DeviceInfo()
di.SetDeviceClass("BaslerGTC/Basler/basler_xw")
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice(di))
camera.Open()

print(f"Connected to camera: {camera.GetDeviceInfo().GetModelName()}")

# Close the camera
camera.Close()

要通过序列号或用户自定义名称打开特定的 Stereo ace 相机,请修改 DeviceInfo 对象:

# Open a specific Stereo ace camera by serial number.
di.SetSerialNumber("12345678")

# Open a specific Stereo ace camera by user-defined name.
di.SetUserDefinedName("MyStereoAce")

访问相机参数#

您可以使用 camera 对象访问和修改相机参数。例如:

# Set the exposure time.
camera.ExposureTime.Value = 5000  # in microseconds

# Set the illumination mode.
camera.BslIlluminationMode.Value = "AlternateActive"

# Set the output resolutions.
camera.BslDepthResolution.Value = "High"
camera.BslIntensityResolution.Value = "Full"

# Print the current depth resolution.
print(f"Depth Resolution: {camera.BslDepthResolution.Value}")

采集数据#

以下示例演示了如何从 Stereo ace 相机采集强度和视差数据:

import numpy as np
import cv2
from pypylon import pylon

# Open the first available Stereo ace camera.
di = pylon.DeviceInfo()
di.SetDeviceClass("BaslerGTC/Basler/basler_xw")
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice(di))
camera.Open()

# Enable intensity and disparity components.
camera.ComponentSelector.Value = "Intensity"
camera.ComponentEnable.Value = True
camera.ComponentSelector.Value = "Disparity"
camera.ComponentEnable.Value = True

# Start grabbing
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)

while camera.IsGrabbing():
    grabResult = camera.RetrieveResult(20000, pylon.TimeoutHandling_ThrowException)

    if grabResult.GrabSucceeded():
        # Access intensity and disparity data.
        pylonDataContainer = grabResult.GetDataContainer()
        intensityComponent = pylonDataContainer.GetFirstImageDataComponent(pylon.ComponentType_Intensity, 0, False)
        disparityComponent = pylonDataContainer.GetFirstImageDataComponent(pylon.ComponentType_Disparity, 0, False)
        if not intensityComponent.IsValid() or not disparityComponent.IsValid():
            print("Missing intensity or disparity component in buffer.")
            grabResult.Release()
            continue

        intensity = intensityComponent.Array.reshape(intensityComponent.Height, intensityComponent.Width)
        disparity = disparityComponent.Array.reshape(disparityComponent.Height, disparityComponent.Width)

        # Display the images.
        cv2.imshow("Intensity", intensity)
        cv2.imshow("Disparity", disparity)

        # Break the loop on ESC key press.
        if cv2.waitKey(1) & 0xFF == 27:
            break

    grabResult.Release()

camera.StopGrabbing()
camera.Close()

计算点云#

请参考 ShowPointCloud.py 示例,其中包含有关如何使用 Open3D 计算和可视化点云的示例。

调试应用程序#

如果在将 pypylon 与 Stereo ace 相机结合使用时遇到问题,请查阅 Troubleshooting 主题。