clear_1_test.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import unittest
  2. import base64
  3. from dataclasses import dataclass
  4. from pathlib import Path
  5. import cv2
  6. import requests
  7. url = 'http://192.168.199.249:38811'
  8. def send_request(image_path, image_type, rotate=None):
  9. img = cv2.imread(str(image_path))
  10. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  11. if rotate is not None:
  12. img = cv2.rotate(img, rotate)
  13. _, im_arr = cv2.imencode('.jpg', img)
  14. img_str = base64.b64encode(im_arr).decode('utf-8')
  15. r = requests.post(url + '/ocr_system/regbook', json={"image": img_str, "image_type": image_type})
  16. print(r.json())
  17. return r.json()
  18. @dataclass
  19. class ResultItem:
  20. status: str
  21. orientation: int
  22. type: str
  23. address: str
  24. address_province: str
  25. address_city: str
  26. address_region: str
  27. address_detail: str
  28. name: str
  29. id: str
  30. gender: str
  31. birthplace: str
  32. birthplace_province: str
  33. birthplace_city: str
  34. birthplace_region: str
  35. native_place: str
  36. native_place_province: str
  37. native_place_city: str
  38. native_place_region: str
  39. blood_type: str
  40. religion: str
  41. class TestRegBookOcr(unittest.TestCase):
  42. def _helper(self, image_path, item: ResultItem, rotate=None,image_type=1):
  43. root = Path(__file__).parent
  44. image_path = str(root / image_path)
  45. r = send_request(image_path, image_type, rotate)
  46. self.assertEqual(item, ResultItem(status=r['status'],
  47. orientation=r['result']['orientation'],
  48. type=r['result']['type']['text'],
  49. address=r['result']['address']['text'],
  50. address_province=r['result']['address_province']['text'],
  51. address_city=r['result']['address_city']['text'],
  52. address_region=r['result']['address_region']['text'],
  53. address_detail=r['result']['address_detail']['text'],
  54. name=r['result']['name']['text'],
  55. id=r['result']['id']['text'],
  56. gender=r['result']['gender']['text'],
  57. birthplace=r['result']['birthplace']['text'],
  58. birthplace_province=r['result']['birthplace_province']['text'],
  59. birthplace_city=r['result']['birthplace_city']['text'],
  60. birthplace_region=r['result']['birthplace_region']['text'],
  61. native_place=r['result']['native_place']['text'],
  62. native_place_province=r['result']['native_place_province']['text'],
  63. native_place_city=r['result']['native_place_city']['text'],
  64. native_place_region=r['result']['native_place_region']['text'],
  65. blood_type=r['result']['blood_type']['text'],
  66. religion=r['result']['religion']['text']))
  67. def case_10_0(self, orientation=0):
  68. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  69. image_path = '../images/clean/1/10_img.jpg'
  70. 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': ''}
  71. json_input['orientation'] = orientation
  72. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  73. def test_case_10_0(self):
  74. self.case_10_0(0)
  75. def test_case_10_1(self):
  76. self.case_10_0(1)
  77. def test_case_10_2(self):
  78. self.case_10_0(2)
  79. def test_case_10_3(self):
  80. self.case_10_0(3)
  81. def case_09_0(self, orientation=0):
  82. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  83. image_path = '../images/clean/1/09_img.jpg'
  84. 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': ''}
  85. json_input['orientation'] = orientation
  86. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  87. def test_case_09_0(self):
  88. self.case_09_0(0)
  89. def test_case_09_1(self):
  90. self.case_09_0(1)
  91. def test_case_09_2(self):
  92. self.case_09_0(2)
  93. def test_case_09_3(self):
  94. self.case_09_0(3)
  95. def case_08_0(self, orientation=0):
  96. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  97. image_path = '../images/clean/1/08_img.jpg'
  98. 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': ''}
  99. json_input['orientation'] = orientation
  100. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  101. def test_case_08_0(self):
  102. self.case_08_0(0)
  103. def test_case_08_1(self):
  104. self.case_08_0(1)
  105. def test_case_08_2(self):
  106. self.case_08_0(2)
  107. def test_case_08_3(self):
  108. self.case_08_0(3)
  109. def case_01_0(self, orientation=0):
  110. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  111. image_path = '../images/clean/1/01_img.jpg'
  112. 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': ''}
  113. json_input['orientation'] = orientation
  114. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  115. def test_case_01_0(self):
  116. self.case_01_0(0)
  117. def test_case_01_1(self):
  118. self.case_01_0(1)
  119. def test_case_01_2(self):
  120. self.case_01_0(2)
  121. def test_case_01_3(self):
  122. self.case_01_0(3)
  123. def case_11_0(self, orientation=0):
  124. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  125. image_path = '../images/clean/1/11_img.jpg'
  126. 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': ''}
  127. json_input['orientation'] = orientation
  128. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  129. def test_case_11_0(self):
  130. self.case_11_0(0)
  131. def test_case_11_1(self):
  132. self.case_11_0(1)
  133. def test_case_11_2(self):
  134. self.case_11_0(2)
  135. def test_case_11_3(self):
  136. self.case_11_0(3)
  137. def case_13_0(self, orientation=0):
  138. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  139. image_path = '../images/clean/1/13_img.jpg'
  140. 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': ''}
  141. json_input['orientation'] = orientation
  142. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  143. def test_case_13_0(self):
  144. self.case_13_0(0)
  145. def test_case_13_1(self):
  146. self.case_13_0(1)
  147. def test_case_13_2(self):
  148. self.case_13_0(2)
  149. def test_case_13_3(self):
  150. self.case_13_0(3)
  151. def case_03_0(self, orientation=0):
  152. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  153. image_path = '../images/clean/1/03_img.jpg'
  154. 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': ''}
  155. json_input['orientation'] = orientation
  156. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  157. def test_case_03_0(self):
  158. self.case_03_0(0)
  159. def test_case_03_1(self):
  160. self.case_03_0(1)
  161. def test_case_03_2(self):
  162. self.case_03_0(2)
  163. def test_case_03_3(self):
  164. self.case_03_0(3)
  165. def case_02_0(self, orientation=0):
  166. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  167. image_path = '../images/clean/1/02_img.jpg'
  168. 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': ''}
  169. json_input['orientation'] = orientation
  170. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  171. def test_case_02_0(self):
  172. self.case_02_0(0)
  173. def test_case_02_1(self):
  174. self.case_02_0(1)
  175. def test_case_02_2(self):
  176. self.case_02_0(2)
  177. def test_case_02_3(self):
  178. self.case_02_0(3)
  179. def case_12_0(self, orientation=0):
  180. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  181. image_path = '../images/clean/1/12_img.jpg'
  182. 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': ''}
  183. json_input['orientation'] = orientation
  184. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  185. def test_case_12_0(self):
  186. self.case_12_0(0)
  187. def test_case_12_1(self):
  188. self.case_12_0(1)
  189. def test_case_12_2(self):
  190. self.case_12_0(2)
  191. def test_case_12_3(self):
  192. self.case_12_0(3)
  193. def case_07_0(self, orientation=0):
  194. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  195. image_path = '../images/clean/1/07_img.jpg'
  196. 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': ''}
  197. json_input['orientation'] = orientation
  198. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  199. def test_case_07_0(self):
  200. self.case_07_0(0)
  201. def test_case_07_1(self):
  202. self.case_07_0(1)
  203. def test_case_07_2(self):
  204. self.case_07_0(2)
  205. def test_case_07_3(self):
  206. self.case_07_0(3)
  207. def case_06_0(self, orientation=0):
  208. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  209. image_path = '../images/clean/1/06_img.jpg'
  210. 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': ''}
  211. json_input['orientation'] = orientation
  212. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  213. def test_case_06_0(self):
  214. self.case_06_0(0)
  215. def test_case_06_1(self):
  216. self.case_06_0(1)
  217. def test_case_06_2(self):
  218. self.case_06_0(2)
  219. def test_case_06_3(self):
  220. self.case_06_0(3)
  221. def case_04_0(self, orientation=0):
  222. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  223. image_path = '../images/clean/1/04_img.jpg'
  224. 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': ''}
  225. json_input['orientation'] = orientation
  226. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  227. def test_case_04_0(self):
  228. self.case_04_0(0)
  229. def test_case_04_1(self):
  230. self.case_04_0(1)
  231. def test_case_04_2(self):
  232. self.case_04_0(2)
  233. def test_case_04_3(self):
  234. self.case_04_0(3)
  235. def case_05_0(self, orientation=0):
  236. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  237. image_path = '../images/clean/1/05_img.jpg'
  238. 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': ''}
  239. json_input['orientation'] = orientation
  240. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  241. def test_case_05_0(self):
  242. self.case_05_0(0)
  243. def test_case_05_1(self):
  244. self.case_05_0(1)
  245. def test_case_05_2(self):
  246. self.case_05_0(2)
  247. def test_case_05_3(self):
  248. self.case_05_0(3)