import unittest import base64 from dataclasses import dataclass from pathlib import Path import cv2 import requests url = 'http://localhost:8080' def send_request(image_path, image_type, rotate=None): img = cv2.imread(str(image_path)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) if rotate is not None: img = cv2.rotate(img, rotate) _, im_arr = cv2.imencode('.jpg', img) img_str = base64.b64encode(im_arr).decode('utf-8') r = requests.post(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, rotate=None,image_type=1): root = Path(__file__).parent image_path = str(root / image_path) r = send_request(image_path, image_type, rotate) 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 case_02_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/02_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '徐科', 'gender': '男', 'admission_time': '2010年09月01日', 'education_time': '2013年06月30日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '武汉职业技术学院', 'major': '摄影摄像技术', 'number': '108341201306417803'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_02_0(self): self.case_02_0(0) def test_case_02_1(self): self.case_02_0(1) def test_case_02_2(self): self.case_02_0(2) def test_case_02_3(self): self.case_02_0(3) def case_17_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/17_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '陈默涵', 'gender': '女', 'admission_time': '2018年09月05日', 'education_time': '2021年06月25日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '新疆农业职业技术学院', 'major': '食品营养与检测', 'number': '109951202106002702'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_17_0(self): self.case_17_0(0) def test_case_17_1(self): self.case_17_0(1) def test_case_17_2(self): self.case_17_0(2) def test_case_17_3(self): self.case_17_0(3) def case_12_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/12_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '张聪', 'gender': '男', 'admission_time': '2008年09月01日', 'education_time': '2011年06月30日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '湖北工业大学商贸学院', 'major': '计算机应用技术', 'number': '132471201106331235'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_12_0(self): self.case_12_0(0) def test_case_12_1(self): self.case_12_0(1) def test_case_12_2(self): self.case_12_0(2) def test_case_12_3(self): self.case_12_0(3) def case_06_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/06_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '李婷婷', 'gender': '女', 'admission_time': '2015年09月01日', 'education_time': '2019年07月01日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '广西财经学院', 'major': '商务英语', 'number': '115481201905007155'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_06_0(self): self.case_06_0(0) def test_case_06_1(self): self.case_06_0(1) def test_case_06_2(self): self.case_06_0(2) def test_case_06_3(self): self.case_06_0(3) def case_04_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/04_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '卢超', 'gender': '男', 'admission_time': '2013年09月01日', 'education_time': '2016年06月30日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '鄂东职业技术学院', 'major': '建筑工程技术', 'number': '137971201606511857'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_04_0(self): self.case_04_0(0) def test_case_04_1(self): self.case_04_0(1) def test_case_04_2(self): self.case_04_0(2) def test_case_04_3(self): self.case_04_0(3) def case_07_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/07_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '罗建亮', 'gender': '男', 'admission_time': '2008年11月01日', 'education_time': '2011年07月05日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '百色职业学院', 'major': '机电一体化技术', 'number': '140681201106000733'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_07_0(self): self.case_07_0(0) def test_case_07_1(self): self.case_07_0(1) def test_case_07_2(self): self.case_07_0(2) def test_case_07_3(self): self.case_07_0(3) def case_05_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/05_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '苏丽欢', 'gender': '男', 'admission_time': '2012年09月01日', 'education_time': '2015年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '广西民族师范学院', 'major': '汉语', 'number': '106041201506002280'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_05_0(self): self.case_05_0(0) def test_case_05_1(self): self.case_05_0(1) def test_case_05_2(self): self.case_05_0(2) def test_case_05_3(self): self.case_05_0(3) def case_14_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/14_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '郭伟', 'gender': '男', 'admission_time': '2004年09月14日', 'education_time': '2007年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '山东劳动职业技术学院', 'major': '机械制造工艺及设备', 'number': '123291200706402393'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_14_0(self): self.case_14_0(0) def test_case_14_1(self): self.case_14_0(1) def test_case_14_2(self): self.case_14_0(2) def test_case_14_3(self): self.case_14_0(3) def case_10_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/10_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '韩昊哲', 'gender': '男', 'admission_time': '2017年09月01日', 'education_time': '2020年07月15日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '西安交通工程学院', 'major': '铁道供电技术', 'number': '135691202006002464'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_10_0(self): self.case_10_0(0) def test_case_10_1(self): self.case_10_0(1) def test_case_10_2(self): self.case_10_0(2) def test_case_10_3(self): self.case_10_0(3) def case_08_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/08_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '罗华鼎', 'gender': '男', 'admission_time': '2015年09月15日', 'education_time': '2018年06月30日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '广西交通职业技术学院', 'major': '建筑设计技术', 'number': '123561201806001431'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_08_0(self): self.case_08_0(0) def test_case_08_1(self): self.case_08_0(1) def test_case_08_2(self): self.case_08_0(2) def test_case_08_3(self): self.case_08_0(3) def case_13_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/13_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '郑红超', 'gender': '男', 'admission_time': '2011年09月01日', 'education_time': '2014年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '漯河职业技术学院', 'major': '市场营销', 'number': '108351201406002687'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_13_0(self): self.case_13_0(0) def test_case_13_1(self): self.case_13_0(1) def test_case_13_2(self): self.case_13_0(2) def test_case_13_3(self): self.case_13_0(3) def case_11_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/11_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '张广渊', 'gender': '女', 'admission_time': '2007年09月01日', 'education_time': '2011年06月30日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '武汉工业学院工商学院', 'major': '国际经济与贸易', 'number': '132411201105872980'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_11_0(self): self.case_11_0(0) def test_case_11_1(self): self.case_11_0(1) def test_case_11_2(self): self.case_11_0(2) def test_case_11_3(self): self.case_11_0(3) def case_09_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/09_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '段有知', 'gender': '男', 'admission_time': '2014年09月01日', 'education_time': '2018年06月30日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '武汉理工大学', 'major': '测控技术与仪器', 'number': '104971201805735616'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_09_0(self): self.case_09_0(0) def test_case_09_1(self): self.case_09_0(1) def test_case_09_2(self): self.case_09_0(2) def test_case_09_3(self): self.case_09_0(3) def case_03_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/03_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '许星皓', 'gender': '', 'admission_time': '', 'education_time': '2012年12月30日', 'education_level': '专科', 'education_type': '高等教育自学考试', 'learning_type': '', 'school': '海南科技职业学院', 'major': '数控技术应用(专科)', 'number': '66460183101004424'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_03_0(self): self.case_03_0(0) def test_case_03_1(self): self.case_03_0(1) def test_case_03_2(self): self.case_03_0(2) def test_case_03_3(self): self.case_03_0(3) def case_01_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/01_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '薛峰', 'gender': '男', 'admission_time': '2009年09月01日', 'education_time': '2013年06月30日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '武汉纺织大学外经贸学院', 'major': '高分子材料与工程', 'number': '132401201305799592'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_01_0(self): self.case_01_0(0) def test_case_01_1(self): self.case_01_0(1) def test_case_01_2(self): self.case_01_0(2) def test_case_01_3(self): self.case_01_0(3) def case_15_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/15_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '马兴', 'gender': '男', 'admission_time': '2013年09月10日', 'education_time': '2016年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '天津中德应用技术大学', 'major': '物联网应用技术', 'number': '121051201606002822'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_15_0(self): self.case_15_0(0) def test_case_15_1(self): self.case_15_0(1) def test_case_15_2(self): self.case_15_0(2) def test_case_15_3(self): self.case_15_0(3) def case_16_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/1/16_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '杨雪凌', 'gender': '男', 'admission_time': '2015年08月02日', 'education_time': '2019年07月01日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '云南民族大学', 'major': '市场营销', 'number': '106911201905004742'} json_input['orientation'] = orientation self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation]) def test_case_16_0(self): self.case_16_0(0) def test_case_16_1(self): self.case_16_0(1) def test_case_16_2(self): self.case_16_0(2) def test_case_16_3(self): self.case_16_0(3)