博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用阿里云人脸检测定位API
阅读量:6874 次
发布时间:2019-06-26

本文共 2921 字,大约阅读时间需要 9 分钟。

hot3.png

开通人脸识别服务,获取 AccessId 和 AccessSecret

在网上找一张照片,比如这张 ,记下图片url

新建文件夹 demo,进入文件夹,使用 wget 下载照片到文件夹内,并重命名为容易记忆的名字,比如 a.jpg,并从 下载 haarcascade_frontalface_default.xml

新建脚本 demo.py (这里使用的是 Python 3.6),添加

import requestsimport hmacimport hashlibimport datetimeimport json import base64from urllib.parse import urlparseimport cv2

然后是各种参数

image_url = ""imagePath = "a.jpg"url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute"ak_id = "
"ak_secret = "
"

定义几个函数

def to_md5_base64(str):    hash = hashlib.md5()    hash.update(str.encode("utf-8"))    return base64.b64encode(hash.digest()).decode("utf-8").strip()def to_sha1_base64(stringToSign, secret):    hmacsha1 = hmac.new(secret.encode("utf-8"), stringToSign.encode("utf-8"), hashlib.sha1)    return base64.b64encode(hmacsha1.digest()).decode("utf-8")def get_current_date():    date = datetime.datetime.strftime(datetime.datetime.utcnow(), "%a, %d %b %Y %H:%M:%S GMT")    return date

发起请求所用的正文和headers

body = {"image_url":image_url,"type":0}options = {    "url": url,    "method": "POST",    "body": json.dumps(body, separators=(",",":")),    "headers": {        "Accept":"application/json",        "Content-Type":"application/json",        "Date": get_current_date(),        "Authorization":""    }}

准备阿里云API校验所需的数字签名

urlPath = urlparse(options['url'])if urlPath.query != '':    urlPath = urlPath.path + "?" + urlPath.queryelse:    urlPath = urlPath.pathbodymd5 = to_md5_base64(options['body'])stringToSign = options['method'] + '\n' \             + options['headers']['Accept'] + '\n' \             + bodymd5 + '\n' \             + options['headers']['Content-Type'] + '\n' \             + options['headers']['Date'] + '\n' \             + urlPathsignature = to_sha1_base64(stringToSign, ak_secret)authHeader = 'Dataplus '+ ak_id + ':' + signature options['headers']['authorization'] = authHeader

最后发起请求调用API

r = requests.post(url, data=options['body'], headers=options['headers'])result = json.loads(r.text)

返回结果格式说明可以看

处理结果,将每张人脸的矩形框、特征点、姿态和瞳孔坐标整合成json格式数据。

faces = []landmark_num = result['landmark_num']for i in range(result['face_num']):    face = {}    face['rect'] = result['face_rect'][i*4:(i+1)*4]    face['prob'] = result['face_prob'][i]    face['pose'] = result['pose'][i*3:(i+1)*3]    face['landmark'] = result['landmark'][i*landmark_num*2:(i+1)*landmark_num*2]    face['iris'] = result['iris'][i*6:(i+1)*6]        faces.append(face)

最后用OpenCV展现照片,同时画出各种特征点以及瞳孔。

image = cv2.imread(imagePath)for face in faces:    [x, y, w, h] = face['rect']    cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2)    lm = iter(face['landmark'])    for x in lm:        cv2.circle(image, (int(x),int(next(lm))), 2, (0,255,0), 2)    iris = iter(face['iris'])    for x in iris:        cv2.circle(image, (int(x),int(next(iris))), int(next(iris)), (0,255,0), 1)cv2.imshow("faces",image)cv2.waitKey(1)

 

转载于:https://my.oschina.net/wuliong/blog/1550613

你可能感兴趣的文章
T-SQL中INSERT、UPDATE
查看>>
Linux下Nginx服务器配置Modsecurity实现Web应用防护系统
查看>>
用 zabbix 监测 snmptrap 的主动告警功能
查看>>
HDU1717 小数化分数2
查看>>
delphi 导入excel
查看>>
Linux下 FTP 常见错误 500 530等错误解决方法
查看>>
oracle asm
查看>>
VC基于单文档opengl框架
查看>>
openSUSE13.2安装ruby和rails
查看>>
python 高级函数
查看>>
F.Cards with Numbers
查看>>
简单入门Buffer
查看>>
Paint House II
查看>>
测试评审清单
查看>>
字节流数据的写出(输出)和读取(输入)
查看>>
OO第四阶段总结
查看>>
javascript总结02
查看>>
创建windows服务
查看>>
KSQL日期字段访问
查看>>
HTML5 入门基础
查看>>