import unittest import base64 from dataclasses import dataclass from pathlib import Path import cv2 import requests url = 'http://192.168.199.249:38811' 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=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'], 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/1/10_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '家庭户', 'address': '宁夏回族自治区银川市灵武市宁东镇磁矿家属区1502', 'address_province': '宁夏回族自治区', 'address_city': '银川市', 'address_region': '灵武市', 'address_detail': '宁东镇磁矿家属区1502', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/1/09_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '非农业家庭户口', 'address': '宁夏回族自治区吴忠市利通区东兴街花寺中心村05幢5041室23065号', 'address_province': '宁夏回族自治区', 'address_city': '吴忠市', 'address_region': '利通区', 'address_detail': '东兴街花寺中心村05幢5041室23065号', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/1/08_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '乡村家庭户', 'address': '宁夏回族自治区吴忠市利通区金积镇黎花桥村708030', 'address_province': '宁夏回族自治区', 'address_city': '吴忠市', 'address_region': '利通区', 'address_detail': '金积镇黎花桥村708030', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/1/01_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '城镇居民家庭户口', 'address': '浙江省宁波市慈溪市县南华街366号', 'address_province': '浙江省', 'address_city': '宁波市', 'address_region': '慈溪市', 'address_detail': '县南华街366号', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/1/11_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '乡村家庭户', 'address': '宁夏回族自治区吴忠市利通区金积镇丁家湾子村18040号', 'address_province': '宁夏回族自治区', 'address_city': '吴忠市', 'address_region': '利通区', 'address_detail': '金积镇丁家湾子村18040号', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/1/13_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '农业家庭户口', 'address': '广东省梅州市兴宁市水口镇茂兴村社角36号', 'address_province': '广东省', 'address_city': '梅州市', 'address_region': '兴宁市', 'address_detail': '水口镇茂兴村社角36号', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/1/03_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '家庭户', 'address': '广西壮族自治区桂林市临桂县两江镇宝山村委下樟村83号', 'address_province': '广西壮族自治区', 'address_city': '桂林市', 'address_region': '临桂县', 'address_detail': '两江镇宝山村委下樟村83号', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/1/02_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '非农业家庭户口', 'address': '皂市水泥厂22-039', 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/1/12_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '乡村家庭户', 'address': '宁夏回族自治区吴忠市利通区郭家桥乡吴桥村四队88号', 'address_province': '宁夏回族自治区', 'address_city': '吴忠市', 'address_region': '利通区', 'address_detail': '郭家桥乡吴桥村四队88号', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/1/07_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '家庭户', 'address': '甘肃省定西市临洮县康家集乡大头山村羊角湾社', 'address_province': '甘肃省', 'address_city': '定西市', 'address_region': '临洮县', 'address_detail': '康家集乡大头山村羊角湾社', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/1/06_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '居民户口', 'address': '黑龙江省佳木斯市桦川县东河乡兴安村1组', 'address_province': '黑龙江省', 'address_city': '佳木斯市', 'address_region': '桦川县', 'address_detail': '东河乡兴安村1组', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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, 'type': '居民家庭户口', 'address': '江西省新余市渝水区胜利南路183号1栋1单元1402室', 'address_province': '江西省', 'address_city': '新余市', 'address_region': '渝水区', 'address_detail': '胜利南路183号1栋1单元1402室', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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/clean/1/05_img.jpg' json_input = {'status': '000', 'orientation': 0, 'type': '居民家庭户', 'address': '安徽省马鞍山市花山区湖北东路479号4栋405室', 'address_province': '安徽省', 'address_city': '马鞍山市', 'address_region': '花山区', 'address_detail': '湖北东路479号4栋405室', 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''} 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)