ocr_test.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. # 带线框识别比较差 例:6.27_02.png
  8. def send_request(image_path, image_type='1'):
  9. with open(image_path, 'rb') as f:
  10. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  11. r = requests.post(f'{url}/ocr_system/schoolcert', json={'image': img_str, 'image_type': image_type})
  12. print(r.json())
  13. return r.json()
  14. @dataclass
  15. class ResultItem:
  16. status: str
  17. orientation: int
  18. name: str
  19. gender: str
  20. admission_time: str
  21. education_time: str
  22. education_level: str
  23. education_type: str
  24. learning_type: str
  25. school: str
  26. major: str
  27. number: str
  28. class TestSchoolCertOcr(unittest.TestCase):
  29. def _helper(self, image_path, item: ResultItem, image_type='1'):
  30. root = Path(__file__).parent
  31. image_path = str(root / image_path)
  32. r = send_request(image_path, image_type)
  33. self.assertEqual(item, ResultItem(status=r['status'],
  34. orientation=r['result']['orientation'],
  35. name=r['result']['name']['text'],
  36. gender=r['result']['gender']['text'],
  37. admission_time=r['result']['admission_time']['text'],
  38. education_time=r['result']['education_time']['text'],
  39. education_type=r['result']['education_type']['text'],
  40. education_level=r['result']['education_level']['text'],
  41. learning_type=r['result']['learning_type']['text'],
  42. school=r['result']['school']['text'],
  43. major=r['result']['major']['text'],
  44. number=r['result']['number']['text']
  45. ))
  46. # 方向、姓名错误
  47. def test_case_01_0(self):
  48. image_path = '../images/01_0.png'
  49. self._helper(image_path, ResultItem(status='000',
  50. orientation=0,
  51. name='肖亮',
  52. gender='男',
  53. admission_time='2012年9月8日',
  54. education_time='2018年6月28日',
  55. education_level='本科',
  56. education_type='普通高等教育',
  57. learning_type='普通全日制',
  58. school='济南大学泉城学院',
  59. major='市场营销',
  60. number='140021201805000882'))
  61. # 方向、姓名错误
  62. def test_case_01_90(self):
  63. image_path = '../images/01_90.png'
  64. self._helper(image_path, ResultItem(status='000',
  65. orientation=1,
  66. name='肖亮',
  67. gender='男',
  68. admission_time='2012年9月8日',
  69. education_time='2018年6月28日',
  70. education_level='本科',
  71. education_type='普通高等教育',
  72. learning_type='普通全日制',
  73. school='济南大学泉城学院',
  74. major='市场营销',
  75. number='140021201805000882'))
  76. # 方向、姓名错误
  77. def test_case_01_180(self):
  78. image_path = '../images/01_180.png'
  79. self._helper(image_path, ResultItem(status='000',
  80. orientation=2,
  81. name='肖亮',
  82. gender='男',
  83. admission_time='2012年9月8日',
  84. education_time='2018年6月28日',
  85. education_level='本科',
  86. education_type='普通高等教育',
  87. learning_type='普通全日制',
  88. school='济南大学泉城学院',
  89. major='市场营销',
  90. number='140021201805000882'))
  91. # 方向、姓名错误
  92. def test_case_01_270(self):
  93. image_path = '../images/01_270.png'
  94. self._helper(image_path, ResultItem(status='000',
  95. orientation=3,
  96. name='肖亮',
  97. gender='男',
  98. admission_time='2012年9月8日',
  99. education_time='2018年6月28日',
  100. education_level='本科',
  101. education_type='普通高等教育',
  102. learning_type='普通全日制',
  103. school='济南大学泉城学院',
  104. major='市场营销',
  105. number='140021201805000882'))
  106. # 姓名错误
  107. def test_case_02(self):
  108. image_path = '../images/02.png'
  109. self._helper(image_path, ResultItem(status='000',
  110. orientation=0,
  111. name='覃俊',
  112. gender='男',
  113. admission_time='2000年9月1日',
  114. education_time='2004年6月21日',
  115. education_level='本科',
  116. education_type='普通',
  117. learning_type='普通全日制',
  118. school='江苏科技大学',
  119. major='会计学',
  120. number='102891200405001172'))
  121. # 方向、姓名错误
  122. def test_case_04(self):
  123. image_path = '../images/04.jpeg'
  124. self._helper(image_path, ResultItem(status='000',
  125. orientation=0,
  126. name='汤怡青',
  127. gender='女',
  128. admission_time='2010年09月01日',
  129. education_time='2014年06月28日',
  130. education_level='本科',
  131. education_type='普通高等教育',
  132. learning_type='普通全日制',
  133. school='北京交通大学',
  134. major='软件工程',
  135. number='100041201405003059'))
  136. # 方向、姓名错误
  137. def test_629_01(self):
  138. image_path = '../images/6.29_01.jpg'
  139. self._helper(image_path, ResultItem(status='000',
  140. orientation=0,
  141. name='刘杰',
  142. gender='男',
  143. admission_time='2011年09月01日',
  144. education_time='2015年06月25日',
  145. education_level='本科',
  146. education_type='普通高等教育',
  147. learning_type='普通全日制',
  148. school='山东理工大学',
  149. major='英语(师范类)',
  150. number='104331201505208429'))
  151. # 姓名错误
  152. def test_629_02(self):
  153. image_path = '../images/6.29_02.png'
  154. self._helper(image_path, ResultItem(status='000',
  155. orientation=0,
  156. name='张开天',
  157. gender='男',
  158. admission_time='2017年09月01日',
  159. education_time='2021年06月15日',
  160. education_level='本科',
  161. education_type='普通高等教育',
  162. learning_type='普通全日制',
  163. school='甘肃农业大学',
  164. major='农林经济管理',
  165. number='622301199710247376'))
  166. # 入学时间、毕业时间、教育类型错误
  167. def test_629_03(self):
  168. image_path = '../images/6.29_03.jpg'
  169. self._helper(image_path, ResultItem(status='000',
  170. orientation=0,
  171. name='陈贵龙',
  172. gender='男',
  173. admission_time='2019年09月08日',
  174. education_time='2023年06月30日',
  175. education_level='本科',
  176. education_type='普通高等教育',
  177. learning_type='普通全日制',
  178. school='浙江树人学院',
  179. major='通信工程',
  180. number='342423200102156552'))