语言:python3
环境:树莓派
外设:USB摄像头
一、拍照并识别年龄性别等数据
import os
import sys
import re
import commands
a=commands.getoutput("fswebcam --no-banner -r 640x480 image3.jpg")
print a
import requests
from json import JSONDecoder
http_url ="https://api-cn.faceplusplus.com/facepp/v3/detect"
#你要调用API的URL
key ="4sGlYXFKnhSMyFq0l6MMcVl9AZSyVQdn"
secret ="P3n3RLv4VP4ggH7o4KAURSYiCZ6ZYOPh"
#face++提供的一对密钥
filepath1 ="/home/pi/image3.jpg"
#图片文件的绝对路径
data = {"api_key":key, "api_secret": secret, "return_attributes": "gender,age,smiling,beauty"}
#必需的参数,注意key、secret、"gender,age,smiling,beauty"均为字符串,与官网要求一致
files = {"image_file": open(filepath1, "rb")
}
'''以二进制读入图像,这个字典中open(filepath1, "rb")返回的是二进制的图像文件,所以"image_file"是二进制文件,符合官网要求'''
response = requests.post(http_url, data=data, files=files)
#POTS上传
req_con = response.content.decode('utf-8')
#response的内容是JSON格式
req_dict = JSONDecoder().decode(req_con)
#对其解码成字典格式
print(req_dict)
#输出
二、拍照并与图片库中人脸对比
库中人像为吴彦祖。
import os
import sys
import re
import commands
a=commands.getoutput("fswebcam --no-banner -r 640x480 image3.jpg")
print a
import requests
from json import JSONDecoder
http_url ="https://api-cn.faceplusplus.com/facepp/v3/search"
#你要调用API的URL
key ="4sGlYXFKnhSMyFq0l6MMcVl9AZSyVQdn"
secret ="P3n3RLv4VP4ggH7o4KAURSYiCZ6ZYOPh"
#face++提供的一对密钥
filepath1 ="/home/pi/image3.jpg"
#图片文件的绝对路径
data = { "api_key":key,
"api_secret": secret,
"outer_id": 'wuyanzu_2'}
#必需的参数.
files= {"image_file": open(filepath1, "rb")
}
'''以二进制读入图像,这个字典中open(filepath1, "rb")返回的是二进制的图像文件,所以"image_file"是二进制文件,符合官网要求'''
response = requests.post(http_url, data=data,files=files)
#POTS上传
req_con = response.content.decode('utf-8')
#response的内容是JSON格式
req_dict = JSONDecoder().decode(req_con)
#对其解码成字典格式
print(req_dict)
#输出