サンプルコード#
単一画像の静止画取得#
1枚の画像を取り込んで処理する。
from pypylon import pylon
def process(image):
print(image.shape)
with pylon.InstantCamera(pylon.FirstFound) as camera:
camera.StartGrabbingMax(1)
with camera.RetrieveResult(5000) as grab_result:
if grab_result.GrabSucceeded():
image = grab_result.Array
process(image)
連続アクキジションループ#
画像を連続的に処理する。
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
while camera.IsGrabbing():
with camera.RetrieveResult(5000) as grab_result:
if grab_result.GrabSucceeded():
process(grab_result.Array)
ディスクへの画像の保存#
ソフトウェアトリガーの設定#
pylon.SoftwareTriggerConfiguration.ApplyConfiguration(camera.NodeMap)
camera.ExecuteSoftwareTrigger()
ハードウェアトリガーの設定#
pylon.ConfigurationHelper.DisableAllTriggers(camera.NodeMap)
camera.TriggerSelector.Value = "FrameStart"
camera.TriggerMode.Value = "On"
camera.TriggerSource.Value = "Line1"
露光時間とゲインの設定#
camera.ExposureAuto.Value = "Off"
camera.ExposureTime.Value = 3000
camera.GainAuto.Value = "Off"
camera.Gain.Value = 5
関心領域(ROI)の指定#
詳細については、Configuring Camera Parametersを参照してください。
カメラから左上の隅を取得する。
camera.StopGrabbing()
camera.Width.Value = 640
camera.Height.Value = 480
camera.OffsetX.Value = 0
camera.OffsetY.Value = 0
camera.StartGrabbing()
カメラから中央の領域を取得する。
camera.StopGrabbing()
camera.Width.Value = 640
camera.Height.Value = 480
camera.OffsetX.Value = (camera.MaxWidth - camera.Width) / 2
camera.OffsetY.Value = (camera.MaxHeight - camera.Height) / 2
camera.StartGrabbing()
画像をOpenCVフォーマットに変換する#
converter = pylon.ImageFormatConverter()
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
image = converter.Convert(grab_result).GetArray()
OpenCVで画像を表示する#
情報
または、次を使用することもできます。 pylonDisplay 画像関数。
タイムアウト処理付きのキャプチャ#
from pypylon import pylon
try:
with camera.RetrieveResult(
1000,
pylon.TimeoutHandling_ThrowException
) as grab_result:
if grab_result.GrabSucceeded():
process(grab_result.Array)
except Exception as e:
log_error(e)
マルチカメラ設定#
を使用して、 複数のカメラ を効率的に処理するには、 InstantCameraArrayを使用するのが最適です。これにより、1つのループ内ですべてのカメラからのグラブが可能になります。
from pypylon import pylon
RETRIEVE_TIMEOUT_MS = 5000
factory = pylon.TlFactory.GetInstance()
devices = factory.EnumerateDevices()
camera_count = len(devices)
with pylon.InstantCameraArray(camera_count) as cameras:
# Attach devices
for i, camera in enumerate(cameras):
camera.Attach(factory.CreateDevice(devices[i]))
print("Using device:", camera.DeviceInfo.ModelName)
# Start acquisition
cameras.StartGrabbing()
while cameras.IsGrabbing():
with cameras.RetrieveResult(
RETRIEVE_TIMEOUT_MS,
pylon.TimeoutHandling_ThrowException
) as grab_result:
if grab_result.GrabSucceeded():
# Identify source camera
camera_index = grab_result.CameraContext
camera_name = cameras[camera_index].DeviceInfo.ModelName
# Access image data (GenDC-safe)
image = grab_result.Array
print(
f"Camera {camera_index}: {camera_name}"
f" SizeX: {grab_result.Width} SizeY: {grab_result.Height} "
f" First pixel: {image[0, 0]}"
)
else:
print("Error:", grab_result.ErrorCode, grab_result.ErrorDescription)
スレッド処理#
import threading
import queue
image_queue = queue.Queue()
def grab_loop():
while camera.IsGrabbing():
with camera.RetrieveResult(5000) as grab_result:
if grab_result.GrabSucceeded():
image_queue.put(grab_result.Array)
def process_loop():
while True:
image = image_queue.get()
process(image)
image_queue.task_done()
安全なシャットダウンパターン#
import threading
stop_event = threading.Event()
while not stop_event.is_set():
# processing loop
pass
stop_event.set()
camera.StopGrabbing()
ビデオの記録#
import cv2
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter("output.avi", fourcc, 30, (640, 480))
out.write(image)
基本処理の例(ROI + 判定)#
import numpy as np
roi = image[100:200, 200:300]
mean = np.mean(roi)
if mean > 100:
print("OK")
else:
print("NOK")
主なポイント#
- サンプルコードにより、開発をスピードアップできます。
- コードスニペットを組み合わせて、より複雑なアプリケーションを構築します。
- 特定のユースケースに合わせてサンプルコードを適応させます。