Tricolops 4 dienas atpakaļ
vecāks
revīzija
16ccdb14b3
3 mainītis faili ar 36 papildinājumiem un 27 dzēšanām
  1. 23 14
      labelme/ai/barcode_decode.py
  2. 1 1
      labelme/utils/__init__.py
  3. 12 12
      labelme/widgets/canvas.py

+ 23 - 14
labelme/ai/barcode_decode.py

@@ -13,6 +13,8 @@ import numpy as np
 import openvino as ov
 import os.path as osp
 import cv2
+import os
+import time
 from labelme.utils import img_qt_to_arr
 from labelme.utils import load_barcode_dict
 
@@ -68,13 +70,14 @@ class BarcodeDecodeModel:
     #     self.pixmap = pixmap
     #     logger.debug("Pixmap set successfully in BarcodeDecodeModel.")
 
-    def preprocess_image(self, image):
-        norm = Normalize(mean=(0.45, 0.45, 0.45), std=(0.24, 0.24, 0.24))
-        resized_image = cv2.resize(image, (self.decoding_input_shape[3], self.decoding_input_shape[2]))
+    def preprocess_image(self, normalized_img):
+        norm = Normalize(mean=(0.45, 0.45, 0.45), std=(0.25, 0.25, 0.25))
+        resized_image = cv2.resize(normalized_img, (self.decoding_input_shape[3], self.decoding_input_shape[2]))
         resized_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)
         resized_image = norm(resized_image)
+        
         # Resize image for detection model input size
-        logger.debug(f"Preprocessing image for detection: {image.shape}")
+        logger.debug(f"Preprocessing image for detection: {normalized_img.shape}")
         # resized_image = resized_image.astype('float32') / 255.0
 
         input_tensor = resized_image.transpose(2, 0, 1)  # Convert HWC to CHW
@@ -147,7 +150,7 @@ class BarcodeDecodeModel:
 
             # Save the aligned barcode image
             # cv2.imwrite(f"decoding_barcode_{detection_idx + 1}.png", aligned_barcode)
-            logger.debug(f"Aligned barcode saved at  {detection_idx + 1}.")
+            # logger.debug(f"Aligned barcode saved at  {detection_idx + 1}.")
 
             # Normalize the image to scale pixel intensities to the range [0, 255]
             normalized_img = np.zeros(aligned_barcode.shape, aligned_barcode.dtype)
@@ -162,7 +165,7 @@ class BarcodeDecodeModel:
             # confidence = None 
             # Run decoding on the original image
             is_valid, decoded, decoded_text, msg, avg_conf, detection_idx = self.run_decoding(normalized_img, detection_idx)
-            # print(f"Valid: {is_valid}, detection_idx:{detection_idx + 1}, decoded:{decoded}, decoded_text:{decoded_text}, msg:{msg}, avg_conf:{avg_conf}")
+            print(f"Valid: {is_valid}, detection_idx:{detection_idx + 1}, decoded:{decoded}, decoded_text:{decoded_text}, msg:{msg}, avg_conf:{avg_conf}")
             
             if not is_valid:
                 logger.warning(f"Decoding failed for detection {detection_idx + 1}: {msg}. Retrying with 180° rotation")
@@ -181,18 +184,21 @@ class BarcodeDecodeModel:
             logger.error(f"Error in decode_from_points: {e}")
             return "Error: Decoding failed"
         
-    def run_decoding(self, image_np, detection_idx):
+    def run_decoding(self, normalized_img, detection_idx):
         """Helper to run decoding on the given image."""
         preprocessed_img = self.preprocess_image(
-            image_np
+            normalized_img
         )
+
+        # cv2.imwrite(f"cropped_image_decoding_normalized{detection_idx + 1}.png",normalized_img)
+
         decode_result = self.decoding_sess.infer_new_request({'x': preprocessed_img})
         output_tensor = decode_result['save_infer_model/scale_0.tmp_0']
         logger.debug(f"Output tensor shape: {output_tensor.shape}")
-
+        print(f"decode_result: {decode_result}")
         output_indices_batch = np.argmax(output_tensor, axis=2)
         output_probs_batch = np.max(output_tensor, axis=2)
-        # print(f"output_indices:{output_indices}")
+        print(f"output_probs_batch:{output_probs_batch}")
         # Decode text from indices
 
         def preprocess_output_indices(output_indices_batch, output_probs_batch):
@@ -200,6 +206,7 @@ class BarcodeDecodeModel:
             if output_indices_batch is None or len(output_indices_batch) == 0:
                 return False, "Empty output indices batch", None
 
+            print(f"output_indices_batch: {output_indices_batch}")
             first_row = output_indices_batch[0]
             first_row_probs = output_probs_batch[0]
             if first_row is None or len(first_row) == 0:
@@ -334,6 +341,7 @@ class BarcodeDecodeModel:
         logger.debug(f"raw_values passed to validate_checksum: {raw_values}")
         if raw_values[0] == 107:
             logger.debug("Ean Barcode detected")
+            
             code_map_predicted = {
                 'C1': '107', 'C2': '108', '0A': '109', '1A': '110', '2A': '111', '3A': '112', '4A': '113',
                 '5A': '114', '6A': '115', '7A': '116', '8A': '117', '9A': '118', '0B': '119', '1B': '120',
@@ -341,6 +349,7 @@ class BarcodeDecodeModel:
                 '9B': '128', '0C': '129', '1C': '130', '2C': '131', '3C': '132', '4C': '133', '5C': '134',
                 '6C': '135', '7C': '136', '8C': '137', '9C': '138'
             }
+            
             LEFT_PATTERN = (
                 "AAAAAA", "AABABB", "AABBAB", "AABBBA", "ABAABB",
                 "ABBAAB", "ABBBAA", "ABABAB", "ABABBA", "ABBABA"
@@ -349,7 +358,7 @@ class BarcodeDecodeModel:
             predicted_digits = [val for val in raw_values[1:-1] if val != 108]
             # print(f"predicted_digits: {predicted_digits}")
             mapped_keys = [reverse_code_map.get(str(val), f"UNK({val})") for val in predicted_digits]
-            # print(f"Mapped keys: {mapped_keys}")
+            logger.debug(f"Mapped keys: {mapped_keys}")
             detected_pattern = ''.join(k[-1] if len(k) == 2 else '?' for k in mapped_keys[:6])
             # print(f"Detected_pattern: {detected_pattern}")
             if detected_pattern in LEFT_PATTERN:
@@ -361,7 +370,7 @@ class BarcodeDecodeModel:
             predicted_ean_value = ''.join(k[0] if len(k) == 2 and k[0].isdigit() else '?' for k in mapped_keys)
             # print(f"predicted_ean_value:{predicted_ean_value}")
             ean13 = str(pattern_index) + predicted_ean_value[:12]
-            # print(f"ean13 base:{ean13}")
+            logger.debug(f"ean13 base:{ean13}")
             if len(ean13) != 13 or not ean13.isdigit():
                 logger.warning(f"Invalid Ean value needs to be 13 digits")
                 return False, None, "Invalid EAN-13 base", None
@@ -379,8 +388,8 @@ class BarcodeDecodeModel:
                 predicted_ean_checksum = predicted_ean_value[-1]
                 # print(f"predicted_ean_checksum:{predicted_ean_checksum}")
                 if str(predicted_ean_checksum) != str(calculated_ean_checksum):
-                    logger.warning(f"Invalid ean 13 checksum value, supposed to be {calculated_ean_checksum}")
-                    return False, None, f"Invalid ean 13 checksum: expected {calculated_ean_checksum}", None
+                    logger.warning(f"Invalid ean 13 checksum value, supposed to be {predicted_ean_checksum} but got {calculated_ean_checksum}")
+                    return False, None, f"Invalid ean 13 checksum: expected {predicted_ean_checksum}", None
                 else:
                     ean_list = [int(char) for char in ean13]
                     predicted_ean_digits_print = raw_values

+ 1 - 1
labelme/utils/__init__.py

@@ -33,7 +33,7 @@ import os
 def load_barcode_dict():
     """Load the barcode dictionary from the utils folder."""
     dict_path = os.path.join(os.path.dirname(__file__), "barcode_dict.txt")
-    # print(f"dict_path: {dict_path}")
+    print(f"dict_path: {dict_path}")
     try:
         with open(dict_path, "r", encoding="utf-8") as f:
             characters = f.read().splitlines()

+ 12 - 12
labelme/widgets/canvas.py

@@ -863,17 +863,17 @@ class Canvas(QtWidgets.QWidget):
         if(self.current is None):
             return
             # If in manual mode and points are finalized
-        if self.createMode in ["polygon", "rectangle"] and len(self.current.points) > 0:
-            if self._detection_model is None:
-                logger.info(f"Initializing AI model")
-                self.initializeBarcodeModel(self.detection_model_path, self.segmentation_model_path, self.decoding_model_path)
-            # Extract the points from the manually drawn shape
-            manual_points = [[point.x(), point.y()] for point in self.current.points]
-            print(manual_points)
-            logger.debug(f"Manually drawn points: {manual_points}")
-            self.detect_and_segment(manual_points=manual_points)
-            # self.mode = self.EDIT  # Or any appropriate mode to disable drawing behavior
-            return
+        # if self.createMode in ["polygon", "rectangle"] and len(self.current.points) > 0:
+        #     if self._detection_model is None:
+        #         logger.info(f"Initializing AI model")
+        #         self.initializeBarcodeModel(self.detection_model_path, self.segmentation_model_path, self.decoding_model_path)
+        #     # Extract the points from the manually drawn shape
+        #     manual_points = [[point.x(), point.y()] for point in self.current.points]
+        #     print(manual_points)
+        #     logger.debug(f"Manually drawn points: {manual_points}")
+        #     self.detect_and_segment(manual_points=manual_points)
+        #     # self.mode = self.EDIT  # Or any appropriate mode to disable drawing behavior
+        #     return
         if self.createMode == "ai_polygon":
             # convert points to polygon by an AI model
             assert self.current.shape_type == "points"
@@ -1117,7 +1117,7 @@ class Canvas(QtWidgets.QWidget):
         # logger.debug(f"scaled points: {scaled_points}")
         return scaled_points
     
-    def expand_bbox(self, x_min, y_min, x_max, y_max, factor=1.3):
+    def expand_bbox(self, x_min, y_min, x_max, y_max, factor=1.5):
         """
         Expands the bounding box by a given factor.
         """