分别用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 '内蒙古自 ...
随机推荐
- AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle
https://beta.atcoder.jp/contests/abc075/tasks/abc075_d 题意: 给出坐标平面上n个点的坐标,要求找到一个面积最小的矩形使得这个矩形的边界加上内部的 ...
- Python系列之 - 装饰器
装饰器主要是用来对函数的操作,我们把定义的函数比作一个蛋糕的话,那么装饰器就是盒子,如果要吃蛋糕就先打开盒子.具体到程序中就是在函数外层又套了一层,套的那一层就是一个装饰器.这么说可能有点抽象,那么我 ...
- __dict__
类有一个__dict__字典属性,保存了当前类的每一个成员,举例如下: >>> class A: def __init__(self,value): self.value=value ...
- 最短路径之Dijkstra算法
Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra算法能得出最短路径的最优 ...
- 使Asp.net Core同时支持输出Json/Xml
我们知道Asp.net Core是支持输出为Json格式的.同时也支持输出为xml格式.只要我们正确的配置.并在Request时指定正确的Accept,即可根据不同的Header来输出不同的格式. 前 ...
- Now trying to drop the old temporary tablespace, the session hangs.
1.描述 问题描述:删除临时表空间时,会话Hangs挂起 SQL> drop tablespace TEMP_B including contents and datafiles; 2.故障诊断 ...
- [LeetCode] Reverse String II 翻转字符串之二
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- HashMap 你真的了解吗?
HashMap深入解析及详细介绍 一. hashmap简介 HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特 ...
- “百度杯”CTF比赛 十二月场_blog(kindeditor编辑器遍历,insert注入,文件包含)
题目在i春秋的ctf训练营中能找到 首先先是一个用户登录与注册界面,一般有注册界面的都是要先让你注册一波,然后找惊喜的 那我就顺着他的意思去注册一个号 注册了一个123用户登录进来看到有个文本编辑器, ...
- [SCOI 2010]字符串
Description lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgw ...