import unittest import base64 from dataclasses import dataclass from pathlib import Path import requests url = 'http://localhost:8080' # 带线框识别比较差 例:6.27_02.png def send_request(image_path, image_type): with open(image_path, 'rb') as f: img_str: str = base64.encodebytes(f.read()).decode('utf-8') r = requests.post(f'{url}/ocr_system/schoolcert', json={'image': img_str, 'image_type': image_type}) print(r.json()) return r.json() @dataclass class ResultItem: status: str orientation: int name: str gender: str admission_time: str education_time: str education_level: str education_type: str learning_type: str school: str major: str number: str class TestSchoolCertOcr(unittest.TestCase): def _helper(self, image_path, item: ResultItem, image_type='0'): root = Path(__file__).parent image_path = str(root / image_path) r = send_request(image_path, image_type) self.assertEqual(item, ResultItem(status=r['status'], orientation=r['result']['orientation'], name=r['result']['name']['text'], gender=r['result']['gender']['text'], admission_time=r['result']['admission_time']['text'], education_time=r['result']['education_time']['text'], education_type=r['result']['education_type']['text'], education_level=r['result']['education_level']['text'], learning_type=r['result']['learning_type']['text'], school=r['result']['school']['text'], major=r['result']['major']['text'], number=r['result']['number']['text'])) def test_case_01_0(self): image_path = '../images/0/10050464A11学籍学历证明001.JPG' self._helper(image_path, ResultItem(status='000', orientation=0, name='郑思龙', gender='男', admission_time='2019年09月01日', education_time='2022年07月01日', education_level='专科', education_type='普通高等教育', learning_type='普通全日制', school='黑龙江农垦职业学院', major='食品加工技术', number='')) def test_case_02_0(self): image_path = '../images/0/10051793A11学籍学历证明001.JPG' self._helper(image_path, ResultItem(status='000', orientation=0, name='白传旭', gender='男', admission_time='2019年09月01日', education_time='2022年07月01日', education_level='专科', education_type='普通高等教育', learning_type='普通全日制', school='黑龙江民族职业学院', major='食品检测技术', number='')) def test_case_03_0(self): image_path = '../images/0/10051795A11学籍学历证明001.JPG' self._helper(image_path, ResultItem(status='000', orientation=0, name='冯鹏宇', gender='男', admission_time='2019年09月01日', education_time='2022年07月01日', education_level='专科', education_type='普通高等教育', learning_type='普通全日制', school='黑龙江民族职业学院', major='食品加工技术', number='')) def test_case_04_0(self): image_path = '../images/0/10052117A11学籍学历证明001.JPG' self._helper(image_path, ResultItem(status='000', orientation=0, name='刘泽瑾', gender='女', admission_time='2017年09月01日', education_time='2021年07月01日', education_level='本科', education_type='普通高等教育', learning_type='普通全日制', school='华南理工大学', major='食品质量与安全', number='')) def test_case_05_0(self): image_path = '../images/0/10052164A11学籍学历证明001.JPG' self._helper(image_path, ResultItem(status='000', orientation=0, name='蒋嘉璐', gender='男', admission_time='2019年09月01日', education_time='2022年07月01日', education_level='专科', education_type='普通高等教育', learning_type='普通全日制', school='黑龙江民族职业学院', major='食品加工技术', number='')) def test_case_06_0(self): image_path = '../images/0/10052165A11学籍学历证明001.JPG' self._helper(image_path, ResultItem(status='000', orientation=0, name='班玉雪', gender='女', admission_time='2019年09月01日', education_time='2022年07月01日', education_level='专科', education_type='普通高等教育', learning_type='普通全日制', school='黑龙江民族职业学院', major='食品质量与安全', number='')) def test_case_07_0(self): image_path = '../images/0/10055552A11学籍学历证明001.JPG' self._helper(image_path, ResultItem(status='000', orientation=0, name='高健', gender='男', admission_time='2020年09月01日', education_time='2022年06月30日', education_level='本科', education_type='普通高等教育', learning_type='普通全日制', school='武汉生物工程学院', major='机械设计制造及其自动化', number=''))