From 8ace9df86af13cb299fe551f7dcae25f11687bee Mon Sep 17 00:00:00 2001 From: Viajero <2737079298@qq.com> Date: Tue, 16 Sep 2025 11:15:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OCR_part/ocr_interface.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/OCR_part/ocr_interface.py b/OCR_part/ocr_interface.py index b98c5b8..75770c0 100644 --- a/OCR_part/ocr_interface.py +++ b/OCR_part/ocr_interface.py @@ -5,6 +5,18 @@ import cv2 class OCRProcessor: def __init__(self): self.model = TextRecognition(model_name="PP-OCRv5_server_rec") + # 定义允许的字符集合(不包含空白字符) + self.allowed_chars = [ + # 中文省份简称 + '京', '沪', '津', '渝', '冀', '晋', '蒙', '辽', '吉', '黑', + '苏', '浙', '皖', '闽', '赣', '鲁', '豫', '鄂', '湘', '粤', + '桂', '琼', '川', '贵', '云', '藏', '陕', '甘', '青', '宁', '新', + # 字母 A-Z + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + # 数字 0-9 + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' + ] print("OCR模型初始化完成(占位)") def predict(self, image_array): @@ -14,6 +26,14 @@ class OCRProcessor: results = output[0]["rec_text"] placeholder_result = results.split(',') return placeholder_result + + def filter_allowed_chars(self, text): + """只保留允许的字符""" + filtered_text = "" + for char in text: + if char in self.allowed_chars: + filtered_text += char + return filtered_text # 保留原有函数接口 _processor = OCRProcessor() @@ -42,8 +62,12 @@ def LPRNmodel_predict(image_array): else: result_str = str(raw_result) - # 过滤掉'·'字符 + # 过滤掉'·'和'-'字符 filtered_str = result_str.replace('·', '') + filtered_str = filtered_str.replace('-', '') + + # 只保留允许的字符 + filtered_str = _processor.filter_allowed_chars(filtered_str) # 转换为字符列表 char_list = list(filtered_str)