import unittest import base64 from dataclasses import dataclass from pathlib import Path import cv2 import requests url = 'http://192.168.199.249:18080' 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/idcard', json={"image": img_str, "image_type": image_type}) print(r.json()) return r.json() @dataclass class ResultItem: status: str orientation: int name: str id: str ethnicity: str gender: str birthday: str address: str address_province: str address_city: str address_region: str address_detail: str expire_date:str class TestSchoolCertOcr(unittest.TestCase): def _helper(self, image_path, item: ResultItem, rotate=None,image_type=0): 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'], id=r['result']['id']['text'], ethnicity=r['result']['ethnicity']['text'], gender=r['result']['gender']['text'], birthday=r['result']['birthday']['text'], address=r['result']['address']['text'], address_province=r['result']['address_province']['text'], address_city=r['result']['address_city']['text'], address_region=r['result']['address_region']['text'], address_detail=r['result']['address_detail']['text'], expire_date=r['result']['expire_date']['text'])) def case_10_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/10_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '赵子嫣', 'id': '341621200001024945', 'ethnicity': '汉', 'gender': '女', 'birthday': '2000年01月02日', 'address': '安徽省涡阳县马店镇吴府行政村刘王自然村246号', 'address_province': '安徽省', 'address_city': '亳州市', 'address_region': '涡阳县', 'address_detail': '马店镇吴府行政村刘王自然村246号', 'expire_date': ''} 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_09_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/09_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '郑志聪', 'id': '441521199808234751', 'ethnicity': '汉', 'gender': '男', 'birthday': '1998年08月23日', 'address': '广东省海丰县海城镇城西社区居民委员会西中街一巷242号', 'address_province': '广东省', 'address_city': '汕尾市', 'address_region': '海丰县', 'address_detail': '海城镇城西社区居民委员会西中街一巷242号', 'expire_date': ''} 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_08_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/08_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '路文强', 'id': '610111199412202514', 'ethnicity': '汉', 'gender': '男', 'birthday': '1994年12月20日', 'address': '西安市灞桥区路家湾1组65号', 'address_province': '陕西省', 'address_city': '西安市', 'address_region': '灞桥区', 'address_detail': '路家湾1组65号', 'expire_date': ''} 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_01_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/01_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '刘璐', 'id': '152104199803025225', 'ethnicity': '汉', 'gender': '女', 'birthday': '1998年03月02日', 'address': '内蒙古牙克石市库都尔镇小九亚街林北巷121号', 'address_province': '内蒙古自治区', 'address_city': '呼伦贝尔市', 'address_region': '牙克石市', 'address_detail': '库都尔镇小九亚街林北巷121号', 'expire_date': ''} 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_11_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/11_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '刘方杰', 'id': '360421199608230039', 'ethnicity': '汉', 'gender': '男', 'birthday': '1996年08月23日', 'address': '江西省九江市九江县沙河街镇庐山西路388号', 'address_province': '江西省', 'address_city': '九江市', 'address_region': '九江县', 'address_detail': '沙河街镇庐山西路388号', 'expire_date': ''} 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_13_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/13_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '菅恒硕', 'id': '372330199906226155', 'ethnicity': '汉', 'gender': '男', 'birthday': '1999年06月22日', 'address': '山东省邹平县魏桥镇菅家村14号', 'address_province': '山东省', 'address_city': '滨州市', 'address_region': '邹平县', 'address_detail': '魏桥镇营家村14号', 'expire_date': ''} 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_03_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/03_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '李琳', 'id': '130535199611180029', 'ethnicity': '汉', 'gender': '女', 'birthday': '1996年11月18日', 'address': '河北省邢台市临西县影院南路5号监察局家属院002号', 'address_province': '河北省', 'address_city': '邢台市', 'address_region': '临西县', 'address_detail': '影院南路5号监察局家属院002号', 'expire_date': ''} 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_02_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/02_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '雷盈盈', 'id': '511181198404100026', 'ethnicity': '汉', 'gender': '女', 'birthday': '1984年04月10日', 'address': '四川省峨眉山市绥山镇西正街135号附2号', 'address_province': '四川省', 'address_city': '乐山市', 'address_region': '峨眉山市', 'address_detail': '绥山镇西正街135号附2号', 'expire_date': ''} 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_12_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/12_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '达日罕', 'id': '150422200001286055', 'ethnicity': '蒙古', 'gender': '男', 'birthday': '2000年01月28日', 'address': '内蒙古赤峰市巴林左旗查干哈达苏木红光嘎查胡北沟屯一组', 'address_province': '内蒙古自治区', 'address_city': '赤峰市', 'address_region': '巴林左旗', 'address_detail': '查干哈达苏木红光嘎查胡北沟屯一组', 'expire_date': ''} 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_07_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/07_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '韩凯峰', 'id': '152822199806244811', 'ethnicity': '汉', 'gender': '男', 'birthday': '1998年06月24日', 'address': '内蒙古巴彦淖尔市五原县新公中镇永生村三社064号', 'address_province': '内蒙古自治区', 'address_city': '巴彦淖尔市', 'address_region': '五原县', 'address_detail': '新公中镇永生村三社064号', 'expire_date': ''} 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_06_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/06_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '李秉桦', 'id': '452730200006265615', 'ethnicity': '壮', 'gender': '男', 'birthday': '2000年06月26日', 'address': '南宁市江南区五一路105号6栋4单元107房', 'address_province': '广西壮族自治区', 'address_city': '南宁市', 'address_region': '江南区', 'address_detail': '五一路105号6栋4单元107房', 'expire_date': ''} 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_14_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/14_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '张劲松', 'id': '150125199902090217', 'ethnicity': '汉', 'gender': '男', 'birthday': '1999年02月09日', 'address': '呼和浩特市赛罕区呼伦贝尔南路雅格星城A3号楼2502号', 'address_province': '内蒙古自治区', 'address_city': '呼和浩特市', 'address_region': '赛罕区', 'address_detail': '呼伦贝尔南路雅格星城A3号楼2502号', 'expire_date': ''} 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_04_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/04_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '裴浩森', 'id': '142729200010121851', 'ethnicity': '汉', 'gender': '男', 'birthday': '2000年10月12日', 'address': '山西省闻喜县礼元镇裴柏村118号', 'address_province': '山西省', 'address_city': '运城市', 'address_region': '闻喜县', 'address_detail': '礼元镇装柏村118号', 'expire_date': ''} 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_05_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/test_img/7.27/0/05_img.jpg' json_input = {'status': '000', 'orientation': 0, 'name': '杨赫日轮', 'id': '152322199902094318', 'ethnicity': '蒙古', 'gender': '男', 'birthday': '1999年02月09日', 'address': '内蒙古通辽市科尔沁左翼中旗舍伯吐镇中熬本台嘎查86号', 'address_province': '内蒙古自治区', 'address_city': '通辽市', 'address_region': '科尔沁左翼中旗', 'address_detail': '舍伯吐镇中熬本台嘎查86号', 'expire_date': ''} 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)