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

Python程式設計指南#

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

Basler pylon 官方的 Python 語言繫結為 pypylon 專案。pypylon 是一個託管於 GitHub 上的開源專案。您需要 26.06 或更高版本才能使用 Basler 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:示範如何透過將相機的強度和深度解析度設為高(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 相機時遇到問題,請參閱故障排除主題。