Explorar o código

Add test camera and make vision test work

Silas Gruen hai 11 meses
pai
achega
605d74f48d

+ 3 - 2
.gitignore

@@ -1,4 +1,5 @@
-*.cpython-39.pyc
+.cpython-39.pyc
 .VSCodeCounter/
-*.pyc
+.pyc
 robot-control/src/robot_control.egg-info/
+__pycache__

+ 42 - 2
robot-control/src/vision/datamatrix.py

@@ -1,17 +1,57 @@
 import cv2
 from pylibdmtx.pylibdmtx import decode
 import logging
+import os
+from pathlib import Path
 
-# TODO[SG]: add real datamatrix code. This is just a placeholder.
 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):
-        self.camera = cv2.VideoCapture(self.camera_id)
+        if self.camera_id < 0:
+            test_path = "robot-control/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()

BIN=BIN
robot-control/tests/files/test_matrix.jpg


BIN=BIN
robot-control/tests/files/test_matrix_fail.jpg


+ 12 - 7
robot-control/tests/test_vision.py

@@ -4,17 +4,22 @@ import numpy as np
 
 @pytest.fixture
 def datamatrix_reader():
+    return DataMatrixReader(-1)
+@pytest.fixture
+def datamatrix_reader_realcam():
     return DataMatrixReader()
 
 class TestDataMatrixReader:
-    def test_init_reader(self, datamatrix_reader: DataMatrixReader):
-        assert datamatrix_reader is not None
+    def test_init_reader(self, datamatrix_reader_realcam: DataMatrixReader):
+        assert datamatrix_reader_realcam is not None
 
-    def test_read_failure(self, datamatrix_reader: DataMatrixReader):
+    def test_read_success(self, datamatrix_reader: DataMatrixReader):
         datamatrix_reader.initialize()
         result = datamatrix_reader.read_datamatrix()
-        assert result is None
+        assert result == "https://batteries.up-cell.de/cells/2224"
 
-    def test_read_success(self, datamatrix_reader: DataMatrixReader):
-        #TDOD[SG]: Add real test data
-        pass
+    def test_read_failure(self, datamatrix_reader: DataMatrixReader):
+        datamatrix_reader.initialize()
+        result = datamatrix_reader.read_datamatrix() # first is success
+        result = datamatrix_reader.read_datamatrix() # second is failure
+        assert result is None