文档首页 > > SDK参考> 使用SDK(Python)> 图像标签示例

图像标签示例

分享
更新时间:2020/08/26 GMT+08:00

本服务认证方式有两种方式。本章节以图像标签为例,分别使用AK/SKToken认证方式进行调用。图像标签对应URI:POST /v1.0/image/tagging。

AK/SK认证方式使用SDK

  1. “image_tagging_aksk_demo.py”文件中配置“app_key”“app_secret”。示例代码如下:
    1
    2
    3
    4
    5
    6
    7
    8
    if __name__ == '__main__':
        # Services currently support North China-Beijing(cn-north-4)
        init_global_env('cn-north-4') 
        #
        # access image tagging,post data by ak,sk
        #
        app_key = '*************'
        app_secret = '************'
    
  2. 图像标签支持调用文件调用URL两种调用方式。需要您在“image_tagging_aksk_demo.py”文件中修改图片文件的本地路径或URL路径。
    • 调用文件,需修改图片的本地路径。将“encode_to_base64”“data/image-tagging-demo.jpg”替换为需要识别的图片路径。示例代码如下:
      1
      2
      3
          # call interface use the file
          result = image_tagging_aksk(app_key, app_secret, encode_to_base64('data/image-tagging-demo.jpg'), '', 'zh', 5, 60)
          print(result)
      
    • 调用URL,需修改图片URL路径。将“demo_data_url”的图片URL路径替换为需要识别的图片URL路径。示例代码如下:
      1
      2
      3
      4
          demo_data_url = 'https://sdk-obs-source-save.obs.cn-north-4.myhuaweicloud.com/tagging-normal.jpg'
          # call interface use the url
          result = image_tagging_aksk(app_key, app_secret, '', demo_data_url, 'zh', 5, 30)
          print(result)
      
  3. 运行“image_tagging_aksk_demo.py”,识别结果输出到控制台表示执行成功。
    1
    2
    3
    {"result":{"tags":[{"confidence":"98.38","i18n_tag":{"en":"Person","zh":"人"},"tag":"人","type":"object"},{"confidence":"97.12","i18n_tag":{"en":"Children","zh":"儿童"},"tag":"儿童","type":"object"},{"confidence":"96.39","i18n_tag":{"en":"Sandbox","zh":"(供儿童玩的)沙坑"},"tag":"(供儿童玩的)沙坑","type":"scene"},{"confidence":"89.28","i18n_tag":{"en":"Play","zh":"玩耍"},"tag":"玩耍","type":"object"},{"confidence":"87.99","i18n_tag":{"en":"Toy","zh":"玩具"},"tag":"玩具","type":"object"}]}}
    
    Process finished with exit code 0
    

Token认证方式使用SDK

  1. “image_tagging_aksk_demo.py”文件中配置实际注册的用户名“user_name”和密码“password”,非IAM登录用户user_name和account_name一致。示例代码如下:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    if __name__ == '__main__':
    
        # Services currently support North China-Beijing(cn-north-4)
        init_global_env('cn-north-4')
    
        #
        # access image tagging,post data by token
        #
        user_name = '******'
        password = '******'
        account_name = '******'  # the same as user_name in commonly use
     
        token = get_token(user_name, password, account_name)
    
  2. 图像标签支持调用文件调用URL两种调用方式。需要在“image_tagging_aksk_demo.py”文件中修改图片文件的本地路径或URL路径。
    • 调用文件,需修改图片的本地路径。将“encode_to_base64”“data/image-tagging-demo.jpg”替换为需要识别的图片路径。示例代码如下:
      1
      2
      3
          # call interface use the file
          result = image_tagging(token, encode_to_base64('data/image-tagging-demo.jpg'), '', 'zh', 5, 60)
          print(result)
      
    • 调用URL,需修改图片URL路径。将“demo_data_url”的图片URL路径替换为需要识别的图片URL路径。示例代码如下:
      1
      2
      3
      4
          demo_data_url = 'https://sdk-obs-source-save.obs.cn-north-4.myhuaweicloud.com/tagging-normal.jpg'
          # call interface use the url
          result = image_tagging(token, '', demo_data_url, 'zh', 5, 30)
          print(result)
      
  3. 运行“image_tagging_aksk_demo.py”,识别结果输出到控制台表示执行成功。
    1
    2
    3
    {"result":{"tags":[{"confidence":"98.38","i18n_tag":{"en":"Person","zh":"人"},"tag":"人","type":"object"},{"confidence":"97.12","i18n_tag":{"en":"Children","zh":"儿童"},"tag":"儿童","type":"object"},{"confidence":"96.39","i18n_tag":{"en":"Sandbox","zh":"(供儿童玩的)沙坑"},"tag":"(供儿童玩的)沙坑","type":"scene"},{"confidence":"89.28","i18n_tag":{"en":"Play","zh":"玩耍"},"tag":"玩耍","type":"object"},{"confidence":"87.99","i18n_tag":{"en":"Toy","zh":"玩具"},"tag":"玩具","type":"object"}]}}
    
    Process finished with exit code 0
    
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!非常感谢您的反馈,我们会继续努力做到更好!
反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问