称之为单机版,主要是相对于调用摄像头实时识别而言。本篇主要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++和百度获取人脸属性(python单机版)的更多相关文章

  1. 百度离线人脸识别sdk的使用

    1.1下载sdk运行 百度离线人脸识别sdk的使用 1.2配置环境 添加至项目,可以拖动复制或者以类库形式添加face-resource此文件夹 放到根目录上一层 激活文件与所有dll引用放到根目录嫌 ...

  2. python实例编写(4)--js,滚动条,cookie,验证码,获取特定属性的元素,实现原理

    一.调用js 执行方法:execute_script(script,*args) 场景一:在页面上直接执行调用js 场景二:在定位的某个元素上执行调用js 如:掩藏文字(提示插件 tooltip设置淡 ...

  3. 获取元素属性get_attribute

    获取text # coding:utf-8 from appium import webdriver from time import sleep desired_caps = { 'platform ...

  4. 基于百度AI人脸识别技术的Demo

    编写demo之前首先浏览官方API:http://ai.baidu.com/docs#/Face-API/top 下面是源码: package com.examsafety.test; import ...

  5. Entity Framework 6 Recipes 2nd Edition(12-8)译 -> 重新获取一个属性的原始值

    12-8. 重新获取一个属性的原始值 问题 在实体保存到数据库之前,你想重新获取属性的原始值 解决方案 假设你有一个模型 (见 Figure 12-11) 表示一个员工( Employee),包含工资 ...

  6. JS中isPrototypeOf 和hasOwnProperty 的区别 ------- js使用in和hasOwnProperty获取对象属性的区别

    JS中isPrototypeOf 和hasOwnProperty 的区别 1.isPrototypeOf isPrototypeOf是用来判断指定对象object1是否存在于另一个对象object2的 ...

  7. js动画之获取元素属性

    首先我们要介绍一些知识 offsetWidth element.offsetWidth = width + padding + border; width 我们也知道element.style.wid ...

  8. [WinAPI] API 5 [遍历驱动器并获取驱动器属性]

    (1) GetLogicalDrives.获取主机中所有的逻辑驱动器,以BitMap的形式返回.◇返回值GetLogicalDrive函数返回一个DWORD类型的值,第一位表示所对应的驱动器是否存在. ...

  9. mysql根据身份证信息来获取用户属性信息

    需要:根据身份证信息来获取用户属性 方法:可以使用如下sql语句: ) ' then '北京市' ' then '天津市' ' then '河北省' ' then '山西省' ' then '内蒙古自 ...

随机推荐

  1. ng-model,ng-value,ng-bind,{{}}----angularJS数据绑定

    最典型用法 双向绑定 <input type="text" value="{{apple}}" ng-model="apple" &g ...

  2. js实现继承的5种方式

    js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承有以下通用的几种方式1.使用对象冒充实现继承(该种实现 ...

  3. amd屏幕亮度无法调整,无法调节亮度

    1:CMD+R键打开"运行",输入"regedit"进入注册表 2:搜索"KMD_EnableBrightnessInterface2",找 ...

  4. Apache 安装与配置(WIN10)

    本地坏境:windows 10 Pro 1709 Apache版本:httpd-2.4.32-Win64-VC15 Apache下载地址:https://www.apachelounge.com/do ...

  5. Mac 下安装 MySQL 经历

    1.使用 homebrew 安装: brew install mysql 结果报错: $ brew install mysql ==> Downloading http://dev.mysql. ...

  6. 最快的3x3中值模糊

    10.1国庆后,知名博主:laviewpbt  http://www.cnblogs.com/Imageshop/ 发起了一个优化3x3中值模糊的小活动. 俺也参加其中,今天博主laviewpbt   ...

  7. 计蒜客模拟赛5 D2T2 蚂蚁搬家

    很久很久以前,有很多蚂蚁部落共同生活在一片祥和的村庄里.但在某一天,村庄里突然出现了一只食蚁兽,蚂蚁们为了保全性命而决定搬家. 然而这个村庄四面环山,想要离开这个村庄必须要从地洞里离开,村子里一共有 ...

  8. hdu 5468(莫比乌斯+搜索)

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

  9. [BZOJ]1005 明明的烦恼(HNOI2008)

    BZOJ的第一页果然还是很多裸题啊,小C陆续划水屯些板子. Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间 ...

  10. Visual Studio 2015 无法命中断点

    新安装操作系统后发现,vs2015无法命中断点. 在项目中设置生成调试信息:FULL   即可.