clear_0_test.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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=0):
  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/0/18_img.jpg'
  52. json_input = {'status': '000', 'orientation': 0, 'name': '罗子龙', 'gender': '男', 'admission_time': '2018年09月08日', 'education_time': '2022年06月16日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '江苏理工学院', 'major': '汽车服务工程(师范)', 'number': ''}
  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/0/02_img.jpg'
  66. json_input = {'status': '000', 'orientation': 0, 'name': '张文豪', 'gender': '男', 'admission_time': '2018年09月08日', 'education_time': '2022年07月01日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '昌吉学院', 'major': '自动化', 'number': ''}
  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/0/17_img.jpg'
  80. json_input = {'status': '000', 'orientation': 0, 'name': '周志伟', 'gender': '男', 'admission_time': '2019年09月06日', 'education_time': '2022年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '呼和浩特职业学院', 'major': '药品生物技术', 'number': ''}
  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/0/12_img.jpg'
  94. json_input = {'status': '000', 'orientation': 0, 'name': '王新宇', 'gender': '男', 'admission_time': '2019年09月01日', 'education_time': '2022年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '包头轻工职业技术学院', 'major': '农产品加工与质量检测', 'number': ''}
  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/0/06_img.jpg'
  108. json_input = {'status': '000', 'orientation': 0, 'name': '王振东', 'gender': '男', 'admission_time': '2014年09月11日', 'education_time': '2017年06月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '浙江水利水电学院', 'major': '物流管理', 'number': ''}
  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/0/04_img.jpg'
  122. json_input = {'status': '000', 'orientation': 0, 'name': '殷梦媛', 'gender': '女', 'admission_time': '2021年03月01日', 'education_time': '2024年03月01日', 'education_level': '本科', 'education_type': '成人高等教育', 'learning_type': '函授', 'school': '西北师范大学', 'major': '工商管理', 'number': ''}
  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/0/19_img.jpg'
  136. json_input = {'status': '000', 'orientation': 0, 'name': '杨超玉', 'gender': '男', 'admission_time': '2018年09月15日', 'education_time': '2022年06月30日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '浙江师范大学', 'major': '旅游管理', 'number': ''}
  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/0/07_img.jpg'
  150. json_input = {'status': '000', 'orientation': 0, 'name': '唐久桐', 'gender': '男', 'admission_time': '2017年09月01日', 'education_time': '2021年06月28日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '辽宁科技大学', 'major': '无机非金属材料工程', 'number': ''}
  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/0/20_img.jpg'
  164. json_input = {'status': '000', 'orientation': 0, 'name': '热依木别克·阿曼太', 'gender': '男', 'admission_time': '2020年10月26日', 'education_time': '2023年06月30日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '新疆建设职业技术学院', 'major': '房地产检测与估价', 'number': ''}
  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/0/05_img.jpg'
  178. json_input = {'status': '000', 'orientation': 0, 'name': '陈海涛', 'gender': '男', 'admission_time': '2005年09月01日', 'education_time': '2009年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '江苏广播电视大学', 'major': '应用电子技术', 'number': ''}
  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/0/14_img.jpg'
  192. json_input = {'status': '000', 'orientation': 0, 'name': '屈程祥', 'gender': '', 'admission_time': '2019年09月01日', 'education_time': '2022年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '包头轻工职业技术学院', 'major': '农产品加工与质量检测', 'number': ''}
  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/0/10_img.jpg'
  206. json_input = {'status': '000', 'orientation': 0, 'name': '陈可欣', 'gender': '女', 'admission_time': '2018年09月01日', 'education_time': '2022年07月03日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '内蒙古财经大学', 'major': '税收学', 'number': ''}
  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/0/08_img.jpg'
  220. json_input = {'status': '000', 'orientation': 0, 'name': '王锦', 'gender': '男', 'admission_time': '2019年09月06日', 'education_time': '2022年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '黄冈职业技术学院', 'major': '计算机应用技术', 'number': ''}
  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/0/13_img.jpg'
  234. json_input = {'status': '000', 'orientation': 0, 'name': '韩玉龙', 'gender': '男', 'admission_time': '2018年09月12日', 'education_time': '2022年07月01日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '内蒙古科技大学', 'major': '应用物理学', 'number': ''}
  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/0/11_img.jpg'
  248. json_input = {'status': '000', 'orientation': 0, 'name': '段泽煜', 'gender': '男', 'admission_time': '2018年09月01日', 'education_time': '2022年07月08日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '内蒙古农业大学', 'major': '信息管理与信息系统', 'number': ''}
  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/0/09_img.jpg'
  262. json_input = {'status': '000', 'orientation': 0, 'name': '常忠花', 'gender': '女', 'admission_time': '2017年09月16日', 'education_time': '2020年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '山西运城农业职业技术学院', 'major': '畜牧兽医', 'number': ''}
  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/0/03_img.jpg'
  276. json_input = {'status': '000', 'orientation': 0, 'name': '刘疆林', 'gender': '男', 'admission_time': '2018年09月08日', 'education_time': '2022年07月01日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '昌吉学院', 'major': '自动化', 'number': ''}
  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/0/01_img.jpg'
  290. json_input = {'status': '000', 'orientation': 0, 'name': '李铭清', 'gender': '男', 'admission_time': '2018年09月09日', 'education_time': '2022年06月30日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '武汉设计工程学院', 'major': '食品质量与安全', 'number': ''}
  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/0/15_img.jpg'
  304. json_input = {'status': '000', 'orientation': 0, 'name': '卢雪', 'gender': '女', 'admission_time': '2019年09月01日', 'education_time': '2022年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '乌兰察布医学高等专科学校', 'major': '助产', 'number': ''}
  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/0/16_img.jpg'
  318. json_input = {'status': '000', 'orientation': 0, 'name': '刘烜阳', 'gender': '男', 'admission_time': '2020年09月01日', 'education_time': '2022年07月08日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '内蒙古农业大学', 'major': '车辆工程', 'number': ''}
  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)