pupilometer/tests/test_camera_integration.py
2025-11-28 16:18:30 +07:00

53 lines
1.7 KiB
Python

import pytest
from pypylon import pylon
import cv2
@pytest.mark.usefixtures("camera_available")
def test_capture_photo():
"""
Tests that a photo can be captured from the Basler camera.
This test depends on the `camera_available` fixture in conftest.py.
"""
try:
# Get the transport layer factory.
tl_factory = pylon.TlFactory.GetInstance()
# Get all attached devices and exit application if no device is found.
devices = tl_factory.EnumerateDevices()
# Only grab from the first camera found
camera = pylon.InstantCamera(tl_factory.CreateDevice(devices[0]))
camera.Open()
# Max number of images to grab
countOfImagesToGrab = 1
# Create an image format converter
converter = pylon.ImageFormatConverter()
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
# Start grabbing continuously
camera.StartGrabbingMax(countOfImagesToGrab)
img = None
while camera.IsGrabbing():
grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
if grabResult.GrabSucceeded():
# Access the image data
image = converter.Convert(grabResult)
img = image.GetArray()
grabResult.Release()
camera.Close()
assert img is not None, "Failed to capture an image."
assert img.shape[0] > 0, "Captured image has zero height."
assert img.shape[1] > 0, "Captured image has zero width."
except Exception as e:
pytest.fail(f"An error occurred during photo capture: {e}")