跳至內容
測試伺服器
開發伺服器

Python程式設計人員指南#

本節提供專門針對 Stereo ace 相機的資訊。本文並未旨在全面介紹 Basler pylon 官方的 Python 語言繫結。

Basler pylon 官方的 Python 語言繫結為 pypylon 專案。pypylon 是託管於 GitHub 上的開源專案。您必須使用 26.06 或更新版本才能使用 Stereo ace 相機。

如果您對 pypylon 不熟悉,Basler 建議您先閱讀 pypylon documentation 來熟悉 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 補充套件包含一些 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 位址。

如果您在執行範例時遇到問題,請參閱Troubleshooting主題。

範例清單#

  • SimpleGrab: 示範如何從 Stereo ace 相機擷取影像,以及存取強度與視差資料。
  • ShowPointCloud: 示範如何從視差資料計算點雲,並使用 Open3D 進行視覺化。
  • OpenDeviceByIpAddress: 示範如何透過其中一個 IP 位址開啟 Stereo ace 相機。
  • TriggerLowLatency: 示範如何透過將相機的強度與深度解析度設定為 High (2x2 Binning) 來降低延遲。
  • LeftAndRightIntensity: 示範如何從 Stereo ace 相機擷取左側與右側強度影像。

如需更多詳細資訊,請參閱 Python 範例目錄中的範例程式碼。

如何將 pypylon 與 Stereo ace 相機搭配使用#

開啟與存取 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")

存取相機 Property#

您可以使用 camera 物件來存取和修改相機 Property。例如:

# 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主題。