clear_0_test.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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=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': orientation, 'type': '', 'address': '', 'address_province': '',
  71. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '佘姗',
  72. 'id': '421125199209122045', 'gender': '女', 'birthplace': '湖北省黄冈市浠水县',
  73. '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/0/09_img.jpg'
  88. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  89. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '陈玉虎',
  90. 'id': '622427199811114715', 'gender': '男', 'birthplace': '甘肃省定西市临洮县',
  91. 'birthplace_province': '甘肃省', 'birthplace_city': '定西市', 'birthplace_region': '临洮县',
  92. 'native_place': '甘肃省定西市临洮县', 'native_place_province': '甘肃省', 'native_place_city': '定西市',
  93. '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/0/08_img.jpg'
  106. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  107. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '唐振宇',
  108. 'id': '230826199211171418', 'gender': '男', 'birthplace': '黑龙江省佳木斯市桦川县',
  109. '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/0/01_img.jpg'
  124. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  125. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '汤茜',
  126. 'id': '511011199906124749', 'gender': '女', 'birthplace': '四川省内江市东兴区',
  127. 'birthplace_province': '四川省', 'birthplace_city': '内江市', 'birthplace_region': '东兴区',
  128. 'native_place': '四川省内江市东兴区', 'native_place_province': '四川省', 'native_place_city': '内江市',
  129. 'native_place_region': '东兴区', 'blood_type': '', 'religion': ''}
  130. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  131. def test_case_01_0(self):
  132. self.case_01_0(0)
  133. def test_case_01_1(self):
  134. self.case_01_0(1)
  135. def test_case_01_2(self):
  136. self.case_01_0(2)
  137. def test_case_01_3(self):
  138. self.case_01_0(3)
  139. def case_11_0(self, orientation=0):
  140. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  141. image_path = '../images/clean/0/11_img.jpg'
  142. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  143. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '陈文浩',
  144. 'id': '640302199906030310', 'gender': '男', 'birthplace': '宁夏回族自治区吴忠市',
  145. 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '吴忠市', 'birthplace_region': '',
  146. 'native_place': '宁夏回族自治区', 'native_place_province': '宁夏回族自治区', 'native_place_city': '',
  147. 'native_place_region': '', 'blood_type': '', 'religion': ''}
  148. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  149. def test_case_11_0(self):
  150. self.case_11_0(0)
  151. def test_case_11_1(self):
  152. self.case_11_0(1)
  153. def test_case_11_2(self):
  154. self.case_11_0(2)
  155. def test_case_11_3(self):
  156. self.case_11_0(3)
  157. def case_13_0(self, orientation=0):
  158. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  159. image_path = '../images/clean/0/13_img.jpg'
  160. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  161. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '陈佳新',
  162. 'id': '640302199610081119', 'gender': '男', 'birthplace': '宁夏回族自治区吴忠市',
  163. 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '吴忠市', 'birthplace_region': '',
  164. 'native_place': '宁夏回族自治区吴忠市', 'native_place_province': '宁夏回族自治区', 'native_place_city': '吴忠市',
  165. 'native_place_region': '', 'blood_type': 'AB型', 'religion': ''}
  166. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  167. def test_case_13_0(self):
  168. self.case_13_0(0)
  169. def test_case_13_1(self):
  170. self.case_13_0(1)
  171. def test_case_13_2(self):
  172. self.case_13_0(2)
  173. def test_case_13_3(self):
  174. self.case_13_0(3)
  175. def case_03_0(self, orientation=0):
  176. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  177. image_path = '../images/clean/0/03_img.jpg'
  178. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  179. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '张媛媛',
  180. 'id': '420106197810313222', 'gender': '女', 'birthplace': '湖北省武汉市武昌区',
  181. 'birthplace_province': '湖北省', 'birthplace_city': '武汉市', 'birthplace_region': '武昌区',
  182. 'native_place': '河南省开封市', 'native_place_province': '河南省', 'native_place_city': '开封市',
  183. 'native_place_region': '', 'blood_type': '', 'religion': '无'}
  184. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  185. def test_case_03_0(self):
  186. self.case_03_0(0)
  187. def test_case_03_1(self):
  188. self.case_03_0(1)
  189. def test_case_03_2(self):
  190. self.case_03_0(2)
  191. def test_case_03_3(self):
  192. self.case_03_0(3)
  193. def case_02_0(self, orientation=0):
  194. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  195. image_path = '../images/clean/0/02_img.jpg'
  196. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  197. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '孙也桐',
  198. 'id': '230125199603270066', 'gender': '女', 'birthplace': '黑龙江省哈尔滨市宾县',
  199. 'birthplace_province': '黑龙江省', 'birthplace_city': '哈尔滨市', 'birthplace_region': '宾县',
  200. 'native_place': '黑龙江省哈尔滨市宾县', 'native_place_province': '黑龙江省', 'native_place_city': '哈尔滨市',
  201. 'native_place_region': '宾县', 'blood_type': '', 'religion': ''}
  202. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  203. def test_case_02_0(self):
  204. self.case_02_0(0)
  205. def test_case_02_1(self):
  206. self.case_02_0(1)
  207. def test_case_02_2(self):
  208. self.case_02_0(2)
  209. def test_case_02_3(self):
  210. self.case_02_0(3)
  211. def case_12_0(self, orientation=0):
  212. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  213. image_path = '../images/clean/0/12_img.jpg'
  214. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  215. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '马洋',
  216. 'id': '64030219980128113X', 'gender': '男', 'birthplace': '宁夏回族自治区吴忠市',
  217. 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '吴忠市', 'birthplace_region': '',
  218. 'native_place': '宁夏回族自治区吴忠市', 'native_place_province': '宁夏回族自治区', 'native_place_city': '吴忠市',
  219. 'native_place_region': '', 'blood_type': 'o型', 'religion': ''}
  220. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  221. def test_case_12_0(self):
  222. self.case_12_0(0)
  223. def test_case_12_1(self):
  224. self.case_12_0(1)
  225. def test_case_12_2(self):
  226. self.case_12_0(2)
  227. def test_case_12_3(self):
  228. self.case_12_0(3)
  229. def case_17_0(self, orientation=0):
  230. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  231. image_path = '../images/clean/0/17_img.jpg'
  232. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  233. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '马骞',
  234. 'id': '64038219971205341X', 'gender': '男', 'birthplace': '宁夏回族自治区银川市灵武市',
  235. 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '银川市', 'birthplace_region': '灵武市',
  236. 'native_place': '宁夏回族自治区银川市灵武市', 'native_place_province': '宁夏回族自治区', 'native_place_city': '银川市',
  237. 'native_place_region': '灵武市', 'blood_type': 'B型', 'religion': ''}
  238. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  239. def test_case_17_0(self):
  240. self.case_17_0(0)
  241. def test_case_17_1(self):
  242. self.case_17_0(1)
  243. def test_case_17_2(self):
  244. self.case_17_0(2)
  245. def test_case_17_3(self):
  246. self.case_17_0(3)
  247. def case_07_0(self, orientation=0):
  248. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  249. image_path = '../images/clean/0/07_img.jpg'
  250. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  251. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '赵陈晨',
  252. 'id': '370124200012107523', 'gender': '女', 'birthplace': '山东省济南市平阴县',
  253. 'birthplace_province': '山东省', 'birthplace_city': '济南市', 'birthplace_region': '平阴县',
  254. 'native_place': '山东省济南市平阴县', 'native_place_province': '山东省', 'native_place_city': '济南市',
  255. 'native_place_region': '平阴县', 'blood_type': '', 'religion': ''}
  256. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  257. def test_case_07_0(self):
  258. self.case_07_0(0)
  259. def test_case_07_1(self):
  260. self.case_07_0(1)
  261. def test_case_07_2(self):
  262. self.case_07_0(2)
  263. def test_case_07_3(self):
  264. self.case_07_0(3)
  265. def case_06_0(self, orientation=0):
  266. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  267. image_path = '../images/clean/0/06_img.jpg'
  268. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  269. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '李晓令',
  270. 'id': '45212219940905067X', 'gender': '男', 'birthplace': '广西壮族自治区南宁市横县',
  271. 'birthplace_province': '广西壮族自治区', 'birthplace_city': '南宁市', 'birthplace_region': '横县',
  272. 'native_place': '广西壮族自治区南宁市横县', 'native_place_province': '广西壮族自治区', 'native_place_city': '南宁市',
  273. 'native_place_region': '横县', 'blood_type': '', 'religion': '无宗教信仰'}
  274. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  275. def test_case_06_0(self):
  276. self.case_06_0(0)
  277. def test_case_06_1(self):
  278. self.case_06_0(1)
  279. def test_case_06_2(self):
  280. self.case_06_0(2)
  281. def test_case_06_3(self):
  282. self.case_06_0(3)
  283. def case_16_0(self, orientation=0):
  284. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  285. image_path = '../images/clean/0/16_img.jpg'
  286. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  287. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '张景威',
  288. 'id': '120222199610247017', 'gender': '男', 'birthplace': '天津市市辖区武清区',
  289. 'birthplace_province': '天津市', 'birthplace_city': '市辖区', 'birthplace_region': '武清区',
  290. 'native_place': '天津市市辖区武清区', 'native_place_province': '天津市', 'native_place_city': '市辖区',
  291. 'native_place_region': '武清区', 'blood_type': '', 'religion': ''}
  292. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  293. def test_case_16_0(self):
  294. self.case_16_0(0)
  295. def test_case_16_1(self):
  296. self.case_16_0(1)
  297. def test_case_16_2(self):
  298. self.case_16_0(2)
  299. def test_case_16_3(self):
  300. self.case_16_0(3)
  301. def case_14_0(self, orientation=0):
  302. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  303. image_path = '../images/clean/0/14_img.jpg'
  304. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  305. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '陈鑫',
  306. 'id': '640382199605273716', 'gender': '男', 'birthplace': '宁夏回族自治区银川市灵武市',
  307. 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '银川市', 'birthplace_region': '灵武市',
  308. 'native_place': '宁夏回族自治区银川市灵武市', 'native_place_province': '宁夏回族自治区', 'native_place_city': '银川市',
  309. 'native_place_region': '灵武市', 'blood_type': '不明', 'religion': '伊斯兰教'}
  310. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  311. def test_case_14_0(self):
  312. self.case_14_0(0)
  313. def test_case_14_1(self):
  314. self.case_14_0(1)
  315. def test_case_14_2(self):
  316. self.case_14_0(2)
  317. def test_case_14_3(self):
  318. self.case_14_0(3)
  319. def case_04_0(self, orientation=0):
  320. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  321. image_path = '../images/clean/0/04_img.jpg'
  322. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  323. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '刘振荣',
  324. 'id': '152125630420052', 'gender': '女', 'birthplace': '内蒙古自治区牙克石市',
  325. 'birthplace_province': '内蒙古自治区', 'birthplace_city': '牙克石市', 'birthplace_region': '',
  326. 'native_place': '河北省衡水市阜城县', 'native_place_province': '河北省', 'native_place_city': '衡水市',
  327. 'native_place_region': '阜城县', 'blood_type': 'A型', 'religion': '无'}
  328. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  329. def test_case_04_0(self):
  330. self.case_04_0(0)
  331. def test_case_04_1(self):
  332. self.case_04_0(1)
  333. def test_case_04_2(self):
  334. self.case_04_0(2)
  335. def test_case_04_3(self):
  336. self.case_04_0(3)
  337. def case_05_0(self, orientation=0):
  338. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  339. image_path = '../images/clean/0/05_img.jpg'
  340. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  341. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '宋国军',
  342. 'id': '152125610820051', 'gender': '男', 'birthplace': '内蒙古自治区', 'birthplace_province': '内蒙古自治区',
  343. 'birthplace_city': '', 'birthplace_region': '', 'native_place': '河南省三门峡市陕县',
  344. 'native_place_province': '河南省', 'native_place_city': '三门峡市', 'native_place_region': '陕县',
  345. 'blood_type': 'A型', 'religion': '无'}
  346. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  347. def test_case_05_0(self):
  348. self.case_05_0(0)
  349. def test_case_05_1(self):
  350. self.case_05_0(1)
  351. def test_case_05_2(self):
  352. self.case_05_0(2)
  353. def test_case_05_3(self):
  354. self.case_05_0(3)
  355. def case_15_0(self, orientation=0):
  356. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  357. image_path = '../images/clean/0/15_img.jpg'
  358. json_input = {'status': '000', 'orientation': orientation, 'type': '', 'address': '', 'address_province': '',
  359. 'address_city': '', 'address_region': '', 'address_detail': '', 'name': '马宁',
  360. 'id': '64038219981016341x', 'gender': '男', 'birthplace': '宁夏回族自治区灵武市吴四队',
  361. 'birthplace_province': '宁夏回族自治区', 'birthplace_city': '灵武市', 'birthplace_region': '吴四队',
  362. 'native_place': '宁夏回族自治区', 'native_place_province': '宁夏回族自治区', 'native_place_city': '',
  363. 'native_place_region': '', 'blood_type': 'B型', 'religion': ''}
  364. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  365. def test_case_15_0(self):
  366. self.case_15_0(0)
  367. def test_case_15_1(self):
  368. self.case_15_0(1)
  369. def test_case_15_2(self):
  370. self.case_15_0(2)
  371. def test_case_15_3(self):
  372. self.case_15_0(3)