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