import unittest import base64 from dataclasses import dataclass from pathlib import Path import cv2 import requests url = 'http://192.168.199.249:5524' 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/regbook', json={"image": img_str, "image_type": image_type}) print(r.json()) return r.json() @dataclass class ResultItem: status: str orientation: int type: str address: str address_province: str address_city: str address_region: str address_detail: str name: str id: str gender: str birthplace: str birthplace_province: str birthplace_city: str birthplace_region: str native_place: str native_place_province: str native_place_city: str native_place_region: str blood_type: str religion: str class TestRegBookOcr(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'], type=r['result']['type']['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'], name=r['result']['name']['text'], id=r['result']['id']['text'], gender=r['result']['gender']['text'], birthplace=r['result']['birthplace']['text'], birthplace_province=r['result']['birthplace_province']['text'], birthplace_city=r['result']['birthplace_city']['text'], birthplace_region=r['result']['birthplace_region']['text'], native_place=r['result']['native_place']['text'], native_place_province=r['result']['native_place_province']['text'], native_place_city=r['result']['native_place_city']['text'], native_place_region=r['result']['native_place_region']['text'], blood_type=r['result']['blood_type']['text'], religion=r['result']['religion']['text'])) def case_10_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/0/10_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '佘姗', 'id': '421125199209122045', 'gender': '女', 'birthplace': '湖北省黄冈市浠水县', 'birthplace_province': '湖北省', 'birthplace_city': '黄冈市', 'birthplace_region': '浠水县', 'native_place': '湖北省黄冈市浠水县', 'native_place_province': '湖北省', 'native_place_city': '黄冈市', 'native_place_region': '浠水县', 'blood_type': '不明', 'religion': ''} 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/clean/0/09_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '陈玉虎', 'id': '622427199811114715', 'gender': '男', 'birthplace': '甘肃省定西市临洮县', 'birthplace_province': '甘肃省', 'birthplace_city': '定西市', 'birthplace_region': '临洮县', 'native_place': '甘肃省定西市临洮县', 'native_place_province': '甘肃省', 'native_place_city': '定西市', 'native_place_region': '临洮县', 'blood_type': '不明', 'religion': '无宗教信仰'} 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/clean/0/08_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '唐振宇', 'id': '230826199211171418', 'gender': '男', 'birthplace': '黑龙江省佳木斯市桦川县', 'birthplace_province': '黑龙江省', 'birthplace_city': '佳木斯市', 'birthplace_region': '桦川县', 'native_place': '黑龙江省佳木斯市桦川县', 'native_place_province': '黑龙江省', 'native_place_city': '佳木斯市', 'native_place_region': '桦川县', 'blood_type': '', 'religion': ''} 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/clean/0/01_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '汤茜', 'id': '511011199906124749', 'gender': '女', 'birthplace': '四川省内江市东兴区', 'birthplace_province': '四川省', 'birthplace_city': '内江市', 'birthplace_region': '东兴区', 'native_place': '四川省内江市东兴区', 'native_place_province': '四川省', 'native_place_city': '内江市', 'native_place_region': '东兴区', 'blood_type': '', 'religion': ''} 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/clean/0/11_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '陈文浩', 'id': '640302199906030310', 'gender': '男', 'birthplace': '宁夏回族自治区吴忠市', 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '吴忠市', 'birthplace_region': '', 'native_place': '宁夏回族自治区', 'native_place_province': '宁夏回族自治区', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/0/13_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '陈佳新', 'id': '640302199610081119', 'gender': '男', 'birthplace': '宁夏回族自治区吴忠市', 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '吴忠市', 'birthplace_region': '', 'native_place': '宁夏回族自治区吴忠市', 'native_place_province': '宁夏回族自治区', 'native_place_city': '吴忠市', 'native_place_region': '', 'blood_type': 'AB型', 'religion': ''} 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/clean/0/03_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '张媛媛', 'id': '420106197810313222', 'gender': '女', 'birthplace': '湖北省武汉市武昌区', 'birthplace_province': '湖北省', 'birthplace_city': '武汉市', 'birthplace_region': '武昌区', 'native_place': '河南省开封市', 'native_place_province': '河南省', 'native_place_city': '开封市', 'native_place_region': '', 'blood_type': '', 'religion': '无'} 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/clean/0/02_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '孙也桐', 'id': '230125199603270066', 'gender': '女', 'birthplace': '黑龙江省哈尔滨市宾县', 'birthplace_province': '黑龙江省', 'birthplace_city': '哈尔滨市', 'birthplace_region': '宾县', 'native_place': '黑龙江省哈尔滨市宾县', 'native_place_province': '黑龙江省', 'native_place_city': '哈尔滨市', 'native_place_region': '宾县', 'blood_type': '', 'religion': ''} 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/clean/0/12_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '马洋', 'id': '64030219980128113X', 'gender': '男', 'birthplace': '宁夏回族自治区吴忠市', 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '吴忠市', 'birthplace_region': '', 'native_place': '宁夏回族自治区吴忠市', 'native_place_province': '宁夏回族自治区', 'native_place_city': '吴忠市', 'native_place_region': '', 'blood_type': 'o型', 'religion': ''} 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_17_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/0/17_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '马骞', 'id': '64038219971205341X', 'gender': '男', 'birthplace': '宁夏回族自治区银川市灵武市', 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '银川市', 'birthplace_region': '灵武市', 'native_place': '宁夏回族自治区银川市灵武市', 'native_place_province': '宁夏回族自治区', 'native_place_city': '银川市', 'native_place_region': '灵武市', 'blood_type': 'B型', 'religion': ''} 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_07_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/0/07_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '赵陈晨', 'id': '370124200012107523', 'gender': '女', 'birthplace': '山东省济南市平阴县', 'birthplace_province': '山东省', 'birthplace_city': '济南市', 'birthplace_region': '平阴县', 'native_place': '山东省济南市平阴县', 'native_place_province': '山东省', 'native_place_city': '济南市', 'native_place_region': '平阴县', 'blood_type': '', 'religion': ''} 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/clean/0/06_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '李晓令', 'id': '45212219940905067X', 'gender': '男', 'birthplace': '广西壮族自治区南宁市横县', 'birthplace_province': '广西壮族自治区', 'birthplace_city': '南宁市', 'birthplace_region': '横县', 'native_place': '广西壮族自治区南宁市横县', 'native_place_province': '广西壮族自治区', 'native_place_city': '南宁市', 'native_place_region': '横县', 'blood_type': '', 'religion': '无宗教信仰'} 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_16_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/0/16_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '张景威', 'id': '120222199610247017', 'gender': '男', 'birthplace': '天津市市辖区武清区', 'birthplace_province': '天津市', 'birthplace_city': '市辖区', 'birthplace_region': '武清区', 'native_place': '天津市市辖区武清区', 'native_place_province': '天津市', 'native_place_city': '市辖区', 'native_place_region': '武清区', 'blood_type': '', 'religion': ''} 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) def case_14_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/0/14_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '陈鑫', 'id': '640382199605273716', 'gender': '男', 'birthplace': '宁夏回族自治区银川市灵武市', 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '银川市', 'birthplace_region': '灵武市', 'native_place': '宁夏回族自治区银川市灵武市', 'native_place_province': '宁夏回族自治区', 'native_place_city': '银川市', 'native_place_region': '灵武市', 'blood_type': '不明', 'religion': '伊斯兰教'} 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/clean/0/04_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '刘振荣', 'id': '152125630420052', 'gender': '女', 'birthplace': '内蒙古自治区牙克石市', 'birthplace_province': '内蒙古自治区', 'birthplace_city': '牙克石市', 'birthplace_region': '', 'native_place': '河北省衡水市阜城县', 'native_place_province': '河北省', 'native_place_city': '衡水市', 'native_place_region': '阜城县', 'blood_type': 'A型', 'religion': '无'} 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/clean/0/05_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '宋国军', 'id': '152125610820051', 'gender': '男', 'birthplace': '内蒙古自治区', 'birthplace_province': '内蒙古自治区', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '河南省三门峡市陕县', 'native_place_province': '河南省', 'native_place_city': '三门峡市', 'native_place_region': '陕县', 'blood_type': 'A型', 'religion': '无'} 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_15_0(self, orientation=0): dict_orientation = {0: None, 1: 0, 2: 1, 3: 2} image_path = '../images/clean/0/15_img.jpg' json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '马宁', 'id': '64038219981016341x', 'gender': '男', 'birthplace': '宁夏回族自治区灵武市吴四队', 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '灵武市', 'birthplace_region': '吴四队', 'native_place': '宁夏回族自治区', 'native_place_province': '宁夏回族自治区', 'native_place_city': '', 'native_place_region': '', 'blood_type': 'B型', 'religion': ''} 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)