123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- 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)
-
|