id727_0_test.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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:18080'
  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/idcard', 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. name: str
  23. id: str
  24. ethnicity: str
  25. gender: str
  26. birthday: str
  27. address: str
  28. address_province: str
  29. address_city: str
  30. address_region: str
  31. address_detail: str
  32. expire_date:str
  33. class TestSchoolCertOcr(unittest.TestCase):
  34. def _helper(self, image_path, item: ResultItem, rotate=None,image_type=0):
  35. root = Path(__file__).parent
  36. image_path = str(root / image_path)
  37. r = send_request(image_path, image_type, rotate)
  38. self.assertEqual(item, ResultItem(status=r['status'],
  39. orientation=r['result']['orientation'],
  40. name=r['result']['name']['text'],
  41. id=r['result']['id']['text'],
  42. ethnicity=r['result']['ethnicity']['text'],
  43. gender=r['result']['gender']['text'],
  44. birthday=r['result']['birthday']['text'],
  45. address=r['result']['address']['text'],
  46. address_province=r['result']['address_province']['text'],
  47. address_city=r['result']['address_city']['text'],
  48. address_region=r['result']['address_region']['text'],
  49. address_detail=r['result']['address_detail']['text'],
  50. expire_date=r['result']['expire_date']['text']))
  51. def case_10_0(self, orientation=0):
  52. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  53. image_path = '../images/test_img/7.27/0/10_img.jpg'
  54. json_input = {'status': '000', 'orientation': 0, 'name': '赵子嫣', 'id': '341621200001024945', 'ethnicity': '汉', 'gender': '女', 'birthday': '2000年01月02日', 'address': '安徽省涡阳县马店镇吴府行政村刘王自然村246号', 'address_province': '安徽省', 'address_city': '亳州市', 'address_region': '涡阳县', 'address_detail': '马店镇吴府行政村刘王自然村246号', 'expire_date': ''}
  55. json_input['orientation'] = orientation
  56. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  57. def test_case_10_0(self):
  58. self.case_10_0(0)
  59. def test_case_10_1(self):
  60. self.case_10_0(1)
  61. def test_case_10_2(self):
  62. self.case_10_0(2)
  63. def test_case_10_3(self):
  64. self.case_10_0(3)
  65. def case_09_0(self, orientation=0):
  66. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  67. image_path = '../images/test_img/7.27/0/09_img.jpg'
  68. json_input = {'status': '000', 'orientation': 0, 'name': '郑志聪', 'id': '441521199808234751', 'ethnicity': '汉', 'gender': '男', 'birthday': '1998年08月23日', 'address': '广东省海丰县海城镇城西社区居民委员会西中街一巷242号', 'address_province': '广东省', 'address_city': '汕尾市', 'address_region': '海丰县', 'address_detail': '海城镇城西社区居民委员会西中街一巷242号', 'expire_date': ''}
  69. json_input['orientation'] = orientation
  70. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  71. def test_case_09_0(self):
  72. self.case_09_0(0)
  73. def test_case_09_1(self):
  74. self.case_09_0(1)
  75. def test_case_09_2(self):
  76. self.case_09_0(2)
  77. def test_case_09_3(self):
  78. self.case_09_0(3)
  79. def case_08_0(self, orientation=0):
  80. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  81. image_path = '../images/test_img/7.27/0/08_img.jpg'
  82. json_input = {'status': '000', 'orientation': 0, 'name': '路文强', 'id': '610111199412202514', 'ethnicity': '汉', 'gender': '男', 'birthday': '1994年12月20日', 'address': '西安市灞桥区路家湾1组65号', 'address_province': '陕西省', 'address_city': '西安市', 'address_region': '灞桥区', 'address_detail': '路家湾1组65号', 'expire_date': ''}
  83. json_input['orientation'] = orientation
  84. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  85. def test_case_08_0(self):
  86. self.case_08_0(0)
  87. def test_case_08_1(self):
  88. self.case_08_0(1)
  89. def test_case_08_2(self):
  90. self.case_08_0(2)
  91. def test_case_08_3(self):
  92. self.case_08_0(3)
  93. def case_01_0(self, orientation=0):
  94. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  95. image_path = '../images/test_img/7.27/0/01_img.jpg'
  96. json_input = {'status': '000', 'orientation': 0, 'name': '刘璐', 'id': '152104199803025225', 'ethnicity': '汉', 'gender': '女', 'birthday': '1998年03月02日', 'address': '内蒙古牙克石市库都尔镇小九亚街林北巷121号', 'address_province': '内蒙古自治区', 'address_city': '呼伦贝尔市', 'address_region': '牙克石市', 'address_detail': '库都尔镇小九亚街林北巷121号', 'expire_date': ''}
  97. json_input['orientation'] = orientation
  98. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  99. def test_case_01_0(self):
  100. self.case_01_0(0)
  101. def test_case_01_1(self):
  102. self.case_01_0(1)
  103. def test_case_01_2(self):
  104. self.case_01_0(2)
  105. def test_case_01_3(self):
  106. self.case_01_0(3)
  107. def case_11_0(self, orientation=0):
  108. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  109. image_path = '../images/test_img/7.27/0/11_img.jpg'
  110. json_input = {'status': '000', 'orientation': 0, 'name': '刘方杰', 'id': '360421199608230039', 'ethnicity': '汉', 'gender': '男', 'birthday': '1996年08月23日', 'address': '江西省九江市九江县沙河街镇庐山西路388号', 'address_province': '江西省', 'address_city': '九江市', 'address_region': '九江县', 'address_detail': '沙河街镇庐山西路388号', 'expire_date': ''}
  111. json_input['orientation'] = orientation
  112. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  113. def test_case_11_0(self):
  114. self.case_11_0(0)
  115. def test_case_11_1(self):
  116. self.case_11_0(1)
  117. def test_case_11_2(self):
  118. self.case_11_0(2)
  119. def test_case_11_3(self):
  120. self.case_11_0(3)
  121. def case_13_0(self, orientation=0):
  122. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  123. image_path = '../images/test_img/7.27/0/13_img.jpg'
  124. json_input = {'status': '000', 'orientation': 0, 'name': '菅恒硕', 'id': '372330199906226155', 'ethnicity': '汉', 'gender': '男', 'birthday': '1999年06月22日', 'address': '山东省邹平县魏桥镇菅家村14号', 'address_province': '山东省', 'address_city': '滨州市', 'address_region': '邹平县', 'address_detail': '魏桥镇营家村14号', 'expire_date': ''}
  125. json_input['orientation'] = orientation
  126. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  127. def test_case_13_0(self):
  128. self.case_13_0(0)
  129. def test_case_13_1(self):
  130. self.case_13_0(1)
  131. def test_case_13_2(self):
  132. self.case_13_0(2)
  133. def test_case_13_3(self):
  134. self.case_13_0(3)
  135. def case_03_0(self, orientation=0):
  136. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  137. image_path = '../images/test_img/7.27/0/03_img.jpg'
  138. json_input = {'status': '000', 'orientation': 0, 'name': '李琳', 'id': '130535199611180029', 'ethnicity': '汉', 'gender': '女', 'birthday': '1996年11月18日', 'address': '河北省邢台市临西县影院南路5号监察局家属院002号', 'address_province': '河北省', 'address_city': '邢台市', 'address_region': '临西县', 'address_detail': '影院南路5号监察局家属院002号', 'expire_date': ''}
  139. json_input['orientation'] = orientation
  140. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  141. def test_case_03_0(self):
  142. self.case_03_0(0)
  143. def test_case_03_1(self):
  144. self.case_03_0(1)
  145. def test_case_03_2(self):
  146. self.case_03_0(2)
  147. def test_case_03_3(self):
  148. self.case_03_0(3)
  149. def case_02_0(self, orientation=0):
  150. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  151. image_path = '../images/test_img/7.27/0/02_img.jpg'
  152. json_input = {'status': '000', 'orientation': 0, 'name': '雷盈盈', 'id': '511181198404100026', 'ethnicity': '汉', 'gender': '女', 'birthday': '1984年04月10日', 'address': '四川省峨眉山市绥山镇西正街135号附2号', 'address_province': '四川省', 'address_city': '乐山市', 'address_region': '峨眉山市', 'address_detail': '绥山镇西正街135号附2号', 'expire_date': ''}
  153. json_input['orientation'] = orientation
  154. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  155. def test_case_02_0(self):
  156. self.case_02_0(0)
  157. def test_case_02_1(self):
  158. self.case_02_0(1)
  159. def test_case_02_2(self):
  160. self.case_02_0(2)
  161. def test_case_02_3(self):
  162. self.case_02_0(3)
  163. def case_12_0(self, orientation=0):
  164. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  165. image_path = '../images/test_img/7.27/0/12_img.jpg'
  166. json_input = {'status': '000', 'orientation': 0, 'name': '达日罕', 'id': '150422200001286055', 'ethnicity': '蒙古', 'gender': '男', 'birthday': '2000年01月28日', 'address': '内蒙古赤峰市巴林左旗查干哈达苏木红光嘎查胡北沟屯一组', 'address_province': '内蒙古自治区', 'address_city': '赤峰市', 'address_region': '巴林左旗', 'address_detail': '查干哈达苏木红光嘎查胡北沟屯一组', 'expire_date': ''}
  167. json_input['orientation'] = orientation
  168. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  169. def test_case_12_0(self):
  170. self.case_12_0(0)
  171. def test_case_12_1(self):
  172. self.case_12_0(1)
  173. def test_case_12_2(self):
  174. self.case_12_0(2)
  175. def test_case_12_3(self):
  176. self.case_12_0(3)
  177. def case_07_0(self, orientation=0):
  178. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  179. image_path = '../images/test_img/7.27/0/07_img.jpg'
  180. json_input = {'status': '000', 'orientation': 0, 'name': '韩凯峰', 'id': '152822199806244811', 'ethnicity': '汉', 'gender': '男', 'birthday': '1998年06月24日', 'address': '内蒙古巴彦淖尔市五原县新公中镇永生村三社064号', 'address_province': '内蒙古自治区', 'address_city': '巴彦淖尔市', 'address_region': '五原县', 'address_detail': '新公中镇永生村三社064号', 'expire_date': ''}
  181. json_input['orientation'] = orientation
  182. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  183. def test_case_07_0(self):
  184. self.case_07_0(0)
  185. def test_case_07_1(self):
  186. self.case_07_0(1)
  187. def test_case_07_2(self):
  188. self.case_07_0(2)
  189. def test_case_07_3(self):
  190. self.case_07_0(3)
  191. def case_06_0(self, orientation=0):
  192. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  193. image_path = '../images/test_img/7.27/0/06_img.jpg'
  194. json_input = {'status': '000', 'orientation': 0, 'name': '李秉桦', 'id': '452730200006265615', 'ethnicity': '壮', 'gender': '男', 'birthday': '2000年06月26日', 'address': '南宁市江南区五一路105号6栋4单元107房', 'address_province': '广西壮族自治区', 'address_city': '南宁市', 'address_region': '江南区', 'address_detail': '五一路105号6栋4单元107房', 'expire_date': ''}
  195. json_input['orientation'] = orientation
  196. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  197. def test_case_06_0(self):
  198. self.case_06_0(0)
  199. def test_case_06_1(self):
  200. self.case_06_0(1)
  201. def test_case_06_2(self):
  202. self.case_06_0(2)
  203. def test_case_06_3(self):
  204. self.case_06_0(3)
  205. def case_14_0(self, orientation=0):
  206. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  207. image_path = '../images/test_img/7.27/0/14_img.jpg'
  208. json_input = {'status': '000', 'orientation': 0, 'name': '张劲松', 'id': '150125199902090217', 'ethnicity': '汉', 'gender': '男', 'birthday': '1999年02月09日', 'address': '呼和浩特市赛罕区呼伦贝尔南路雅格星城A3号楼2502号', 'address_province': '内蒙古自治区', 'address_city': '呼和浩特市', 'address_region': '赛罕区', 'address_detail': '呼伦贝尔南路雅格星城A3号楼2502号', 'expire_date': ''}
  209. json_input['orientation'] = orientation
  210. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  211. def test_case_14_0(self):
  212. self.case_14_0(0)
  213. def test_case_14_1(self):
  214. self.case_14_0(1)
  215. def test_case_14_2(self):
  216. self.case_14_0(2)
  217. def test_case_14_3(self):
  218. self.case_14_0(3)
  219. def case_04_0(self, orientation=0):
  220. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  221. image_path = '../images/test_img/7.27/0/04_img.jpg'
  222. json_input = {'status': '000', 'orientation': 0, 'name': '裴浩森', 'id': '142729200010121851', 'ethnicity': '汉', 'gender': '男', 'birthday': '2000年10月12日', 'address': '山西省闻喜县礼元镇裴柏村118号', 'address_province': '山西省', 'address_city': '运城市', 'address_region': '闻喜县', 'address_detail': '礼元镇装柏村118号', 'expire_date': ''}
  223. json_input['orientation'] = orientation
  224. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  225. def test_case_04_0(self):
  226. self.case_04_0(0)
  227. def test_case_04_1(self):
  228. self.case_04_0(1)
  229. def test_case_04_2(self):
  230. self.case_04_0(2)
  231. def test_case_04_3(self):
  232. self.case_04_0(3)
  233. def case_05_0(self, orientation=0):
  234. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  235. image_path = '../images/test_img/7.27/0/05_img.jpg'
  236. json_input = {'status': '000', 'orientation': 0, 'name': '杨赫日轮', 'id': '152322199902094318', 'ethnicity': '蒙古', 'gender': '男', 'birthday': '1999年02月09日', 'address': '内蒙古通辽市科尔沁左翼中旗舍伯吐镇中熬本台嘎查86号', 'address_province': '内蒙古自治区', 'address_city': '通辽市', 'address_region': '科尔沁左翼中旗', 'address_detail': '舍伯吐镇中熬本台嘎查86号', 'expire_date': ''}
  237. json_input['orientation'] = orientation
  238. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  239. def test_case_05_0(self):
  240. self.case_05_0(0)
  241. def test_case_05_1(self):
  242. self.case_05_0(1)
  243. def test_case_05_2(self):
  244. self.case_05_0(2)
  245. def test_case_05_3(self):
  246. self.case_05_0(3)