【Python】阿里云图像识别打标脚本demo

对于图像打标问题,阿里云、百度、华为都有自己的API接口。经过测试发现。准确率大概差不多,因为选择了阿里云服务器,所以打标也就用了阿里云的。

阿里云打标接口文档
阿里云API校验规则文档

阿里云自带的Python API是用Python 2.7写的,经过测试,我将其改为Python3版本,分享出来方便大家采用。

#!/usr/bin/python
# -*- coding:utf-8 -*-

import datetime
import hmac
import hashlib
import json
from urllib.parse import urlparse
import base64
import requests

# 获取当前时间的方法
def get_current_date():
    date = datetime.datetime.strftime(datetime.datetime.utcnow(), "%a, %d %b %Y %H:%M:%S GMT")
    return date

# 将字符串进行md5加密后转为base64格式
def to_md5_base64(strBody):
    hash = hashlib.md5()
    hash.update(strBody.encode("utf8"))
    ret = hash.digest()
    return base64.b64encode(ret).decode()

# 将字符串进行sha1加密后转为base64格式
def to_sha1_base64(stringToSign, secret):
    hmacsha1 = hmac.new(secret.encode("utf8"), stringToSign.encode("utf8"), hashlib.sha1)
    return base64.b64encode(hmacsha1.digest()).decode()

# 参数准备
ak_id = '********'          # 你的AccessKey ID
ak_secret = '************'  # 你的Access Key Secret

data = {
    "type": 0,
    "image_url": "https://img.ssyer.com/picture/full/rJeeNoaE3M.jpg"  # 要打标的图片URL链接
}

options = {
    'url': 'https://dtplus-cn-shanghai.data.aliyuncs.com/image/tag',
    'method': 'POST',
    'body': json.dumps(data, separators=(',', ':')),
    'headers': {
        'accept': 'application/json',
        'content-type': 'application/json',
        'date': get_current_date(),
        'authorization': ''
    }
}


# 第一步: 对body进行md5加密然后base64压缩
body = ''
if 'body' in options:
    body = options['body']
# print(body)

bodymd5 = ''
if not body == '':
    bodymd5 = to_md5_base64(body)
# print(bodymd5)

# 第二步: 提取urlPath
urlPath = urlparse(options['url'])
if urlPath.query != '':
    urlPath = urlPath.path + "?" + urlPath.query
else:
    urlPath = urlPath.path

# 第三步: 构造stringToSign & signature
stringToSign = options['method'] + '\n' \
               + options['headers']['accept'] + '\n' \
               + bodymd5 + '\n' \
               + options['headers']['content-type'] + '\n' \
               + options['headers']['date'] + '\n' \
               + urlPath

signature = to_sha1_base64(stringToSign, ak_secret)

# 第四步: 构造authHeader
authHeader = 'Dataplus ' + ak_id + ':' + signature
options['headers']['authorization'] = authHeader
# print(authHeader)

# 第五步:判断请求方式,进行post请求
request = None
method = options['method']
url = options['url']
# print(method)
# print(url)

if 'GET' == method:
    response = requests.get(url)
    print("get---response", response)

elif 'POST' == method:
    response = requests.post(url, data=body, headers=options['headers'])
    print("post---response", response)

    resp = json.loads(response.content.decode("utf-8"))
    # print(resp["tags"])
    print([r["value"] for r in resp["tags"]])


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值