import unittest import base64 from dataclasses import dataclass from pathlib import Path import cv2 import requests url = 'http://192.168.199.249:18050' def send_request(image_path, 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/cet4', json={"image": img_str}) print(r.json()) return r.json() @dataclass class ResultItem: status: str orientation: int name: str id: str language: str level: str exam_time: str score: str class TestSchoolCertOcr(unittest.TestCase): def _helper(self, image_path, item: ResultItem, rotate=None): root = Path(__file__).parent image_path = str(root / image_path) r = send_request(image_path, rotate) self.assertEqual(item, ResultItem(status=r['status'], orientation=r['result']['orientation'], name=r['result']['name']['text'], id=r['result']['id']['text'], language=r['result']['language']['text'], level=r['result']['level']['text'], exam_time=r['result']['exam_time']['text'], score=r['result']['score']['text'])) def case_02_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/cet4/02_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '纪春', 'id': '230904199909090529', 'language': '英语', 'level': 'CET4', 'exam_time': '2018年6月', 'score': '472'} 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_04_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/cet4/04_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '林雄', 'id': '431121199807158036', 'language': '英语', 'level': 'CET4', 'exam_time': '2017年12月', 'score': '490'} 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/cet4/07_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '吴晓虎', 'id': '150426199902113039', 'language': '英语', 'level': 'CET4', 'exam_time': '2018年12月', 'score': '425'} 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/cet4/05_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '李然琦', 'id': '370502199709144023', 'language': '英语', 'level': 'CET4', 'exam_time': '2016年12月', 'score': '438'} 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_10_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/cet4/10_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '袁湘粤', 'id': '43052419961021177X', 'language': '英语', 'level': 'CET4', 'exam_time': '2020年12月', 'score': '448'} 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/cet4/08_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '张鑫', 'id': '140227199809282317', 'language': '英语', 'level': 'CET4', 'exam_time': '2021年6月', 'score': '445'} 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_09_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/cet4/09_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '张鹏远', 'id': '150203199812150615', 'language': '英语', 'level': 'CET4', 'exam_time': '2020年12月', 'score': '437'} 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/cet4/03_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '姚帆', 'id': '142702199903124229', 'language': '英语', 'level': 'CET4', 'exam_time': '2019年12月', 'score': '471'} 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/cet4/01_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '武文斌', 'id': '642226199704273215', 'language': '英语', 'level': 'CET4', 'exam_time': '2021年12月', 'score': '474'} 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)