Python程式設計指南#
Basler pylon 的官方 Python 語言綁定是pypylon專案。pypylon 是一個託管於 GitHub 上的開源專案。若要使用 BaslerStereo ace 相機,您需要 26.06 版或更高版本。
如果您是 pypylon 的新手,Basler 建議您先閱讀 pypylon documentation 以熟悉 pypylon API。
Python 程式設計範例#
執行範例的先決條件#
- pylon 26.06 或更高版本
- Python 3.9 或更高版本
- pypylon26.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 像素合併),來降低延遲。
- 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 相機時遇到問題,請參閱「疑難排解」章節。