true_1_test.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. def send_request(image_path, image_type):
  8. with open(image_path, 'rb') as f:
  9. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  10. r = requests.post(f'{url}/ocr_system/schoolcert', json={'image': img_str, 'image_type': image_type})
  11. print(r.json())
  12. return r.json()
  13. @dataclass
  14. class ResultItem:
  15. status: str
  16. orientation: int
  17. name: str
  18. gender: str
  19. admission_time: str
  20. education_time: str
  21. education_level: str
  22. education_type: str
  23. learning_type: str
  24. school: str
  25. major: str
  26. number: str
  27. class TestSchoolCertOcr(unittest.TestCase):
  28. def _helper(self, image_path, item: ResultItem, image_type='1'):
  29. root = Path(__file__).parent
  30. image_path = str(root / image_path)
  31. r = send_request(image_path, image_type)
  32. self.assertEqual(item, ResultItem(status=r['status'],
  33. orientation=r['result']['orientation'],
  34. name=r['result']['name']['text'],
  35. gender=r['result']['gender']['text'],
  36. admission_time=r['result']['admission_time']['text'],
  37. education_time=r['result']['education_time']['text'],
  38. education_type=r['result']['education_type']['text'],
  39. education_level=r['result']['education_level']['text'],
  40. learning_type=r['result']['learning_type']['text'],
  41. school=r['result']['school']['text'],
  42. major=r['result']['major']['text'],
  43. number=r['result']['number']['text']
  44. ))
  45. def test_case_01_0(self):
  46. image_path = '../images/1/10045201A11学籍学历证明001.JPG'
  47. self._helper(image_path, ResultItem(status='000',
  48. orientation=0,
  49. name='徐科',
  50. gender='男',
  51. admission_time='2010年09月01日',
  52. education_time='2013年06月30日',
  53. education_level='专科',
  54. education_type='普通高等教育',
  55. learning_type='普通全日制',
  56. school='武汉职业技术学院',
  57. major='摄影摄像技术',
  58. number='108341201306417803'))
  59. def test_case_02_0(self):
  60. image_path = '../images/1/10047539A11学籍学历证明001.jpg'
  61. self._helper(image_path, ResultItem(status='000',
  62. orientation=0,
  63. name='苏丽欢',
  64. gender='男',
  65. admission_time='2012年09月01日',
  66. education_time='2015年07月01日',
  67. education_level='专科',
  68. education_type='普通高等教育',
  69. learning_type='普通全日制',
  70. school='广西民族师范学院',
  71. major='汉语',
  72. number='106041201506002280'))
  73. def test_case_03_0(self):
  74. image_path = '../images/1/10048160A11学籍学历证明001.jpg'
  75. self._helper(image_path, ResultItem(status='000',
  76. orientation=0,
  77. name='罗建亮',
  78. gender='男',
  79. admission_time='2008年11月01日',
  80. education_time='2011年07月05日',
  81. education_level='专科',
  82. education_type='普通高等教育',
  83. learning_type='普通全日制',
  84. school='百色职业学院',
  85. major='机电一体化技术',
  86. number='140681201106000733'))
  87. def test_case_04_0(self):
  88. image_path = '../images/1/10053240A11学籍学历证明001.jpg'
  89. self._helper(image_path, ResultItem(status='000',
  90. orientation=0,
  91. name='张广渊',
  92. gender='女',
  93. admission_time='2007年09月01日',
  94. education_time='2011年06月30日',
  95. education_level='本科',
  96. education_type='普通高等教育',
  97. learning_type='普通全日制',
  98. school='武汉工业学院工商学院',
  99. major='国际经济与贸易',
  100. number='132411201105872980'))