本文以错题本算法为例
SDK源码地址:Github
安装SDK

  1. pip install leqi-algorithm-mic-sdk

1. 准备

  1. import time
  2. from algorithm_mic_sdk import error
  3. from algorithm_mic_sdk.algorithms.topic import Topic
  4. from algorithm_mic_sdk.auth import AuthInfo
  5. from algorithm_mic_sdk.base import Base
  6. from algorithm_mic_sdk.tools import FileInfo
  7. filename = '1.jpg' # 图片文件路径
  8. corners = [[0, 0], [2160, 0], [0, 3840], [2160, 3840]]
  9. host = 'http://gateway.algo.leqi.us' # 算法host地址
  10. user_name = 'your_name'
  11. password = 'your_password'
  12. # 初始化授权信息类
  13. auth_info = AuthInfo(host, user_name, password)

2. 请求接口

该算法中oss_file必填参数为一图片文件,因此需要先将图片上传至阿里云OSS上.

  1. 构建FileInfo 对象

    1. file_info = FileInfo.for_file_bytes(open(filename, 'rb').read())
  2. 实例化Topic 类,调用其asynchronous_request方法来异步请求算法.

    对于耗时短的算法可以直接调用同步请求方法synchronous_request

    1. # 创建算法对象,各个算法的各个参数具体含义可见文档
    2. topic = Topic(auth_info=auth_info, file=file_info, corners=corners)
    3. resp = topic.asynchronous_request()
    4. task_id = resp.task_id
  3. 轮询调用get_results方法获取任务结果.

    get_results方法对于所有algorithm_mic_sdk.base.Base类及其子类都可以使用,与具体的算法无关.

  1. while True:
  2. # 根据任务ID获取结果,此处可以也可以用:
  3. # resp = Base(auth_info).get_results(task_id)
  4. resp = topic.get_results(task_id)
  5. if resp.gateway_code==1001:
  6. print('任务处理异常 ', resp.gateway_error)
  7. exit()
  8. elif resp.gateway_code==1002:
  9. # 任务还在处理中
  10. time.sleep(1)
  11. continue
  12. elif resp.gateway_code!=1000:
  13. print('未知状态码', resp.gateway_error)
  14. exit()
  15. # 任务处理成功
  16. result_im_oss_name = resp.result['result_im_oss_name']
  17. box = resp.result['box']
  1. 拿到任务结果后,调用get_file方法获取文件二进制数据

    get_file方法对于所有algorithm_mic_sdk.base.Base类及其子类都可以使用,与具体的算法无关.

  1. # 可选,下载文件到本地存储
  2. with open("result.jpg", 'wb') as f:
  3. f.write(topic.get_file(result_im_oss_name).content)

3. 完整代码

  1. import time
  2. from algorithm_mic_sdk.algorithms.topic import Topic
  3. from algorithm_mic_sdk.auth import AuthInfo
  4. from algorithm_mic_sdk.tools import FileInfo
  5. filename = '1.jpg' # 图片文件路径
  6. corners = [[0, 0], [2160, 0], [0, 3840], [2160, 3840]]
  7. host = 'http://gateway.algo.leqi.us' # 算法host地址
  8. user_name = 'your_name'
  9. password = 'your_password'
  10. # 初始化授权信息类
  11. auth_info = AuthInfo(host, user_name, password)
  12. file_info = FileInfo.for_file_bytes(open(filename, 'rb').read())
  13. # 创建算法对象,各个算法的各个参数具体含义可见文档
  14. topic = Topic(auth_info=auth_info, file=file_info, corners=corners)
  15. resp = topic.asynchronous_request()
  16. task_id = resp.task_id
  17. while True:
  18. # 根据任务ID获取结果,此处可以也可以用:
  19. # resp = Base(auth_info).get_results(task_id)
  20. resp = topic.get_results(task_id)
  21. if resp.gateway_code == 1001:
  22. print('任务处理异常 ', resp.gateway_error)
  23. exit()
  24. elif resp.gateway_code == 1002:
  25. print('任务还在处理中')
  26. time.sleep(1)
  27. continue
  28. elif resp.gateway_code != 1000:
  29. print('未知状态码', resp.gateway_error)
  30. exit()
  31. print('任务处理成功')
  32. result_im_oss_name = resp.result['result_im_oss_name']
  33. box = resp.result['box']
  34. # 可选,下载文件到本地存储
  35. with open("result.jpg", 'wb') as f:
  36. f.write(topic.get_file(result_im_oss_name))
  37. break