clear_2_test.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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://localhost:8080'
  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/schoolcert', 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. gender: str
  24. admission_time: str
  25. education_time: str
  26. education_level: str
  27. education_type: str
  28. learning_type: str
  29. school: str
  30. major: str
  31. number: str
  32. class TestSchoolCertOcr(unittest.TestCase):
  33. def _helper(self, image_path, item: ResultItem, rotate=None,image_type=2):
  34. root = Path(__file__).parent
  35. image_path = str(root / image_path)
  36. r = send_request(image_path, image_type, rotate)
  37. self.assertEqual(item, ResultItem(status=r['status'],
  38. orientation=r['result']['orientation'],
  39. name=r['result']['name']['text'],
  40. gender=r['result']['gender']['text'],
  41. admission_time=r['result']['admission_time']['text'],
  42. education_time=r['result']['education_time']['text'],
  43. education_type=r['result']['education_type']['text'],
  44. education_level=r['result']['education_level']['text'],
  45. learning_type=r['result']['learning_type']['text'],
  46. school=r['result']['school']['text'],
  47. major=r['result']['major']['text'],
  48. number=r['result']['number']['text']))
  49. def case_18_0(self, orientation=0):
  50. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  51. image_path = '../images/clean/2/18_img.jpg'
  52. json_input = {'status': '000', 'orientation': 0, 'name': '何昊轩', 'gender': '男', 'admission_time': '2019年9月1日', 'education_time': '2022年7月1日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '包头轻工职业技术学院', 'major': '农产品加工与质量检测', 'number': '126711202206000186'}
  53. json_input['orientation'] = orientation
  54. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  55. def test_case_18_0(self):
  56. self.case_18_0(0)
  57. def test_case_18_1(self):
  58. self.case_18_0(1)
  59. def test_case_18_2(self):
  60. self.case_18_0(2)
  61. def test_case_18_3(self):
  62. self.case_18_0(3)
  63. def case_02_0(self, orientation=0):
  64. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  65. image_path = '../images/clean/2/02_img.jpg'
  66. json_input = {'status': '000', 'orientation': 0, 'name': '刘杰', 'gender': '男', 'admission_time': '2011年9月1日', 'education_time': '2015年6月25日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '山东理工大学', 'major': '英语(师范类)', 'number': '104331201505208429'}
  67. json_input['orientation'] = orientation
  68. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  69. def test_case_02_0(self):
  70. self.case_02_0(0)
  71. def test_case_02_1(self):
  72. self.case_02_0(1)
  73. def test_case_02_2(self):
  74. self.case_02_0(2)
  75. def test_case_02_3(self):
  76. self.case_02_0(3)
  77. def case_17_0(self, orientation=0):
  78. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  79. image_path = '../images/clean/2/17_img.jpg'
  80. json_input = {'status': '000', 'orientation': 0, 'name': '吕彩微', 'gender': '女', 'admission_time': '2018年9月1日', 'education_time': '2022年6月10日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '河北农业大学', 'major': '食品科学与工程', 'number': '100861202205003702'}
  81. json_input['orientation'] = orientation
  82. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  83. def test_case_17_0(self):
  84. self.case_17_0(0)
  85. def test_case_17_1(self):
  86. self.case_17_0(1)
  87. def test_case_17_2(self):
  88. self.case_17_0(2)
  89. def test_case_17_3(self):
  90. self.case_17_0(3)
  91. def case_12_0(self, orientation=0):
  92. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  93. image_path = '../images/clean/2/12_img.jpg'
  94. json_input = {'status': '000', 'orientation': 0, 'name': '辛宇琦', 'gender': '男', 'admission_time': '2015年9月12日', 'education_time': '2018年7月1日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '黑龙江生物科技职业学院', 'major': '生物制药技术', 'number': '134471201806000362'}
  95. json_input['orientation'] = orientation
  96. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  97. def test_case_12_0(self):
  98. self.case_12_0(0)
  99. def test_case_12_1(self):
  100. self.case_12_0(1)
  101. def test_case_12_2(self):
  102. self.case_12_0(2)
  103. def test_case_12_3(self):
  104. self.case_12_0(3)
  105. def case_06_0(self, orientation=0):
  106. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  107. image_path = '../images/clean/2/06_img.jpg'
  108. json_input = {'status': '000', 'orientation': 0, 'name': '韩坤鹏', 'gender': '男', 'admission_time': '2017年9月1日', 'education_time': '2021年6月21日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '四川大学', 'major': '国民经济管理', 'number': '106101202105000788'}
  109. json_input['orientation'] = orientation
  110. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  111. def test_case_06_0(self):
  112. self.case_06_0(0)
  113. def test_case_06_1(self):
  114. self.case_06_0(1)
  115. def test_case_06_2(self):
  116. self.case_06_0(2)
  117. def test_case_06_3(self):
  118. self.case_06_0(3)
  119. def case_04_0(self, orientation=0):
  120. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  121. image_path = '../images/clean/2/04_img.jpg'
  122. json_input = {'status': '000', 'orientation': 0, 'name': '丁志勇', 'gender': '男', 'admission_time': '2014年9月1日', 'education_time': '2017年7月5日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '内蒙古电子信息职业技术学院', 'major': '园林技术(园林施工与监理方向)', 'number': '126731201706001174'}
  123. json_input['orientation'] = orientation
  124. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  125. def test_case_04_0(self):
  126. self.case_04_0(0)
  127. def test_case_04_1(self):
  128. self.case_04_0(1)
  129. def test_case_04_2(self):
  130. self.case_04_0(2)
  131. def test_case_04_3(self):
  132. self.case_04_0(3)
  133. def case_19_0(self, orientation=0):
  134. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  135. image_path = '../images/clean/2/19_img.jpg'
  136. json_input = {'status': '000', 'orientation': 0, 'name': '袁石磊', 'gender': '男', 'admission_time': '2020年9月1日', 'education_time': '2022年6月22日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '内蒙古工业大学', 'major': '过程装备与控制工程', 'number': '101281202205103089'}
  137. json_input['orientation'] = orientation
  138. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  139. def test_case_19_0(self):
  140. self.case_19_0(0)
  141. def test_case_19_1(self):
  142. self.case_19_0(1)
  143. def test_case_19_2(self):
  144. self.case_19_0(2)
  145. def test_case_19_3(self):
  146. self.case_19_0(3)
  147. def case_07_0(self, orientation=0):
  148. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  149. image_path = '../images/clean/2/07_img.jpg'
  150. json_input = {'status': '000', 'orientation': 0, 'name': '刘坤', 'gender': '男', 'admission_time': '2012年9月15日', 'education_time': '2015年6月24日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '山东商业职业技术学院', 'major': '计算机信息管理', 'number': '108321201506004465'}
  151. json_input['orientation'] = orientation
  152. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  153. def test_case_07_0(self):
  154. self.case_07_0(0)
  155. def test_case_07_1(self):
  156. self.case_07_0(1)
  157. def test_case_07_2(self):
  158. self.case_07_0(2)
  159. def test_case_07_3(self):
  160. self.case_07_0(3)
  161. def case_20_0(self, orientation=0):
  162. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  163. image_path = '../images/clean/2/20_img.jpg'
  164. json_input = {'status': '000', 'orientation': 0, 'name': '赵艳鹏', 'gender': '男', 'admission_time': '2010年9月1日', 'education_time': '2014年7月10日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '内蒙古科技大学', 'major': '交通工程', 'number': '101271201405005140'}
  165. json_input['orientation'] = orientation
  166. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  167. def test_case_20_0(self):
  168. self.case_20_0(0)
  169. def test_case_20_1(self):
  170. self.case_20_0(1)
  171. def test_case_20_2(self):
  172. self.case_20_0(2)
  173. def test_case_20_3(self):
  174. self.case_20_0(3)
  175. def case_05_0(self, orientation=0):
  176. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  177. image_path = '../images/clean/2/05_img.jpg'
  178. json_input = {'status': '000', 'orientation': 0, 'name': '赵吉磊', 'gender': '男', 'admission_time': '2016年9月1日', 'education_time': '2019年6月22日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '青岛黄海学院', 'major': '市场营销', 'number': '133201201906002050'}
  179. json_input['orientation'] = orientation
  180. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  181. def test_case_05_0(self):
  182. self.case_05_0(0)
  183. def test_case_05_1(self):
  184. self.case_05_0(1)
  185. def test_case_05_2(self):
  186. self.case_05_0(2)
  187. def test_case_05_3(self):
  188. self.case_05_0(3)
  189. def case_14_0(self, orientation=0):
  190. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  191. image_path = '../images/clean/2/14_img.jpg'
  192. json_input = {'status': '000', 'orientation': 0, 'name': '王宏', 'gender': '男', 'admission_time': '2012年9月2日', 'education_time': '2016年6月17日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '河北工程大学', 'major': '地理信息系统', 'number': '100761201605102054'}
  193. json_input['orientation'] = orientation
  194. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  195. def test_case_14_0(self):
  196. self.case_14_0(0)
  197. def test_case_14_1(self):
  198. self.case_14_0(1)
  199. def test_case_14_2(self):
  200. self.case_14_0(2)
  201. def test_case_14_3(self):
  202. self.case_14_0(3)
  203. def case_10_0(self, orientation=0):
  204. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  205. image_path = '../images/clean/2/10_img.jpg'
  206. json_input = {'status': '000', 'orientation': 0, 'name': '王飞鹏', 'gender': '男', 'admission_time': '2016年9月5日', 'education_time': '2020年7月5日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '烟台大学文经学院', 'major': '财务管理', 'number': '133591202005100874'}
  207. json_input['orientation'] = orientation
  208. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  209. def test_case_10_0(self):
  210. self.case_10_0(0)
  211. def test_case_10_1(self):
  212. self.case_10_0(1)
  213. def test_case_10_2(self):
  214. self.case_10_0(2)
  215. def test_case_10_3(self):
  216. self.case_10_0(3)
  217. def case_08_0(self, orientation=0):
  218. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  219. image_path = '../images/clean/2/08_img.jpg'
  220. json_input = {'status': '000', 'orientation': 0, 'name': '马海龙', 'gender': '男', 'admission_time': '2018年9月1日', 'education_time': '2021年6月17日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '青岛港湾职业技术学院', 'major': '汽车营销与服务', 'number': '130141202106003583'}
  221. json_input['orientation'] = orientation
  222. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  223. def test_case_08_0(self):
  224. self.case_08_0(0)
  225. def test_case_08_1(self):
  226. self.case_08_0(1)
  227. def test_case_08_2(self):
  228. self.case_08_0(2)
  229. def test_case_08_3(self):
  230. self.case_08_0(3)
  231. def case_13_0(self, orientation=0):
  232. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  233. image_path = '../images/clean/2/13_img.jpg'
  234. json_input = {'status': '000', 'orientation': 0, 'name': '边阔', 'gender': '男', 'admission_time': '2015年9月7日', 'education_time': '2020年7月1日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '安徽师范大学', 'major': '人力资源管理', 'number': '103701201905007317'}
  235. json_input['orientation'] = orientation
  236. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  237. def test_case_13_0(self):
  238. self.case_13_0(0)
  239. def test_case_13_1(self):
  240. self.case_13_0(1)
  241. def test_case_13_2(self):
  242. self.case_13_0(2)
  243. def test_case_13_3(self):
  244. self.case_13_0(3)
  245. def case_11_0(self, orientation=0):
  246. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  247. image_path = '../images/clean/2/11_img.jpg'
  248. json_input = {'status': '000', 'orientation': 0, 'name': '赵一凡', 'gender': '男', 'admission_time': '2016年9月1日', 'education_time': '2020年6月15日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '陇东学院', 'major': '自动化', 'number': '107381202005002439'}
  249. json_input['orientation'] = orientation
  250. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  251. def test_case_11_0(self):
  252. self.case_11_0(0)
  253. def test_case_11_1(self):
  254. self.case_11_0(1)
  255. def test_case_11_2(self):
  256. self.case_11_0(2)
  257. def test_case_11_3(self):
  258. self.case_11_0(3)
  259. def case_09_0(self, orientation=0):
  260. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  261. image_path = '../images/clean/2/09_img.jpg'
  262. json_input = {'status': '000', 'orientation': 0, 'name': '袁跃', 'gender': '男', 'admission_time': '2016年9月10日', 'education_time': '2020年6月24日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '山东科技大学', 'major': '高分子材料与工程', 'number': '104241202005002452'}
  263. json_input['orientation'] = orientation
  264. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  265. def test_case_09_0(self):
  266. self.case_09_0(0)
  267. def test_case_09_1(self):
  268. self.case_09_0(1)
  269. def test_case_09_2(self):
  270. self.case_09_0(2)
  271. def test_case_09_3(self):
  272. self.case_09_0(3)
  273. def case_03_0(self, orientation=0):
  274. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  275. image_path = '../images/clean/2/03_img.jpg'
  276. json_input = {'status': '000', 'orientation': 0, 'name': '黄志昌', 'gender': '男', 'admission_time': '2013年9月1日', 'education_time': '2016年6月30日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '漳州职业技术学院', 'major': '市场营销', 'number': '113141201606002183'}
  277. json_input['orientation'] = orientation
  278. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  279. def test_case_03_0(self):
  280. self.case_03_0(0)
  281. def test_case_03_1(self):
  282. self.case_03_0(1)
  283. def test_case_03_2(self):
  284. self.case_03_0(2)
  285. def test_case_03_3(self):
  286. self.case_03_0(3)
  287. def case_01_0(self, orientation=0):
  288. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  289. image_path = '../images/clean/2/01_img.jpg'
  290. json_input = {'status': '000', 'orientation': 0, 'name': '肖亮', 'gender': '男', 'admission_time': '2012年9月8日', 'education_time': '2018年6月28日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '济南大学泉城学院', 'major': '市场营销', 'number': '140021201805000882'}
  291. json_input['orientation'] = orientation
  292. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  293. def test_case_01_0(self):
  294. self.case_01_0(0)
  295. def test_case_01_1(self):
  296. self.case_01_0(1)
  297. def test_case_01_2(self):
  298. self.case_01_0(2)
  299. def test_case_01_3(self):
  300. self.case_01_0(3)
  301. def case_15_0(self, orientation=0):
  302. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  303. image_path = '../images/clean/2/15_img.jpg'
  304. json_input = {'status': '000', 'orientation': 0, 'name': '吕鹏', 'gender': '男', 'admission_time': '2014年9月13日', 'education_time': '2018年6月29日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '山东建筑大学', 'major': '房地产开发与管理', 'number': '104301201805011922'}
  305. json_input['orientation'] = orientation
  306. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  307. def test_case_15_0(self):
  308. self.case_15_0(0)
  309. def test_case_15_1(self):
  310. self.case_15_0(1)
  311. def test_case_15_2(self):
  312. self.case_15_0(2)
  313. def test_case_15_3(self):
  314. self.case_15_0(3)
  315. def case_16_0(self, orientation=0):
  316. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  317. image_path = '../images/clean/2/16_img.jpg'
  318. json_input = {'status': '000', 'orientation': 0, 'name': '李旻熹', 'gender': '女', 'admission_time': '2016年9月1日', 'education_time': '2020年6月10日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '北京体育大学', 'major': '运动训练', 'number': '100431202005000716'}
  319. json_input['orientation'] = orientation
  320. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  321. def test_case_16_0(self):
  322. self.case_16_0(0)
  323. def test_case_16_1(self):
  324. self.case_16_0(1)
  325. def test_case_16_2(self):
  326. self.case_16_0(2)
  327. def test_case_16_3(self):
  328. self.case_16_0(3)