称之为单机版,主要是相对于调用摄像头实时识别而言。本篇主要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. Django:(博客系统)使用使用mysql数据&创建post/category/tag实体,并同步到数据中

    背景: 之前也读过一些关于django的一些书,看过别人写的一些博客系统.但是总有一种看别人的都会,但自己写不出来的感觉,于是为了加深对django的学习就开始动手学习了. 环境搭建: 环境:使用py ...

  2. JavaScript sort() 方法

    定义和用法 sort() 方法用于对数组的元素进行排序. 语法 arrayObject.sort(sortby) 参数 描述 sortby 可选.规定排序顺序.必须是函数. 返回值 对数组的引用.请注 ...

  3. 最新的Windows环境搭建zeroMQ并使用java代码运行zeromq详细教程

    最近项目要用zeromq,linux上很好配置使用,但是windows上配置与使用没有找到合适的解决方案,看的很头疼,这里自己总结下供大家参考 准备工作: 1.libzmq下载地址:https://g ...

  4. ubuntu安装 tar.gz格式程序

    tar.gz(bz或bz2等) 一.安装1.打开一个SHELL,即终端2.用cd 命令进入源代码压缩包所在的目录3.根据压缩包类型解压缩文件(*代表压缩包名称)tar -zxvf ****.tar.g ...

  5. [LeetCode] Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  6. [LeetCode] Find K Closest Elements 寻找K个最近元素

    Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...

  7. [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  8. [LeetCode] Increasing Subsequences 递增子序列

    Given an integer array, your task is to find all the different possible increasing subsequences of t ...

  9. TensorFlow学习笔记(MNIST报错修正 适用Tensorflow1.3)

    在Tensorflow实战Google框架下的深度学习这本书的MNIST的图像识别例子中,每次都要报错   错误如下: Only call `sparse_softmax_cross_entropy_ ...

  10. [SDOI 2011]染色

    Description 题库链接 给定一棵有 \(n\) 个节点的无根树和 \(m\) 个操作,操作有 \(2\) 类: 将节点 \(a\) 到节点 \(b\) 路径上所有点都染成颜色 \(c\) : ...