import cv2 from pylibdmtx.pylibdmtx import decode import logging from pathlib import Path logger = logging.getLogger(__name__) class TestCamera: def __init__(self, test_files_path: str): self.image_files = [] self.current_index = 0 # Load all image files from the test directory path = Path(test_files_path) if path.exists() and path.is_dir(): self.image_files = sorted([ str(f) for f in path.glob("*") if f.suffix.lower() in ['.png', '.jpg', '.jpeg'] ]) if not self.image_files: logger.warning(f"No image files found in {test_files_path}") else: logger.error(f"Test files directory not found: {test_files_path}") def read(self): if not self.image_files: return False, None # Read next image file in sequence image_path = self.image_files[self.current_index] frame = cv2.imread(image_path) if frame is None: logger.error(f"Failed to load image: {image_path}") return False, None # Cycle through available images self.current_index = (self.current_index + 1) % len(self.image_files) return True, frame def release(self): pass class DataMatrixReader: def __init__(self, camera_id: int = 0): self.camera_id = camera_id self.camera = None def initialize(self): if self.camera_id < 0: test_path = "./tests/files/" self.camera = TestCamera(test_path) else: self.camera = cv2.VideoCapture(self.camera_id) def read_datamatrix(self) -> str: ret, frame = self.camera.read() if not ret: raise Exception("Failed to capture image") decoded = decode(frame) if not decoded: logger.warning("No datamatrix found") return None return decoded[0].data.decode('utf-8') def cleanup(self): if self.camera: self.camera.release()