clear_1_test.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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:5524'
  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': orientation, 'type': '家庭户',
  71. 'address': '宁夏回族自治区银川市灵武市宁东镇磁矿家属区1502', 'address_province': '宁夏回族自治区', 'address_city': '银川市',
  72. 'address_region': '灵武市', 'address_detail': '宁东镇磁矿家属区1502', 'name': '', 'id': '', 'gender': '',
  73. 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '',
  74. 'native_place': '', 'native_place_province': '', 'native_place_city': '',
  75. 'native_place_region': '', 'blood_type': '', 'religion': ''}
  76. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  77. def test_case_10_0(self):
  78. self.case_10_0(0)
  79. def test_case_10_1(self):
  80. self.case_10_0(1)
  81. def test_case_10_2(self):
  82. self.case_10_0(2)
  83. def test_case_10_3(self):
  84. self.case_10_0(3)
  85. def case_09_0(self, orientation=0):
  86. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  87. image_path = '../images/clean/1/09_img.jpg'
  88. json_input = {'status': '000', 'orientation': orientation, 'type': '非农业家庭户口',
  89. 'address': '宁夏回族自治区吴忠市利通区东兴街花寺中心村05幢5041室23065号', 'address_province': '宁夏回族自治区',
  90. 'address_city': '吴忠市', 'address_region': '利通区', 'address_detail': '东兴街花寺中心村05幢5041室23065号',
  91. 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '',
  92. 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '',
  93. 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''}
  94. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  95. def test_case_09_0(self):
  96. self.case_09_0(0)
  97. def test_case_09_1(self):
  98. self.case_09_0(1)
  99. def test_case_09_2(self):
  100. self.case_09_0(2)
  101. def test_case_09_3(self):
  102. self.case_09_0(3)
  103. def case_08_0(self, orientation=0):
  104. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  105. image_path = '../images/clean/1/08_img.jpg'
  106. json_input = {'status': '000', 'orientation': orientation, 'type': '乡村家庭户',
  107. 'address': '宁夏回族自治区吴忠市利通区金积镇黎花桥村708030', 'address_province': '宁夏回族自治区', 'address_city': '吴忠市',
  108. 'address_region': '利通区', 'address_detail': '金积镇黎花桥村708030', 'name': '', 'id': '', 'gender': '',
  109. 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '',
  110. 'native_place': '', 'native_place_province': '', 'native_place_city': '',
  111. 'native_place_region': '', 'blood_type': '', 'religion': ''}
  112. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  113. def test_case_08_0(self):
  114. self.case_08_0(0)
  115. def test_case_08_1(self):
  116. self.case_08_0(1)
  117. def test_case_08_2(self):
  118. self.case_08_0(2)
  119. def test_case_08_3(self):
  120. self.case_08_0(3)
  121. def case_01_0(self, orientation=0):
  122. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  123. image_path = '../images/clean/1/01_img.jpg'
  124. json_input = {'status': '000', 'orientation': orientation, 'type': '城镇居民家庭户口', 'address': '浙江省宁波市慈溪市南华街366号',
  125. 'address_province': '浙江省', 'address_city': '宁波市', 'address_region': '慈溪市',
  126. 'address_detail': '南华街366号', 'name': '', 'id': '', 'gender': '', 'birthplace': '',
  127. 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '',
  128. 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '',
  129. 'religion': ''}
  130. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  131. # pass
  132. def test_case_01_0(self):
  133. self.case_01_0(0)
  134. def test_case_01_1(self):
  135. self.case_01_0(1)
  136. def test_case_01_2(self):
  137. self.case_01_0(2)
  138. def test_case_01_3(self):
  139. self.case_01_0(3)
  140. def case_11_0(self, orientation=0):
  141. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  142. image_path = '../images/clean/1/11_img.jpg'
  143. json_input = {'status': '000', 'orientation': orientation, 'type': '乡村家庭户',
  144. 'address': '宁夏回族自治区吴忠市利通区金积镇丁家湾子村18040号', 'address_province': '宁夏回族自治区', 'address_city': '吴忠市',
  145. 'address_region': '利通区', 'address_detail': '金积镇丁家湾子村18040号', 'name': '', 'id': '', 'gender': '',
  146. 'birthplace': '', 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '',
  147. 'native_place': '', 'native_place_province': '', 'native_place_city': '',
  148. 'native_place_region': '', 'blood_type': '', 'religion': ''}
  149. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  150. def test_case_11_0(self):
  151. self.case_11_0(0)
  152. def test_case_11_1(self):
  153. self.case_11_0(1)
  154. def test_case_11_2(self):
  155. self.case_11_0(2)
  156. def test_case_11_3(self):
  157. self.case_11_0(3)
  158. def case_13_0(self, orientation=0):
  159. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  160. image_path = '../images/clean/1/13_img.jpg'
  161. json_input = {'status': '000', 'orientation': orientation, 'type': '农业家庭户口', 'address': '广东省梅州市兴宁市水口镇茂兴村社角36号',
  162. 'address_province': '广东省', 'address_city': '梅州市', 'address_region': '兴宁市',
  163. 'address_detail': '水口镇茂兴村社角36号', 'name': '', 'id': '', 'gender': '', 'birthplace': '',
  164. 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '',
  165. 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '',
  166. 'religion': ''}
  167. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  168. def test_case_13_0(self):
  169. self.case_13_0(0)
  170. def test_case_13_1(self):
  171. self.case_13_0(1)
  172. def test_case_13_2(self):
  173. self.case_13_0(2)
  174. def test_case_13_3(self):
  175. self.case_13_0(3)
  176. def case_03_0(self, orientation=0):
  177. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  178. image_path = '../images/clean/1/03_img.jpg'
  179. json_input = {'status': '000', 'orientation': 0, 'type': '家庭户', 'address': '广西壮族自治区桂林市临桂县两江镇宝山村委下樟村83号',
  180. 'address_province': '广西壮族自治区', 'address_city': '桂林市', 'address_region': '临桂县',
  181. 'address_detail': '两江镇宝山村委下樟村83号', 'name': '', 'id': '', 'gender': '', 'birthplace': '',
  182. 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '',
  183. 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '',
  184. 'religion': ''}
  185. json_input['orientation'] = orientation
  186. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  187. def test_case_03_0(self):
  188. self.case_03_0(0)
  189. def test_case_03_1(self):
  190. self.case_03_0(1)
  191. def test_case_03_2(self):
  192. self.case_03_0(2)
  193. def test_case_03_3(self):
  194. self.case_03_0(3)
  195. def case_02_0(self, orientation=0):
  196. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  197. image_path = '../images/clean/1/02_img.jpg'
  198. json_input = {'status': '000', 'orientation': 0, 'type': '非农业家庭户口', 'address': '皂市水泥厂22-039',
  199. 'address_province': '', 'address_city': '', 'address_region': '', 'address_detail': '',
  200. 'name': '', 'id': '', 'gender': '', 'birthplace': '', 'birthplace_province': '',
  201. 'birthplace_city': '', 'birthplace_region': '', 'native_place': '', 'native_place_province': '',
  202. 'native_place_city': '', 'native_place_region': '', 'blood_type': '', 'religion': ''}
  203. json_input['orientation'] = orientation
  204. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  205. def test_case_02_0(self):
  206. self.case_02_0(0)
  207. def test_case_02_1(self):
  208. self.case_02_0(1)
  209. def test_case_02_2(self):
  210. self.case_02_0(2)
  211. def test_case_02_3(self):
  212. self.case_02_0(3)
  213. def case_12_0(self, orientation=0):
  214. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  215. image_path = '../images/clean/1/12_img.jpg'
  216. json_input = {'status': '000', 'orientation': 0, 'type': '乡村家庭户', 'address': '宁夏回族自治区吴忠市利通区郭家桥乡吴桥村四队88号',
  217. 'address_province': '宁夏回族自治区', 'address_city': '吴忠市', 'address_region': '利通区',
  218. 'address_detail': '郭家桥乡吴桥村四队88号', 'name': '', 'id': '', 'gender': '', 'birthplace': '',
  219. 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '',
  220. 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '',
  221. 'religion': ''}
  222. json_input['orientation'] = orientation
  223. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  224. def test_case_12_0(self):
  225. self.case_12_0(0)
  226. def test_case_12_1(self):
  227. self.case_12_0(1)
  228. def test_case_12_2(self):
  229. self.case_12_0(2)
  230. def test_case_12_3(self):
  231. self.case_12_0(3)
  232. def case_07_0(self, orientation=0):
  233. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  234. image_path = '../images/clean/1/07_img.jpg'
  235. json_input = {'status': '000', 'orientation': 0, 'type': '家庭户', 'address': '甘肃省定西市临洮县康家集乡大头山村羊角湾社',
  236. 'address_province': '甘肃省', 'address_city': '定西市', 'address_region': '临洮县',
  237. 'address_detail': '康家集乡大头山村羊角湾社', 'name': '', 'id': '', 'gender': '', 'birthplace': '',
  238. 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '',
  239. 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '',
  240. 'religion': ''}
  241. json_input['orientation'] = orientation
  242. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  243. def test_case_07_0(self):
  244. self.case_07_0(0)
  245. def test_case_07_1(self):
  246. self.case_07_0(1)
  247. def test_case_07_2(self):
  248. self.case_07_0(2)
  249. def test_case_07_3(self):
  250. self.case_07_0(3)
  251. def case_06_0(self, orientation=0):
  252. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  253. image_path = '../images/clean/1/06_img.jpg'
  254. json_input = {'status': '000', 'orientation': 0, 'type': '居民户口', 'address': '黑龙江省佳木斯市桦川县东河乡兴安村1组',
  255. 'address_province': '黑龙江省', 'address_city': '佳木斯市', 'address_region': '桦川县',
  256. 'address_detail': '东河乡兴安村1组', 'name': '', 'id': '', 'gender': '', 'birthplace': '',
  257. 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '',
  258. 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '',
  259. 'religion': ''}
  260. json_input['orientation'] = orientation
  261. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  262. def test_case_06_0(self):
  263. self.case_06_0(0)
  264. def test_case_06_1(self):
  265. self.case_06_0(1)
  266. def test_case_06_2(self):
  267. self.case_06_0(2)
  268. def test_case_06_3(self):
  269. self.case_06_0(3)
  270. def case_04_0(self, orientation=0):
  271. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  272. image_path = '../images/clean/1/04_img.jpg'
  273. json_input = {'status': '000', 'orientation': 0, 'type': '居民家庭户口', 'address': '江西省新余市渝水区胜利南路183号1栋1单元1402室',
  274. 'address_province': '江西省', 'address_city': '新余市', 'address_region': '渝水区',
  275. 'address_detail': '胜利南路183号1栋1单元1402室', 'name': '', 'id': '', 'gender': '', 'birthplace': '',
  276. 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '',
  277. 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '',
  278. 'religion': ''}
  279. json_input['orientation'] = orientation
  280. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  281. def test_case_04_0(self):
  282. self.case_04_0(0)
  283. def test_case_04_1(self):
  284. self.case_04_0(1)
  285. def test_case_04_2(self):
  286. self.case_04_0(2)
  287. def test_case_04_3(self):
  288. self.case_04_0(3)
  289. def case_05_0(self, orientation=0):
  290. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  291. image_path = '../images/clean/1/05_img.jpg'
  292. json_input = {'status': '000', 'orientation': 0, 'type': '居民家庭户', 'address': '安徽省马鞍山市花山区湖北东路479号4栋405室',
  293. 'address_province': '安徽省', 'address_city': '马鞍山市', 'address_region': '花山区',
  294. 'address_detail': '湖北东路479号4栋405室', 'name': '', 'id': '', 'gender': '', 'birthplace': '',
  295. 'birthplace_province': '', 'birthplace_city': '', 'birthplace_region': '', 'native_place': '',
  296. 'native_place_province': '', 'native_place_city': '', 'native_place_region': '', 'blood_type': '',
  297. 'religion': ''}
  298. json_input['orientation'] = orientation
  299. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  300. def test_case_05_0(self):
  301. self.case_05_0(0)
  302. def test_case_05_1(self):
  303. self.case_05_0(1)
  304. def test_case_05_2(self):
  305. self.case_05_0(2)
  306. def test_case_05_3(self):
  307. self.case_05_0(3)