文档首页 > > SDK参考> 使用SDK(Python)> SDK调用示例

SDK调用示例

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

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

AK/SK认证方式使用SDK

  1. “moderation_image_aksk_demo.py”文件中配置“app_key”“app_secret”。示例代码如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    if __name__ == '__main__':
        # Services currently support North China-Beijing(cn-north-4)
        init_global_env('cn-north-4')
    
        #
        # access moderation image,post data by ak,sk
        #
        app_key = '*************'
        app_secret = '************'
    
  2. 图像内容审核支持调用文件调用URL两种调用方式。需要您在“moderation_image_aksk_demo.py”文件中修改图片文件的本地路径或URL路径。
    • 调用文件,需修改审核图片的本地路径。将“encode_to_base64”“data/moderation-terrorism.jpg”替换为需要检测的图片路径。示例代码如下:
      1
      2
      3
          # call interface use the local file
          result = moderation_image_aksk(app_key, app_secret, encode_to_base64('data/moderation-terrorism.jpg'), '', ['politics','terrorism'],'')
          print(result)
      
    • 调用URL,需修改图片URL路径。将“demo_data_url”的图片URL路径替换为需要检测的图片URL路径。示例代码如下:
      1
      2
      3
      4
      demo_data_url = 'https://ais-sample-data.obs.cn-north-1.myhuaweicloud.com/terrorism.jpg'
          # call interface use the url
          result = moderation_image_aksk(app_key, app_secret, "", demo_data_url, ['politics','terrorism'], '')
          print(result)
      
  3. 运行“moderation_image_aksk_demo.py”,审核结果输出到控制台表示执行成功。
    1
    2
    3
    4
    {"result":{"detail":{"porn":[{"confidence":1.0,"label":"normal"},{"confidence":0.0,"label":"porn"},{"confidence":0.0,"label":"sexy"}]},"suggestion":"pass","category_suggestions":{"porn":"pass"}}}
    {"result":{"detail":{"porn":[{"confidence":1.0,"label":"normal"},{"confidence":0.0,"label":"porn"},{"confidence":0.0,"label":"sexy"}]},"suggestion":"pass","category_suggestions":{"porn":"pass"}}}
    
    Process finished with exit code 0
    

Token认证方式使用SDK

  1. “moderation_image_token_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 moderation image,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两种调用方式。需要在“moderation_image_token_demo.py”文件中修改图片文件的本地路径或URL路径。
    • 调用文件,需修改审核图片的本地路径。将“encode_to_base64”“data/moderation-terrorism.jpg”替换为需要检测的图片路径。示例代码如下:
      1
      2
      3
          # call interface use the local file
          result = moderation_image(token, encode_to_base64('data/moderation-terrorism.jpg'), '', ['porn'], '')
          print(result)
      
    • 调用URL,需修改图片URL路径。将“demo_data_url”的图片URL路径替换为需要检测的图片URL路径。示例代码如下:
      1
      2
      3
      4
      demo_data_url = 'https://ais-sample-data.obs.cn-north-1.myhuaweicloud.com/terrorism.jpg'
          # call interface use the url (token, image, url, threshold=0.95, scene=None)
          result = moderation_image(token, "", demo_data_url, ['porn'], '')
          print(result)
      
  3. 运行“moderation_image_token_demo.py”,审核结果输出到控制台表示执行成功。
    1
    2
    3
    4
    {"result":{"detail":{"porn":[{"confidence":1.0,"label":"normal"},{"confidence":0.0,"label":"porn"},{"confidence":0.0,"label":"sexy"}]},"suggestion":"pass","category_suggestions":{"porn":"pass"}}}
    {"result":{"detail":{"porn":[{"confidence":1.0,"label":"normal"},{"confidence":0.0,"label":"porn"},{"confidence":0.0,"label":"sexy"}]},"suggestion":"pass","category_suggestions":{"porn":"pass"}}}
    
    Process finished with exit code 0
    
分享:

    相关文档

    相关产品

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

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

*必选

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

字符长度不能超过200

提交反馈 取消

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

跳转到云社区