test_interface.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import base64
  2. from pathlib import Path
  3. import requests
  4. import unittest
  5. # 测试test环境
  6. test_url = 'http://aihub-test.digitalyili.com/aiSquare/openApi/reasoning-services/rlocrxm'
  7. test_header = {
  8. 'Authorization': 'Bearer 9679c2b3-b90b-4029-a3c7-f347b4d242f7'
  9. }
  10. def send_request(image_path,suffix):
  11. with open(image_path, 'rb') as f:
  12. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  13. r = requests.post(f'{test_url}+{suffix}', json={'image': img_str, 'image_type': '0'}, headers=test_header)
  14. return r.json()
  15. class TestOcr(unittest.TestCase):
  16. def _helper(self, image_path, suffix):
  17. root = Path(__file__).parent
  18. image_path = str(root / image_path)
  19. r = send_request(image_path, suffix)
  20. self.assertIn('result', r, f'{suffix} test error')
  21. def test_bank(self):
  22. image_path = './image/bankcard.jpg'
  23. suffix = '/yhksb/bankcard'
  24. self._helper(image_path, suffix)
  25. def test_idcard(self):
  26. image_path = './image/idcard.jpg'
  27. suffix = '/sfzsb/idcard'
  28. self._helper(image_path, suffix)
  29. def test_regbook(self):
  30. image_path = './image/regbook.jpg'
  31. suffix = '/hkbsb/regbook'
  32. self._helper(image_path, suffix)
  33. def test_schoolcert(self):
  34. image_path = './image/schoolcert.jpg'
  35. suffix = '/xxw/schoolcert'
  36. self._helper(image_path, suffix)
  37. # 测试sb环境
  38. url = 'http://aihub.digitalyili.com/aiSquare/openApi/reasoning-services/rlocrxm'
  39. header = {
  40. 'Content-Type': 'application/json',
  41. 'Authorization': 'Bearer dcae8cc6-0e49-4db8-a2d2-94ef84da3636'
  42. }
  43. def send_sb_request(image_path,suffix):
  44. with open(image_path, 'rb') as f:
  45. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  46. r = requests.post(f'{url}{suffix}', json={'image': img_str, 'image_type': '0'}, headers=header)
  47. return r.json()
  48. class TestSbOcr(unittest.TestCase):
  49. def _helper1(self, image_path, suffix):
  50. root = Path(__file__).parent
  51. image_path = str(root / image_path)
  52. r = send_sb_request(image_path, suffix)
  53. self.assertIn('result', r, f'{suffix} test error')
  54. def test_bank(self):
  55. image_path = './image/bankcard.jpg'
  56. suffix = '/yhksb/bankcard'
  57. self._helper1(image_path, suffix)
  58. def test_idcard(self):
  59. image_path = './image/idcard.jpg'
  60. suffix = '/sfzsb/idcard'
  61. self._helper1(image_path, suffix)
  62. def test_regbook(self):
  63. # 暂未开通
  64. image_path = './image/regbook.jpg'
  65. suffix = '/hkbsb/regbook'
  66. self._helper1(image_path, suffix)
  67. def test_schoolcert(self):
  68. image_path = './image/schoolcert.jpg'
  69. suffix = '/xxw/schoolcert'
  70. self._helper1(image_path, suffix)