分别用face++和百度获取人脸属性(python单机版)
称之为单机版,主要是相对于调用摄像头实时识别而言。本篇主要py2下利用face++和百度接口获取本地图片中的人脸属性,并按照一定格式保存数据。
face++版
face++是刚注册的,只能用一个试用的key,并且有QPS限制(这个嘛,哪个免费接口没有限制。不过个人觉得这个限制比百度的严重些),还有就是一帧画面只能识别最大的5张脸。能获取较为丰富的表情数据这点开阔以。。。
# -*- coding: utf-8 -*-
import urllib2
import json
import time
##################################################
#协议url
http_url='https://api-cn.faceplusplus.com/facepp/v3/detect'
#公钥
key = "你自己的key"
#密钥
secret = "你自己的secret"
#返回值,你所需要的属性
return_attributes = 'gender,age,emotion,glass,headpose,beauty'
#图片地址
filepath = r"D:\workspaces\timg8.jpg"
参数协议分隔标示
boundary = '----------%s' % hex(int(time.time() * 1000))
###################################################
#制作协议包
data = []
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_key')
data.append(key)
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_secret')
data.append(secret)
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_attributes')
data.append(return_attributes)
data.append('--%s' % boundary)
fr=open(filepath,'rb')
data.append('Content-Disposition: form-data; name="%s"; filename=" "' % 'image_file')
data.append('Content-Type: %s\r\n' % 'application/octet-stream')
data.append(fr.read())
fr.close()
data.append('--%s--\r\n' % boundary)
###########################################################
#发送POST请求
http_body='\r\n'.join(data)
req=urllib2.Request(http_url)
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
req.add_data(http_body)
##########################################################
try:
#req.add_header('Referer','http://remotserver.com/')
#post data to server
#获得结果
resp = urllib2.urlopen(req, timeout=5)
#打印结果
qrcont=resp.read()
print qrcont
#下面2句返回调用时出现的异常
except urllib2.HTTPError as e:
print e.read()
json_resp = json.loads(qrcont)
num = len(json_resp['faces']) #人脸个数
face_s = json_resp['faces']
face_datas = [[]]*num
for i in range(0,num):
tempface = face_s[i]
data2=tempface['attributes']
gender = data2['gender'].values()[0]
age = data2['age'].values()[0]
glass = data2['glass'].values()[0]
headpose = 1 if data2['headpose']['pitch_angle'] > 0 else 0
emotion = data2['emotion']
emotion =max(emotion, key=emotion.get)
face_datas[i] = [gender,age,glass,headpose,emotion]
print face_datas
#face = {'gender':gender,'age':age,'glass':glass,'headpose':headpose,'emotion':emotion}
baidu版
百度的人脸识别是免费的,调用量没有限制,只有QPS限制为2.
# coding : UTF-8
from aip import AipFace
# 定义常量
APP_ID = '你自己的ID'
API_KEY = '你自己的key'
SECRET_KEY = '你自己的secret'
# 初始化AipFace对象
aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)
# 读取图片
filePath = r"D:\workspaces\tmp_cap.jpg"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 定义参数变量
options = {
'max_face_num': 10,
'face_fields': "age,gender,glasses,expression,faceshape",
}
# 调用人脸属性检测接口
#返回result字典,key为[u'log_id', u'result_num', u'result']
result = aipFace.detect(get_file_content(filePath),options)
face_num = result['result_num'] #人脸数目
参考文献:
- face++官方接口文档
https://console.faceplusplus.com.cn/documents/4888373 - face++官方示例代码
https://console.faceplusplus.com.cn/documents/6329752 - baidu python版sdk接口文档
https://cloud.baidu.com/doc/FACE/Face-Python-SDK.html#.E6.96.B0.E5.BB.BAAipFace
分别用face++和百度获取人脸属性(python单机版)的更多相关文章
- 百度离线人脸识别sdk的使用
1.1下载sdk运行 百度离线人脸识别sdk的使用 1.2配置环境 添加至项目,可以拖动复制或者以类库形式添加face-resource此文件夹 放到根目录上一层 激活文件与所有dll引用放到根目录嫌 ...
- python实例编写(4)--js,滚动条,cookie,验证码,获取特定属性的元素,实现原理
一.调用js 执行方法:execute_script(script,*args) 场景一:在页面上直接执行调用js 场景二:在定位的某个元素上执行调用js 如:掩藏文字(提示插件 tooltip设置淡 ...
- 获取元素属性get_attribute
获取text # coding:utf-8 from appium import webdriver from time import sleep desired_caps = { 'platform ...
- 基于百度AI人脸识别技术的Demo
编写demo之前首先浏览官方API:http://ai.baidu.com/docs#/Face-API/top 下面是源码: package com.examsafety.test; import ...
- Entity Framework 6 Recipes 2nd Edition(12-8)译 -> 重新获取一个属性的原始值
12-8. 重新获取一个属性的原始值 问题 在实体保存到数据库之前,你想重新获取属性的原始值 解决方案 假设你有一个模型 (见 Figure 12-11) 表示一个员工( Employee),包含工资 ...
- JS中isPrototypeOf 和hasOwnProperty 的区别 ------- js使用in和hasOwnProperty获取对象属性的区别
JS中isPrototypeOf 和hasOwnProperty 的区别 1.isPrototypeOf isPrototypeOf是用来判断指定对象object1是否存在于另一个对象object2的 ...
- js动画之获取元素属性
首先我们要介绍一些知识 offsetWidth element.offsetWidth = width + padding + border; width 我们也知道element.style.wid ...
- [WinAPI] API 5 [遍历驱动器并获取驱动器属性]
(1) GetLogicalDrives.获取主机中所有的逻辑驱动器,以BitMap的形式返回.◇返回值GetLogicalDrive函数返回一个DWORD类型的值,第一位表示所对应的驱动器是否存在. ...
- mysql根据身份证信息来获取用户属性信息
需要:根据身份证信息来获取用户属性 方法:可以使用如下sql语句: ) ' then '北京市' ' then '天津市' ' then '河北省' ' then '山西省' ' then '内蒙古自 ...
随机推荐
- AQS源码阅读笔记(一)
AQS源码阅读笔记 先看下这个类张非常重要的一个静态内部类Node.如下: static final class Node { //表示当前节点以共享模式等待锁 static final Node S ...
- 04、NetCore2.0下Web应用之Startup源码解析
04.NetCore2.0Web应用之Startup源码解析 通过分析Asp.Net Core 2.0的Startup部分源码,来理解插件框架的运行机制,以及掌握Startup注册的最优姿势. - ...
- 在服务器上,配置redis可以外网访问
首先linux开放默认端口6379打开redis配置文件redis-conf注释掉 bind 127.0.0.1(默认只有本地主要才能访问)这个注释掉现在处于受保护的状态,外网连不上,因为没有密码 在 ...
- pyqt4 写动画不能播放问题集合
最近在学习动画,真的真的是血泪史,百度基本是0资源,各种在谷歌外国大佬的英文中躺过一个一个血坑....... 这是随便写的一个动画功能调试窗口..... 问题现象: 点击食灵,没反应,写的动画不能生成 ...
- Mac 下安装 MySQL 经历
1.使用 homebrew 安装: brew install mysql 结果报错: $ brew install mysql ==> Downloading http://dev.mysql. ...
- 用DotTrace 来分析.NET-Core程序
1. 前言 看园子里面讲dotTrace 的文章不多,最近也有这方面的需要,于是去搜索了一下,.NET 性能分析方面的工具.目的呢,主要是想发现我的代码中,哪些代码占用了最多时间,来进行优化.主要 ...
- [Codeforces 864A]Fair Game
Description Petya and Vasya decided to play a game. They have n cards (n is an even number). A singl ...
- [HNOI2014]画框
题目描述 小T准备在家里摆放几幅画,为此他买来了N幅画和N个画框.为了体现他的品味,小T希望能合理地搭配画与画框,使得其显得既不过于平庸也不太违和. 对于第 幅画与第 个画框的配对,小T都给出了这个配 ...
- POJ2454 Jersey Politics
Description In the newest census of Jersey Cows and Holstein Cows, Wisconsin cows have earned three ...
- [USACO12FEB]牛的IDCow IDs
题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...