Преглед изворни кода

添加 样签OCR测试脚本

zeke-chin пре 2 година
родитељ
комит
bb979b6763

+ 0 - 0
TestAllOcr/config.py → HR-OCR/TestAllOcr/config.py


+ 0 - 0
TestAllOcr/image/bankcard.jpg → HR-OCR/TestAllOcr/image/bankcard.jpg


+ 0 - 0
TestAllOcr/image/blfe.jpg → HR-OCR/TestAllOcr/image/blfe.jpg


+ 0 - 0
TestAllOcr/image/cet.jpg → HR-OCR/TestAllOcr/image/cet.jpg


+ 0 - 0
TestAllOcr/image/idcard.jpg → HR-OCR/TestAllOcr/image/idcard.jpg


+ 0 - 0
TestAllOcr/image/regbook.jpg → HR-OCR/TestAllOcr/image/regbook.jpg


+ 0 - 0
TestAllOcr/image/schoolcert.jpg → HR-OCR/TestAllOcr/image/schoolcert.jpg


+ 6 - 7
TestAllOcr/test_interface.py → HR-OCR/TestAllOcr/test_interface.py

@@ -1,4 +1,3 @@
-import base64
 from pathlib import Path
 import unittest
 import config
@@ -21,31 +20,31 @@ class TestOcr(unittest.TestCase):
         self.assertIn('result', r, f'{suffix} test error')
 
     def test_bank(self):
-        image_path = './image/bankcard.jpg'
+        image_path = 'image/bankcard.jpg'
         suffix = '/yhksb/bankcard' if 'sb' in envl else '/yhksbtest/bankcard'
         self._helper(image_path, suffix)
 
     def test_idcard(self):
-        image_path = './image/idcard.jpg'
+        image_path = 'image/idcard.jpg'
         suffix = '/sfzsb/idcard' if 'sb' in envl else '/sfzsbtest/idcard'
         self._helper(image_path, suffix)
 
     def test_regbook(self):
-        image_path = './image/regbook.jpg'
+        image_path = 'image/regbook.jpg'
         suffix = '/hkbsb/regbook' if 'sb' in envl else '/hkbsbtest/regbook'
         self._helper(image_path, suffix)
 
     def test_schoolcert(self):
-        image_path = './image/schoolcert.jpg'
+        image_path = 'image/schoolcert.jpg'
         suffix = '/xxw/schoolcert' if 'sb' in envl else '/xxwtest/schoolcert'
         self._helper(image_path, suffix)
 
     def test_cet(self):
-        image_path = './image/cet.jpg'
+        image_path = 'image/cet.jpg'
         suffix = '/cet/cet' if 'sb' in envl else '/cettest/cet'
         self._helper(image_path, suffix)
 
     def test_blef(self):
-        image_path = './image/blfe.jpg'
+        image_path = 'image/blfe.jpg'
         suffix = '/blfe/blfe' if 'sb' in envl else '/blfetest/blfe'
         self._helper(image_path, suffix)

+ 0 - 0
HR-OCR/config.py


+ 0 - 0
to_md/new.py → HR-OCR/to_md/new.py


+ 0 - 0
to_md/ocr_config.py → HR-OCR/to_md/ocr_config.py


+ 0 - 0
to_md/use.py → HR-OCR/to_md/use.py


+ 0 - 0
tools/convert_json.py → HR-OCR/tools/convert_json.py


+ 0 - 0
tools/suffix.py → HR-OCR/tools/suffix.py


BIN
YQ-OCR/img/03-植选PET 内包—植选豆乳以团之名形象定制包装周艺轩版.jpg


BIN
YQ-OCR/img/03-植选PET 内包—植选豆乳以团之名形象定制包装周艺轩版.xlsx


BIN
YQ-OCR/img/巧克力味牛奶饮品.jpg


BIN
YQ-OCR/img/巧克力味牛奶饮品.xlsx


BIN
YQ-OCR/img/餐饮纯牛奶 内包.jpg


BIN
YQ-OCR/img/餐饮纯牛奶 内包.xlsx


+ 11 - 0
YQ-OCR/to_md/config.py

@@ -0,0 +1,11 @@
+keyDict = {
+    "productCategory": '产品种类',
+    "ingredients": '配料',
+    "proStanCode": '产品标准代号',
+    "productionDate": '生产日期',
+    "shelfLife": '保质期',
+    "storageConditions": '贮存条件',
+    "conSerHotline": '消费者服务热线',
+    "tips": '温馨提示|友情提示',
+    "welcome": '欢迎访问'
+}

+ 118 - 0
YQ-OCR/to_md/convert_MD.py

@@ -0,0 +1,118 @@
+import re
+from itertools import chain
+from pathlib import Path
+
+import numpy as np
+import pandas as pd
+import json
+from mdutils.mdutils import MdUtils
+import requests
+
+from config import keyDict
+
+url = 'http://192.168.199.107:18087'
+url_path = '/ocr_system/identify'
+imgs_path = '/Users/zeke/work/sx/OCR/image_data/样签图片'
+
+
+# 1. xlsx -> 正确json文件(写入厂家信息)
+# 2. 发送图片(带正确json文件)
+# 3. 把返回的json 和正确的json 进行对比(有key--用返回结果与正确结果比对,无key--用正确结果与返回结果比对)
+
+# 编辑距离
+def Levenshtein_Distance(str1, str2):
+    matrix = [[i + j for j in range(len(str2) + 1)] for i in range(len(str1) + 1)]
+    for i in range(1, len(str1) + 1):
+        for j in range(1, len(str2) + 1):
+            d = 0 if (str1[i - 1] == str2[j - 1]) else 1
+            matrix[i][j] = min(matrix[i - 1][j] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j - 1] + d)
+    return matrix[len(str1)][len(str2)]
+
+
+# 发送请求 带正确答案参数
+def send_request(img_path: Path, img_json: str):
+    file = {'file': (img_path.name, open(img_path, 'rb'), img_path)}
+    payload = {'docDataStr': img_json}
+    r = requests.post(url + url_path, files=file, data=payload)
+    return r.json()
+
+
+# 处理返回结果
+def _parse_result(r):  # sourcery skip: dict-comprehension
+    if r['status'] == '000':
+        result = r['result']
+        res = {}
+        for field in keyDict:
+            if field in result:
+                res[field] = result[field]
+        res['noKeyList'] = result['noKeyList']
+        res['logoList'] = result['logoList']
+        logoFileName = [log['logoFileName'] for log in res['logoList']]
+        res['logoList'] = logoFileName
+        return res
+    elif r['status'] == '101':
+        return "101"
+
+
+# 比较两个json文件 并在md文件中写入对比结果
+def evaluate_one(xlsx_dict, res_dict):
+    true_num = 0
+    for key_yes in res_dict:
+        if type(res_dict[key_yes]) is str:
+            if Levenshtein_Distance(res_dict[key_yes], xlsx_dict[key_yes]) == 0:
+                table_result.extend([key_yes, xlsx_dict[key_yes], res_dict[key_yes], '✅'])
+                true_num += 1
+            else:
+                table_result.extend([key_yes, xlsx_dict[key_yes], res_dict[key_yes], '❌'])
+
+    key_no_dict = {}
+    for key_no_xlsx in xlsx_dict['noKeyList']:
+        key_no_dict[key_no_xlsx] = []
+        for key_no_res in res_dict['noKeyList']:
+            key_no_dict[key_no_xlsx].append((Levenshtein_Distance(key_no_xlsx, key_no_res), key_no_res))
+        sort_NoKey = sorted(key_no_dict[key_no_xlsx], key=lambda x: x[0])
+        NoKey_min_distance = sort_NoKey[0][0]
+        if NoKey_min_distance == 0:
+            table_result.extend(['无key值', key_no_xlsx, sort_NoKey[0][1], '✅'])
+            true_num += 1
+        else:
+            table_result.extend(['无key值', key_no_xlsx, sort_NoKey[0][1], '❌'])
+    rate = true_num / (len(table_result) / 4)
+    all_rate.append(rate)
+    statistics = f'共{len(table_result) // 4}个字段,正确{true_num}个,错误{len(table_result) // 4 - true_num}个'
+
+    return "{:.2f}%".format(rate * 100), statistics
+
+
+# 打开正确的json文件
+def open_true_json(j_path):
+    with j_path.open('r') as f:
+        j_dict = json.load(f)
+        j_json_str = json.dumps(j_dict, ensure_ascii=False)
+        return j_dict, j_json_str
+
+
+if __name__ == '__main__':
+    img_paths = chain(*[Path(imgs_path).rglob(f'*.{ext}') for ext in ['jpg', 'png', 'jpeg', 'PNG', 'JPG', 'JPEG']])
+    all_rate = []
+    for img_path in img_paths:
+        print(img_path)
+        # json result
+        true_d, true_json = open_true_json(img_path.with_suffix('.json'))
+        result = send_request(img_path, true_json)
+        res_d = _parse_result(result)
+        # md
+        md_file_path = img_path.parent / (img_path.with_suffix('.md'))
+        MD = MdUtils(file_name=str(md_file_path))
+        table_result = ['key值', '正确答案', 'ocr返回答案', '是否正确']
+        rate, statistics = evaluate_one(true_d, res_d)
+        MD.new_header(level=1, title='测试结果')
+        MD.new_header(level=2, title=f'正确率:{rate}')
+        MD.new_header(level=3, title=statistics)
+        print(f'正确率:{rate}')
+        MD.new_table(columns=4, rows=len(table_result) // 4, text=table_result, text_align='center')
+        MD.create_md_file()
+
+    print('-------------------------------')
+    all_rate = "{:.2f}%".format(np.mean(all_rate) * 100)
+    print(f'总体正确率:{all_rate}')

+ 57 - 0
YQ-OCR/to_md/xlsx_convert_json.py

@@ -0,0 +1,57 @@
+import json
+import re
+from itertools import chain
+from pathlib import Path
+
+import pandas as pd
+
+from config import keyDict
+
+# 把xlsx转成json
+
+
+excels_path = '/Users/zeke/work/sx/OCR/image_data/样签图片'
+
+
+# 返回文档里所以所需识别字符串
+def get_xlsx_str_list(xlsx_path):
+    workbook_pd_Common = pd.read_excel(xlsx_path, sheet_name="Common")
+    workbook_pd_Packing = pd.read_excel(xlsx_path, sheet_name="Packing")
+    Common_list = list(workbook_pd_Common['内容'].values)
+    for index, text in enumerate(Common_list):
+        Common_list[index] = text.replace('\n', '').replace(' ', '')
+    Packing_list = workbook_pd_Packing.values.tolist()[0]
+    str_list = Common_list
+    str_list.append(str(Packing_list[0]))
+    str_list.append(str(Packing_list[2]))
+    return str_list
+
+
+# 将list 转化为 dict
+def xlsx_list_2_dict(xlsx_list):
+    xlsx_dict = {}
+    for k, v in keyDict.items():
+        for x_str in xlsx_list:
+            if bool(re.match(v, x_str)):
+                xlsx_dict[k] = x_str
+                xlsx_list.remove(x_str)
+    xlsx_dict['noKeyList'] = xlsx_list
+    return xlsx_dict
+
+
+# 解析文档 返回json
+def get_true_json(xlsx_path):  # sourcery skip: inline-immediately-returned-variable
+    xlsx_list = get_xlsx_str_list(xlsx_path)
+    xlsx_dict = xlsx_list_2_dict(xlsx_list)
+    return xlsx_dict
+
+
+if __name__ == '__main__':
+    excel_paths = chain(*[Path(excels_path).rglob('*.xlsx')])
+
+    for excel_path in excel_paths:
+        print(excel_path)
+        true_json = get_true_json(excel_path)
+        json_path = Path(excel_path).with_suffix('.json')
+        with json_path.open('w', encoding='utf-8') as f:
+            json.dump(true_json, f, ensure_ascii=False, indent=4)