Pythonプログラマーズガイド#
Basler pylon の公式 Python 言語バインディングは pypylon プロジェクトです。pypylon は GitHub でホストされているオープンソースプロジェクトです。Basler Stereo ace カメラを使用するには、バージョン 26.06 以降が必要です。
pypylon を初めて使用する場合は、まず pypylon documentation を読んで pypylon API に慣れることを Basler はお勧めします。
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 と pypylon を使用して Stereo ace カメラにアクセスする方法を示すいくつかの Python プログラミングサンプルが含まれています。
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 を使用して一度に 1 つのサンプルを実行します。例:
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のサンプルディレクトリにあるサンプルコードを参照してください。
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")
カメラパラメータへのアクセス#
次のオブジェクトを使用して、カメラのPropertyにアクセスおよび変更できます。 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()
点群の計算#
Open3Dを使用して点群を計算および可視化する例については、 ShowPointCloud.py サンプルを参照してください。
アプリケーションのデバッグ#
pypylonとStereo aceカメラの使用中に問題が発生した場合は、トラブルシューティングのトピックを確認してください。