clear_1_test.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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=1):
  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_02_0(self, orientation=0):
  50. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  51. image_path = '../images/clean/1/02_img.jpg'
  52. json_input = {'status': '000', 'orientation': 0, 'name': '徐科', 'gender': '男', 'admission_time': '2010年09月01日', 'education_time': '2013年06月30日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '武汉职业技术学院', 'major': '摄影摄像技术', 'number': '108341201306417803'}
  53. json_input['orientation'] = orientation
  54. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  55. def test_case_02_0(self):
  56. self.case_02_0(0)
  57. def test_case_02_1(self):
  58. self.case_02_0(1)
  59. def test_case_02_2(self):
  60. self.case_02_0(2)
  61. def test_case_02_3(self):
  62. self.case_02_0(3)
  63. def case_17_0(self, orientation=0):
  64. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  65. image_path = '../images/clean/1/17_img.jpg'
  66. json_input = {'status': '000', 'orientation': 0, 'name': '陈默涵', 'gender': '女', 'admission_time': '2018年09月05日', 'education_time': '2021年06月25日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '新疆农业职业技术学院', 'major': '食品营养与检测', 'number': '109951202106002702'}
  67. json_input['orientation'] = orientation
  68. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  69. def test_case_17_0(self):
  70. self.case_17_0(0)
  71. def test_case_17_1(self):
  72. self.case_17_0(1)
  73. def test_case_17_2(self):
  74. self.case_17_0(2)
  75. def test_case_17_3(self):
  76. self.case_17_0(3)
  77. def case_12_0(self, orientation=0):
  78. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  79. image_path = '../images/clean/1/12_img.jpg'
  80. json_input = {'status': '000', 'orientation': 0, 'name': '张聪', 'gender': '男', 'admission_time': '2008年09月01日', 'education_time': '2011年06月30日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '湖北工业大学商贸学院', 'major': '计算机应用技术', 'number': '132471201106331235'}
  81. json_input['orientation'] = orientation
  82. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  83. def test_case_12_0(self):
  84. self.case_12_0(0)
  85. def test_case_12_1(self):
  86. self.case_12_0(1)
  87. def test_case_12_2(self):
  88. self.case_12_0(2)
  89. def test_case_12_3(self):
  90. self.case_12_0(3)
  91. def case_06_0(self, orientation=0):
  92. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  93. image_path = '../images/clean/1/06_img.jpg'
  94. json_input = {'status': '000', 'orientation': 0, 'name': '李婷婷', 'gender': '女', 'admission_time': '2015年09月01日', 'education_time': '2019年07月01日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '广西财经学院', 'major': '商务英语', 'number': '115481201905007155'}
  95. json_input['orientation'] = orientation
  96. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  97. def test_case_06_0(self):
  98. self.case_06_0(0)
  99. def test_case_06_1(self):
  100. self.case_06_0(1)
  101. def test_case_06_2(self):
  102. self.case_06_0(2)
  103. def test_case_06_3(self):
  104. self.case_06_0(3)
  105. def case_04_0(self, orientation=0):
  106. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  107. image_path = '../images/clean/1/04_img.jpg'
  108. json_input = {'status': '000', 'orientation': 0, 'name': '卢超', 'gender': '男', 'admission_time': '2013年09月01日', 'education_time': '2016年06月30日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '鄂东职业技术学院', 'major': '建筑工程技术', 'number': '137971201606511857'}
  109. json_input['orientation'] = orientation
  110. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  111. def test_case_04_0(self):
  112. self.case_04_0(0)
  113. def test_case_04_1(self):
  114. self.case_04_0(1)
  115. def test_case_04_2(self):
  116. self.case_04_0(2)
  117. def test_case_04_3(self):
  118. self.case_04_0(3)
  119. def case_07_0(self, orientation=0):
  120. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  121. image_path = '../images/clean/1/07_img.jpg'
  122. json_input = {'status': '000', 'orientation': 0, 'name': '罗建亮', 'gender': '男', 'admission_time': '2008年11月01日', 'education_time': '2011年07月05日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '百色职业学院', 'major': '机电一体化技术', 'number': '140681201106000733'}
  123. json_input['orientation'] = orientation
  124. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  125. def test_case_07_0(self):
  126. self.case_07_0(0)
  127. def test_case_07_1(self):
  128. self.case_07_0(1)
  129. def test_case_07_2(self):
  130. self.case_07_0(2)
  131. def test_case_07_3(self):
  132. self.case_07_0(3)
  133. def case_05_0(self, orientation=0):
  134. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  135. image_path = '../images/clean/1/05_img.jpg'
  136. json_input = {'status': '000', 'orientation': 0, 'name': '苏丽欢', 'gender': '男', 'admission_time': '2012年09月01日', 'education_time': '2015年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '广西民族师范学院', 'major': '汉语', 'number': '106041201506002280'}
  137. json_input['orientation'] = orientation
  138. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  139. def test_case_05_0(self):
  140. self.case_05_0(0)
  141. def test_case_05_1(self):
  142. self.case_05_0(1)
  143. def test_case_05_2(self):
  144. self.case_05_0(2)
  145. def test_case_05_3(self):
  146. self.case_05_0(3)
  147. def case_14_0(self, orientation=0):
  148. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  149. image_path = '../images/clean/1/14_img.jpg'
  150. json_input = {'status': '000', 'orientation': 0, 'name': '郭伟', 'gender': '男', 'admission_time': '2004年09月14日', 'education_time': '2007年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '山东劳动职业技术学院', 'major': '机械制造工艺及设备', 'number': '123291200706402393'}
  151. json_input['orientation'] = orientation
  152. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  153. def test_case_14_0(self):
  154. self.case_14_0(0)
  155. def test_case_14_1(self):
  156. self.case_14_0(1)
  157. def test_case_14_2(self):
  158. self.case_14_0(2)
  159. def test_case_14_3(self):
  160. self.case_14_0(3)
  161. def case_10_0(self, orientation=0):
  162. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  163. image_path = '../images/clean/1/10_img.jpg'
  164. json_input = {'status': '000', 'orientation': 0, 'name': '韩昊哲', 'gender': '男', 'admission_time': '2017年09月01日', 'education_time': '2020年07月15日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '西安交通工程学院', 'major': '铁道供电技术', 'number': '135691202006002464'}
  165. json_input['orientation'] = orientation
  166. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  167. def test_case_10_0(self):
  168. self.case_10_0(0)
  169. def test_case_10_1(self):
  170. self.case_10_0(1)
  171. def test_case_10_2(self):
  172. self.case_10_0(2)
  173. def test_case_10_3(self):
  174. self.case_10_0(3)
  175. def case_08_0(self, orientation=0):
  176. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  177. image_path = '../images/clean/1/08_img.jpg'
  178. json_input = {'status': '000', 'orientation': 0, 'name': '罗华鼎', 'gender': '男', 'admission_time': '2015年09月15日', 'education_time': '2018年06月30日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '广西交通职业技术学院', 'major': '建筑设计技术', 'number': '123561201806001431'}
  179. json_input['orientation'] = orientation
  180. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  181. def test_case_08_0(self):
  182. self.case_08_0(0)
  183. def test_case_08_1(self):
  184. self.case_08_0(1)
  185. def test_case_08_2(self):
  186. self.case_08_0(2)
  187. def test_case_08_3(self):
  188. self.case_08_0(3)
  189. def case_13_0(self, orientation=0):
  190. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  191. image_path = '../images/clean/1/13_img.jpg'
  192. json_input = {'status': '000', 'orientation': 0, 'name': '郑红超', 'gender': '男', 'admission_time': '2011年09月01日', 'education_time': '2014年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '漯河职业技术学院', 'major': '市场营销', 'number': '108351201406002687'}
  193. json_input['orientation'] = orientation
  194. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  195. def test_case_13_0(self):
  196. self.case_13_0(0)
  197. def test_case_13_1(self):
  198. self.case_13_0(1)
  199. def test_case_13_2(self):
  200. self.case_13_0(2)
  201. def test_case_13_3(self):
  202. self.case_13_0(3)
  203. def case_11_0(self, orientation=0):
  204. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  205. image_path = '../images/clean/1/11_img.jpg'
  206. json_input = {'status': '000', 'orientation': 0, 'name': '张广渊', 'gender': '女', 'admission_time': '2007年09月01日', 'education_time': '2011年06月30日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '武汉工业学院工商学院', 'major': '国际经济与贸易', 'number': '132411201105872980'}
  207. json_input['orientation'] = orientation
  208. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  209. def test_case_11_0(self):
  210. self.case_11_0(0)
  211. def test_case_11_1(self):
  212. self.case_11_0(1)
  213. def test_case_11_2(self):
  214. self.case_11_0(2)
  215. def test_case_11_3(self):
  216. self.case_11_0(3)
  217. def case_09_0(self, orientation=0):
  218. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  219. image_path = '../images/clean/1/09_img.jpg'
  220. json_input = {'status': '000', 'orientation': 0, 'name': '段有知', 'gender': '男', 'admission_time': '2014年09月01日', 'education_time': '2018年06月30日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '武汉理工大学', 'major': '测控技术与仪器', 'number': '104971201805735616'}
  221. json_input['orientation'] = orientation
  222. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  223. def test_case_09_0(self):
  224. self.case_09_0(0)
  225. def test_case_09_1(self):
  226. self.case_09_0(1)
  227. def test_case_09_2(self):
  228. self.case_09_0(2)
  229. def test_case_09_3(self):
  230. self.case_09_0(3)
  231. def case_03_0(self, orientation=0):
  232. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  233. image_path = '../images/clean/1/03_img.jpg'
  234. json_input = {'status': '000', 'orientation': 0, 'name': '许星皓', 'gender': '', 'admission_time': '', 'education_time': '2012年12月30日', 'education_level': '专科', 'education_type': '高等教育自学考试', 'learning_type': '', 'school': '海南科技职业学院', 'major': '数控技术应用(专科)', 'number': '66460183101004424'}
  235. json_input['orientation'] = orientation
  236. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  237. def test_case_03_0(self):
  238. self.case_03_0(0)
  239. def test_case_03_1(self):
  240. self.case_03_0(1)
  241. def test_case_03_2(self):
  242. self.case_03_0(2)
  243. def test_case_03_3(self):
  244. self.case_03_0(3)
  245. def case_01_0(self, orientation=0):
  246. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  247. image_path = '../images/clean/1/01_img.jpg'
  248. json_input = {'status': '000', 'orientation': 0, 'name': '薛峰', 'gender': '男', 'admission_time': '2009年09月01日', 'education_time': '2013年06月30日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '武汉纺织大学外经贸学院', 'major': '高分子材料与工程', 'number': '132401201305799592'}
  249. json_input['orientation'] = orientation
  250. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  251. def test_case_01_0(self):
  252. self.case_01_0(0)
  253. def test_case_01_1(self):
  254. self.case_01_0(1)
  255. def test_case_01_2(self):
  256. self.case_01_0(2)
  257. def test_case_01_3(self):
  258. self.case_01_0(3)
  259. def case_15_0(self, orientation=0):
  260. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  261. image_path = '../images/clean/1/15_img.jpg'
  262. json_input = {'status': '000', 'orientation': 0, 'name': '马兴', 'gender': '男', 'admission_time': '2013年09月10日', 'education_time': '2016年07月01日', 'education_level': '专科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '天津中德应用技术大学', 'major': '物联网应用技术', 'number': '121051201606002822'}
  263. json_input['orientation'] = orientation
  264. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  265. def test_case_15_0(self):
  266. self.case_15_0(0)
  267. def test_case_15_1(self):
  268. self.case_15_0(1)
  269. def test_case_15_2(self):
  270. self.case_15_0(2)
  271. def test_case_15_3(self):
  272. self.case_15_0(3)
  273. def case_16_0(self, orientation=0):
  274. dict_orientation = {0: None, 1: 0, 2: 1, 3: 2}
  275. image_path = '../images/clean/1/16_img.jpg'
  276. json_input = {'status': '000', 'orientation': 0, 'name': '杨雪凌', 'gender': '男', 'admission_time': '2015年08月02日', 'education_time': '2019年07月01日', 'education_level': '本科', 'education_type': '普通高等教育', 'learning_type': '普通全日制', 'school': '云南民族大学', 'major': '市场营销', 'number': '106911201905004742'}
  277. json_input['orientation'] = orientation
  278. self._helper(image_path, ResultItem(**json_input), dict_orientation[orientation])
  279. def test_case_16_0(self):
  280. self.case_16_0(0)
  281. def test_case_16_1(self):
  282. self.case_16_0(1)
  283. def test_case_16_2(self):
  284. self.case_16_0(2)
  285. def test_case_16_3(self):
  286. self.case_16_0(3)