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='1'): 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='1'): 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/01_0.png' self._helper(image_path, ResultItem(status='000', orientation=0, name='肖亮', gender='男', admission_time='2012年9月8日', education_time='2018年6月28日', education_level='本科', education_type='普通高等教育', learning_type='普通全日制', school='济南大学泉城学院', major='市场营销', number='140021201805000882')) # 方向、姓名错误 def test_case_01_90(self): image_path = '../images/01_90.png' self._helper(image_path, ResultItem(status='000', orientation=1, name='肖亮', gender='男', admission_time='2012年9月8日', education_time='2018年6月28日', education_level='本科', education_type='普通高等教育', learning_type='普通全日制', school='济南大学泉城学院', major='市场营销', number='140021201805000882')) # 方向、姓名错误 def test_case_01_180(self): image_path = '../images/01_180.png' self._helper(image_path, ResultItem(status='000', orientation=2, name='肖亮', gender='男', admission_time='2012年9月8日', education_time='2018年6月28日', education_level='本科', education_type='普通高等教育', learning_type='普通全日制', school='济南大学泉城学院', major='市场营销', number='140021201805000882')) # 方向、姓名错误 def test_case_01_270(self): image_path = '../images/01_270.png' self._helper(image_path, ResultItem(status='000', orientation=3, name='肖亮', gender='男', admission_time='2012年9月8日', education_time='2018年6月28日', education_level='本科', education_type='普通高等教育', learning_type='普通全日制', school='济南大学泉城学院', major='市场营销', number='140021201805000882')) # 姓名错误 def test_case_02(self): image_path = '../images/02.png' self._helper(image_path, ResultItem(status='000', orientation=0, name='覃俊', gender='男', admission_time='2000年9月1日', education_time='2004年6月21日', education_level='本科', education_type='普通', learning_type='普通全日制', school='江苏科技大学', major='会计学', number='102891200405001172')) # 方向、姓名错误 def test_case_04(self): image_path = '../images/04.jpeg' self._helper(image_path, ResultItem(status='000', orientation=0, name='汤怡青', gender='女', admission_time='2010年09月01日', education_time='2014年06月28日', education_level='本科', education_type='普通高等教育', learning_type='普通全日制', school='北京交通大学', major='软件工程', number='100041201405003059')) # 方向、姓名错误 def test_629_01(self): image_path = '../images/6.29_01.jpg' self._helper(image_path, ResultItem(status='000', orientation=0, name='刘杰', gender='男', admission_time='2011年09月01日', education_time='2015年06月25日', education_level='本科', education_type='普通高等教育', learning_type='普通全日制', school='山东理工大学', major='英语(师范类)', number='104331201505208429')) # 姓名错误 def test_629_02(self): image_path = '../images/6.29_02.png' self._helper(image_path, ResultItem(status='000', orientation=0, name='张开天', gender='男', admission_time='2017年09月01日', education_time='2021年06月15日', education_level='本科', education_type='普通高等教育', learning_type='普通全日制', school='甘肃农业大学', major='农林经济管理', number='622301199710247376')) # 入学时间、毕业时间、教育类型错误 def test_629_03(self): image_path = '../images/6.29_03.jpg' self._helper(image_path, ResultItem(status='000', orientation=0, name='陈贵龙', gender='男', admission_time='2019年09月08日', education_time='2023年06月30日', education_level='本科', education_type='普通高等教育', learning_type='普通全日制', school='浙江树人学院', major='通信工程', number='342423200102156552'))