aliyun.py 884 B

1234567891011121314151617181920212223242526272829
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2024-11-01
  4. ---------
  5. @summary:
  6. ---------
  7. @author: Dzr
  8. """
  9. from base64 import b64encode
  10. import requests
  11. def file_extract(path):
  12. b64_str = b64encode(path.read_bytes()).decode('utf8')
  13. data = {"input": {"image": b64_str}}
  14. url = 'http://1228910937799386.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/ms_eas_c38f0a8d_f314_463b_875d_581a/invoke'
  15. headers = {
  16. 'Content-Type': 'application/json',
  17. 'Authorization': 'ZDljNTk5ZTU3M2U3NzQzOGI5NzJhY2Y2OTI1M2I0NWI5NmVhZjljZA=='
  18. }
  19. # print(b64_str)
  20. # data = {"input":{"image":"http://modelscope.oss-cn-beijing.aliyuncs.com/demo/images/image_ocr_recognition.jpg"}}
  21. response = requests.post(url, headers=headers, json=data, timeout=10)
  22. r_json = response.json()
  23. print(r_json)
  24. return r_json['Data']['text'][0] if r_json and 'Data' in r_json else ''