clear_0_test.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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=0):
  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/0/10_img.jpg'
  70. json_input = {'status': '000', 'orientation': 0, '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': ''}
  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/0/09_img.jpg'
  84. json_input = {'status': '000', 'orientation': 0, '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': '无宗教信仰'}
  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/0/08_img.jpg'
  98. json_input = {'status': '000', 'orientation': 0, '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': ''}
  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/0/01_img.jpg'
  112. json_input = {'status': '000', 'orientation': 0, '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': ''}
  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/0/11_img.jpg'
  126. json_input = {'status': '000', 'orientation': 0, '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': ''}
  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/0/13_img.jpg'
  140. json_input = {'status': '000', 'orientation': 0, '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': ''}
  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/0/03_img.jpg'
  154. json_input = {'status': '000', 'orientation': 0, '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': '无'}
  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/0/02_img.jpg'
  168. json_input = {'status': '000', 'orientation': 0, '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': ''}
  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/0/12_img.jpg'
  182. json_input = {'status': '000', 'orientation': 0, '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': ''}
  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_17_0(self, orientation=0):
  194. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  195. image_path = '../images/clean/0/17_img.jpg'
  196. json_input = {'status': '000', 'orientation': 0, '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': ''}
  197. json_input['orientation'] = orientation
  198. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  199. def test_case_17_0(self):
  200. self.case_17_0(0)
  201. def test_case_17_1(self):
  202. self.case_17_0(1)
  203. def test_case_17_2(self):
  204. self.case_17_0(2)
  205. def test_case_17_3(self):
  206. self.case_17_0(3)
  207. def case_07_0(self, orientation=0):
  208. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  209. image_path = '../images/clean/0/07_img.jpg'
  210. json_input = {'status': '000', 'orientation': 0, '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': ''}
  211. json_input['orientation'] = orientation
  212. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  213. def test_case_07_0(self):
  214. self.case_07_0(0)
  215. def test_case_07_1(self):
  216. self.case_07_0(1)
  217. def test_case_07_2(self):
  218. self.case_07_0(2)
  219. def test_case_07_3(self):
  220. self.case_07_0(3)
  221. def case_06_0(self, orientation=0):
  222. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  223. image_path = '../images/clean/0/06_img.jpg'
  224. json_input = {'status': '000', 'orientation': 0, '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': '无宗教信仰'}
  225. json_input['orientation'] = orientation
  226. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  227. def test_case_06_0(self):
  228. self.case_06_0(0)
  229. def test_case_06_1(self):
  230. self.case_06_0(1)
  231. def test_case_06_2(self):
  232. self.case_06_0(2)
  233. def test_case_06_3(self):
  234. self.case_06_0(3)
  235. def case_16_0(self, orientation=0):
  236. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  237. image_path = '../images/clean/0/16_img.jpg'
  238. json_input = {'status': '000', 'orientation': 2, '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': ''}
  239. json_input['orientation'] = orientation
  240. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  241. def test_case_16_0(self):
  242. self.case_16_0(0)
  243. def test_case_16_1(self):
  244. self.case_16_0(1)
  245. def test_case_16_2(self):
  246. self.case_16_0(2)
  247. def test_case_16_3(self):
  248. self.case_16_0(3)
  249. def case_14_0(self, orientation=0):
  250. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  251. image_path = '../images/clean/0/14_img.jpg'
  252. json_input = {'status': '000', 'orientation': 0, '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': '伊斯兰教'}
  253. json_input['orientation'] = orientation
  254. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  255. def test_case_14_0(self):
  256. self.case_14_0(0)
  257. def test_case_14_1(self):
  258. self.case_14_0(1)
  259. def test_case_14_2(self):
  260. self.case_14_0(2)
  261. def test_case_14_3(self):
  262. self.case_14_0(3)
  263. def case_04_0(self, orientation=0):
  264. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  265. image_path = '../images/clean/0/04_img.jpg'
  266. json_input = {'status': '000', 'orientation': 0, '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': '无'}
  267. json_input['orientation'] = orientation
  268. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  269. def test_case_04_0(self):
  270. self.case_04_0(0)
  271. def test_case_04_1(self):
  272. self.case_04_0(1)
  273. def test_case_04_2(self):
  274. self.case_04_0(2)
  275. def test_case_04_3(self):
  276. self.case_04_0(3)
  277. def case_05_0(self, orientation=0):
  278. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  279. image_path = '../images/clean/0/05_img.jpg'
  280. json_input = {'status': '000', 'orientation': 0, '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': '无'}
  281. json_input['orientation'] = orientation
  282. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  283. def test_case_05_0(self):
  284. self.case_05_0(0)
  285. def test_case_05_1(self):
  286. self.case_05_0(1)
  287. def test_case_05_2(self):
  288. self.case_05_0(2)
  289. def test_case_05_3(self):
  290. self.case_05_0(3)
  291. def case_15_0(self, orientation=0):
  292. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  293. image_path = '../images/clean/0/15_img.jpg'
  294. json_input = {'status': '000', 'orientation': 0, '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': ''}
  295. json_input['orientation'] = orientation
  296. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  297. def test_case_15_0(self):
  298. self.case_15_0(0)
  299. def test_case_15_1(self):
  300. self.case_15_0(1)
  301. def test_case_15_2(self):
  302. self.case_15_0(2)
  303. def test_case_15_3(self):
  304. self.case_15_0(3)