ocr_test2.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. import unittest
  2. import base64
  3. from dataclasses import dataclass
  4. from pathlib import Path
  5. import requests
  6. # url = 'http://localhost:8080'
  7. url = 'http://aihub.digitalyili.com/aiSquare/openApi/reasoning-services'
  8. header = {
  9. 'Authorization': 'Bearer dcae8cc6-0e49-4db8-a2d2-94ef84da3636'
  10. }
  11. # 带线框识别比较差 例:6.27_02.png
  12. def send_request(image_path, image_type='1'):
  13. with open(image_path, 'rb') as f:
  14. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  15. r = requests.post(f'{url}/rlocrxm/xxw/schoolcert', json={'image': img_str, 'image_type': image_type}, headers=header)
  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, image_type='1'):
  34. root = Path(__file__).parent
  35. image_path = str(root / image_path)
  36. r = send_request(image_path, image_type)
  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. ))
  50. # 0测试
  51. # 方向、专业识别错误
  52. def test_0_01_0(self):
  53. image_path = '../images/all/0/01_0.jpg'
  54. self._helper(image_path, ResultItem(status='000',
  55. orientation=0,
  56. name='王海龙',
  57. gender='男',
  58. admission_time='2018年09月05日',
  59. education_time='2021年06月30日',
  60. education_level='专科',
  61. education_type='普通高等教育',
  62. learning_type='普通全日制',
  63. school='新疆农业职业技术学院',
  64. major='食品营养与检测',
  65. number=''))
  66. # 多种错误
  67. def test_0_01_90(self):
  68. image_path = '../images/all/0/01_90.jpg'
  69. self._helper(image_path, ResultItem(status='000',
  70. orientation=1,
  71. name='王海龙',
  72. gender='男',
  73. admission_time='2018年09月05日',
  74. education_time='2021年06月30日',
  75. education_level='专科',
  76. education_type='普通高等教育',
  77. learning_type='普通全日制',
  78. school='新疆农业职业技术学院',
  79. major='食品营养与检测',
  80. number=''))
  81. # 方向、毕业时间识别错误
  82. def test_0_01_180(self):
  83. image_path = '../images/all/0/01_180.jpg'
  84. self._helper(image_path, ResultItem(status='000',
  85. orientation=2,
  86. name='王海龙',
  87. gender='男',
  88. admission_time='2018年09月05日',
  89. education_time='2021年06月30日',
  90. education_level='专科',
  91. education_type='普通高等教育',
  92. learning_type='普通全日制',
  93. school='新疆农业职业技术学院',
  94. major='食品营养与检测',
  95. number=''))
  96. # 方向、专业识别错误
  97. def test_0_01_270(self):
  98. image_path = '../images/all/0/01_270.jpg'
  99. self._helper(image_path, ResultItem(status='000',
  100. orientation=3,
  101. name='王海龙',
  102. gender='男',
  103. admission_time='2018年09月05日',
  104. education_time='2021年06月30日',
  105. education_level='专科',
  106. education_type='普通高等教育',
  107. learning_type='普通全日制',
  108. school='新疆农业职业技术学院',
  109. major='食品营养与检测',
  110. number=''))
  111. # 方向识别错误
  112. def test_0_02(self):
  113. image_path = '../images/all/0/02.jpg'
  114. self._helper(image_path, ResultItem(status='000',
  115. orientation=0,
  116. name='张博渊',
  117. gender='男',
  118. admission_time='2018年09月12日',
  119. education_time='2022年07月01日',
  120. education_level='本科',
  121. education_type='普通高等教育',
  122. learning_type='普通全日制',
  123. school='内蒙古科技大学',
  124. major='应用化学',
  125. number=''))
  126. # 姓名、学校识别错误
  127. def test_0_03(self):
  128. image_path = '../images/all/0/03.jpg'
  129. self._helper(image_path, ResultItem(status='000',
  130. orientation=0,
  131. name='徐志林',
  132. gender='男',
  133. admission_time='2019年09月01日',
  134. education_time='2021年07月01日',
  135. education_level='专科',
  136. education_type='普通',
  137. learning_type='普通全日制',
  138. school='四川工商职业技术学院',
  139. major='食品生物技术',
  140. number=''))
  141. # 入学时间识别错误
  142. def test_0_05_0(self):
  143. image_path = '../images/all/0/05_0.jpg'
  144. self._helper(image_path, ResultItem(status='000',
  145. orientation=0,
  146. name='金泽丞',
  147. gender='男',
  148. admission_time='2020年09月01日',
  149. education_time='2022年07月10日',
  150. education_level='本科',
  151. education_type='普通',
  152. learning_type='普通全日制',
  153. school='内蒙古科技大学',
  154. major='自动化',
  155. number=''))
  156. # 入学时间识别错误
  157. def test_0_05_90(self):
  158. image_path = '../images/all/0/05_90.jpg'
  159. self._helper(image_path, ResultItem(status='000',
  160. orientation=1,
  161. name='金泽丞',
  162. gender='男',
  163. admission_time='2020年09月01日',
  164. education_time='2022年07月10日',
  165. education_level='本科',
  166. education_type='普通',
  167. learning_type='普通全日制',
  168. school='内蒙古科技大学',
  169. major='自动化',
  170. number=''))
  171. # 多种错误
  172. def test_0_05_180(self):
  173. image_path = '../images/all/0/05_180.jpg'
  174. self._helper(image_path, ResultItem(status='000',
  175. orientation=2,
  176. name='金泽丞',
  177. gender='男',
  178. admission_time='2020年09月01日',
  179. education_time='2022年07月10日',
  180. education_level='本科',
  181. education_type='普通',
  182. learning_type='普通全日制',
  183. school='内蒙古科技大学',
  184. major='自动化',
  185. number=''))
  186. # 入学时间识别错误
  187. def test_0_05_270(self):
  188. image_path = '../images/all/0/05_270.jpg'
  189. self._helper(image_path, ResultItem(status='000',
  190. orientation=3,
  191. name='金泽丞',
  192. gender='男',
  193. admission_time='2020年09月01日',
  194. education_time='2022年07月10日',
  195. education_level='本科',
  196. education_type='普通',
  197. learning_type='普通全日制',
  198. school='内蒙古科技大学',
  199. major='自动化',
  200. number=''))
  201. # 学校识别错误
  202. def test_0_06(self):
  203. image_path = '../images/all/0/06.jpg'
  204. self._helper(image_path, ResultItem(status='000',
  205. orientation=0,
  206. name='张豪',
  207. gender='男',
  208. admission_time='2020年10月10日',
  209. education_time='2022年06月30日',
  210. education_level='专科',
  211. education_type='普通',
  212. learning_type='普通全日制',
  213. school='四川工商职业技术学院',
  214. major='食品生物技术',
  215. number=''))
  216. # 1测试
  217. # 方向、姓名错误
  218. def test_1_01(self):
  219. image_path = '../images/all/1/01.jpg'
  220. self._helper(image_path, ResultItem(status='000',
  221. orientation=0,
  222. name='陈默涵',
  223. gender='女',
  224. admission_time='2018年09月05日',
  225. education_time='2021年06月25日',
  226. education_level='专科',
  227. education_type='普通高等教育',
  228. learning_type='普通全日制',
  229. school='新疆农业职业技术学院',
  230. major='食品营养与检测',
  231. number='109951202106002702'),
  232. '0')
  233. # 多种错误
  234. def test_1_02_0(self):
  235. image_path = '../images/all/1/02_0.png'
  236. self._helper(image_path, ResultItem(status='000',
  237. orientation=0,
  238. name='张敏',
  239. gender='男',
  240. admission_time='2016年09月10日',
  241. education_time='2020年06月30日',
  242. education_level='本科',
  243. education_type='普通高等教育',
  244. learning_type='普通全日制',
  245. school='大连科技学院',
  246. major='机械设计制造及其自动化',
  247. number='132071202005100273'),
  248. '0')
  249. # 多种错误
  250. def test_1_02_90(self):
  251. image_path = '../images/all/1/02_90.png'
  252. self._helper(image_path, ResultItem(status='000',
  253. orientation=1,
  254. name='张敏',
  255. gender='男',
  256. admission_time='2016年09月10日',
  257. education_time='2020年06月30日',
  258. education_level='本科',
  259. education_type='普通高等教育',
  260. learning_type='普通全日制',
  261. school='大连科技学院',
  262. major='机械设计制造及其自动化',
  263. number='132071202005100273'),
  264. '0')
  265. # 多种错误
  266. def test_1_02_180(self):
  267. image_path = '../images/all/1/02_180.png'
  268. self._helper(image_path, ResultItem(status='000',
  269. orientation=2,
  270. name='张敏',
  271. gender='男',
  272. admission_time='2016年09月10日',
  273. education_time='2020年06月30日',
  274. education_level='本科',
  275. education_type='普通高等教育',
  276. learning_type='普通全日制',
  277. school='大连科技学院',
  278. major='机械设计制造及其自动化',
  279. number='132071202005100273'),
  280. '0')
  281. # 多种错误
  282. def test_1_02_270(self):
  283. image_path = '../images/all/1/02_270.png'
  284. self._helper(image_path, ResultItem(status='000',
  285. orientation=3,
  286. name='张敏',
  287. gender='男',
  288. admission_time='2016年09月10日',
  289. education_time='2020年06月30日',
  290. education_level='本科',
  291. education_type='普通高等教育',
  292. learning_type='普通全日制',
  293. school='大连科技学院',
  294. major='机械设计制造及其自动化',
  295. number='132071202005100273'),
  296. '0')
  297. def test_1_03(self):
  298. image_path = '../images/all/1/03.JPG'
  299. self._helper(image_path, ResultItem(status='000',
  300. orientation=0,
  301. name='薛峰',
  302. gender='男',
  303. admission_time='2009年09月01日',
  304. education_time='2013年06月30日',
  305. education_level='本科',
  306. education_type='普通高等教育',
  307. learning_type='普通全日制',
  308. school='武汉纺织大学外经贸学院',
  309. major='高分子材料与工程',
  310. number='132401201305799592'),
  311. '0')
  312. # 多种错误
  313. def test_1_04(self):
  314. image_path = '../images/all/1/04.jpg'
  315. self._helper(image_path, ResultItem(status='000',
  316. orientation=0,
  317. name='许星皓',
  318. gender='男',
  319. admission_time='',
  320. education_time='2012年12月30日',
  321. education_level='专科',
  322. education_type='高等教育自学考试',
  323. learning_type='',
  324. school='海南科技职业学院',
  325. major='数控技术应用(专科)',
  326. number='66460183101004424'),
  327. '0')
  328. # 方向错误
  329. def test_1_05(self):
  330. image_path = '../images/all/1/05.jpg'
  331. self._helper(image_path, ResultItem(status='000',
  332. orientation=0,
  333. name='李婷婷',
  334. gender='女',
  335. admission_time='2015年09月01日',
  336. education_time='2019年07月01日',
  337. education_level='本科',
  338. education_type='普通高等教育',
  339. learning_type='普通全日制',
  340. school='广西财经学院',
  341. major='商务英语',
  342. number='115481201905007155'),
  343. '0')
  344. # 2测试
  345. # miss orientation、number
  346. def test_2_01_0(self):
  347. image_path = '../images/all/2/01_0.png'
  348. self._helper(image_path, ResultItem(status='000',
  349. orientation=0,
  350. name='肖亮',
  351. gender='男',
  352. admission_time='2012年9月8日',
  353. education_time='2018年6月28日',
  354. education_level='本科',
  355. education_type='普通高等教育',
  356. learning_type='普通全日制',
  357. school='济南大学泉城学院',
  358. major='市场营销',
  359. number='140021201805000882'),
  360. '2')
  361. # 多种错误
  362. def test_2_01_90(self):
  363. image_path = '../images/all/2/01_90.png'
  364. self._helper(image_path, ResultItem(status='000',
  365. orientation=1,
  366. name='肖亮',
  367. gender='男',
  368. admission_time='2012年9月8日',
  369. education_time='2018年6月28日',
  370. education_level='本科',
  371. education_type='普通高等教育',
  372. learning_type='普通全日制',
  373. school='济南大学泉城学院',
  374. major='市场营销',
  375. number='140021201805000882'),
  376. '2')
  377. # 多种错误
  378. def test_2_01_180(self):
  379. image_path = '../images/all/2/01_180.png'
  380. self._helper(image_path, ResultItem(status='000',
  381. orientation=2,
  382. name='肖亮',
  383. gender='男',
  384. admission_time='2012年9月8日',
  385. education_time='2018年6月28日',
  386. education_level='本科',
  387. education_type='普通高等教育',
  388. learning_type='普通全日制',
  389. school='济南大学泉城学院',
  390. major='市场营销',
  391. number='140021201805000882'),
  392. '2')
  393. # 多种错误
  394. def test_2_01_270(self):
  395. image_path = '../images/all/2/01_180.png'
  396. self._helper(image_path, ResultItem(status='000',
  397. orientation=3,
  398. name='肖亮',
  399. gender='男',
  400. admission_time='2012年9月8日',
  401. education_time='2018年6月28日',
  402. education_level='本科',
  403. education_type='普通高等教育',
  404. learning_type='普通全日制',
  405. school='济南大学泉城学院',
  406. major='市场营销',
  407. number='140021201805000882'),
  408. '2')
  409. # miss learning_type、number
  410. def test_2_02(self):
  411. image_path = '../images/all/2/02.jpeg'
  412. self._helper(image_path, ResultItem(status='000',
  413. orientation=0,
  414. name='提姆·库克',
  415. gender='男',
  416. admission_time='2001年09月01日',
  417. education_time='2004年07月01日',
  418. education_level='本科',
  419. education_type='高等教育',
  420. learning_type='',
  421. school='河北工业大学',
  422. major='人工智能之机器学习',
  423. number='100801200405001235'),
  424. '2')
  425. # 名字错误
  426. def test_2_03(self):
  427. image_path = '../images/all/2/03.png'
  428. self._helper(image_path, ResultItem(status='000',
  429. orientation=0,
  430. name='胡磊',
  431. gender='男',
  432. admission_time='2012年9月1日',
  433. education_time='2016年7月1日',
  434. education_level='本科',
  435. education_type='普通高等教育',
  436. learning_type='普通全日制',
  437. school='红河学院',
  438. major='经济学',
  439. number='106871201605000620'),
  440. '2')
  441. # 正确
  442. def test_2_04(self):
  443. image_path = '../images/all/2/04.png'
  444. self._helper(image_path, ResultItem(status='000',
  445. orientation=0,
  446. name='孟煜翔',
  447. gender='男',
  448. admission_time='2016年9月3日',
  449. education_time='2019年6月24日',
  450. education_level='专科',
  451. education_type='普通高等教育',
  452. learning_type='普通全日制',
  453. school='山东农业工程学院',
  454. major='环境艺术设计',
  455. number='144391201906001607'),
  456. '2')
  457. # 方向错误
  458. def test_2_05(self):
  459. image_path = '../images/all/2/05.jpg'
  460. self._helper(image_path, ResultItem(status='000',
  461. orientation=0,
  462. name='黄浦村',
  463. gender='男',
  464. admission_time='2011年9月1日',
  465. education_time='2014年7月1日',
  466. education_level='专科',
  467. education_type='普通高等教育',
  468. learning_type='普通全日制',
  469. school='江西工程学院',
  470. major='装潢艺术设计',
  471. number='127661201406001009'),
  472. '2')