12月05, 2019

百度和阿里的图像识别API识别率对比

经过人工对比预测结果,百度识别准确性较好

百度通用图像识别

安装SDK

pip install baidu-aip -i https://pypi.douban.com/simple

python调用API

appId, apiKey和secretKey需要在百度云的安全认证页面生成;图像内容需要使用BASE64编码。

import matplotlib.pylab as plt
import aip
import matplotlib.image as mpimg
%matplotlib inline 

appId=""
apiKey=""
secretKey=""

def baiduai_rekog(fn):
    with open(fn,'rb') as f:
        data = f.read()

    clf = aip.AipImageClassify(appId=appId,apiKey=apiKey,secretKey=secretKey)
    ret = clf.advancedGeneral(data)
    return ret

baiduai_rekog('./img/067dba9c-6848-47e2-86ac-7389066f7c03.jpg')

阿里云图像和场景识别

ak_id和ak_secret需要在阿里云中生成认证授权;图像内容需要使用BASE64编码。

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()


data0={"type":0,
      "image_url":"https://qimg.xiaohongshu.com/items/f4a967b8c69d196038103865105a27db?imageView2/1/w/320/h/320/q/90.jpg"
     }

def aliyun_reckog(fn,api='tag'):
    with open(fn,'rb') as f:
        ls_f=base64.b64encode(f.read()) #读取文件内容,转换为base64编码 
    data={"type":1,
          "content":ls_f.decode()
         }  
    options = {
        'url': f'https://dtplus-cn-shanghai.data.aliyuncs.com/image/{api}',
        'method': 'POST',
        'body': json.dumps(data, separators=(',', ':')),
        'headers': {
            'accept': 'application/json',
            'content-type': 'application/json',
            'date': get_current_date(),
            'authorization': ''
        }
    }
    ak_id=''
    ak_secret=''

    # 第一步: 对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']

    response=requests.post(url,data=body, headers=options['headers'])
    if response.status_code==200:
        resp = json.loads(response.content.decode("utf-8"))
        if resp['errno']!=0:
            return resp
        else:
            return [r["value"] for r in resp["tags"]]
    else:
        return {'errno': -1, 'err_msg': 'HTTP error'}

fn='./img/d0d76486-145a-3ea7-994f-d285444f6b76.jpg'


aliyun_reckog(fn,'tag')

本文链接:http://57km.cc/post/image reconginition API.html

-- EOF --

Comments