v3python人脸对比成功
金牛zhange 发布于2018-05 浏览:2773 回复:7
2
收藏
最后编辑于2018-06
# encoding:utf-8
from urllib import request
from urllib import parse
import json
import ssl
import sys
def get_token():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=jPnOkfrida8pFOlRHRo9Sqb7&client_secret=7gYFT3OZMHN0TSrsrkisMeVvle6GhGFZ '
    req = request.Request(host)
    req.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = request.urlopen(req)

    content =response.read()
#"""结果转化为字符"""
    content = bytes.decode(content)
#    """转化为字典"""
    content = eval(content[:-1])
    return content['access_token']

#"""转换图片"""
def imgdata(file1path,file2path):
    import base64
    f = open(r'%s'% file1path,'rb')
    pic1 = base64.b64encode(f.read())
    f.close()
    f = open(r'%s'% file2path,'rb')
    pic2 = base64.b64encode(f.read())
    f.close()

    params = json.dumps(
        [
            {"image":str(pic1,'utf-8'),
             "image_type": "BASE64",
             "face_type": "LIVE",
             "quality_control": "LOW"
             },
            {"image":str(pic2,'utf-8'),# 和错误的那个对比,还有一处错误在下面标记星号处
             "image_type": "BASE64",
             "face_type": "LIVE",
             "quality_control": "LOW"
             }
        ],indent=4
    ).encode('utf-8')

    return params



#人脸检测与属性分析
#'''
def img(file1path,file2path):
    token = get_token()

    url = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token="+token


    data = imgdata(file1path,file2path)

    req = request.Request(url,data = data)


    req.add_header('Content-Type', 'application/json')
    response =request.urlopen(req)

    content = response.read()

    content = bytes.decode(content)
    content = eval(content)
#"""获得分数"""
    score = content['result']['score']#*********************************
    if score > 80:
        return '图片相似度:'+str(score)+',同一个人'
    else:
        return '图片相似度:'+str(score)+',不同一个人'

if __name__ == '__main__':
    file1path = 'tupian4.jpg'
    file2path = 'tupian2.jpg'
    res = img(file1path,file2path)
    print(res)
收藏
点赞
2
个赞
共7条回复 最后由金牛zhange回复于2018-06
#8金牛zhange回复于2018-06
#6 comex97回复
# -*- coding: UTF-8 -*-      from aip import AipFace      # 定义常量   APP_ID = '9851066'   API_KEY = 'LUGBatgyRGoerR9FZbV4SQYk'   SECRET_KEY = 'fB2MNz1c2UHLTximFlC4laXPg7CVfyjV'      # 初始化AipFace对象   aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)      # 读取图片   filePath = "WechatIMG1.jpeg"   def get_file_content(filePath):       with open(filePath, 'rb') as fp:           return fp.read()      # 定义参数变量   options = {       'max_face_num': 1,       'face_fields': "age,beauty,expression,faceshape",   }   # 调用人脸属性检测接口   result = aipFace.detect(get_file_content(filePath),options)      print(result)   print(type(result))   楼主请问这段代码运行出错 提示{u'log_id': 1520120155451L, u'timestamp': 1529996449, u'cached': 0, u'result': None, u'error_code': 222001, u'error_msg': u'param[image] is null'} <type 'dict'> 要如何修改呢? 谢谢
展开

.jpg能行

 

0
#7金牛zhange回复于2018-06
#6 comex97回复
# -*- coding: UTF-8 -*-      from aip import AipFace      # 定义常量   APP_ID = '9851066'   API_KEY = 'LUGBatgyRGoerR9FZbV4SQYk'   SECRET_KEY = 'fB2MNz1c2UHLTximFlC4laXPg7CVfyjV'      # 初始化AipFace对象   aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)      # 读取图片   filePath = "WechatIMG1.jpeg"   def get_file_content(filePath):       with open(filePath, 'rb') as fp:           return fp.read()      # 定义参数变量   options = {       'max_face_num': 1,       'face_fields': "age,beauty,expression,faceshape",   }   # 调用人脸属性检测接口   result = aipFace.detect(get_file_content(filePath),options)      print(result)   print(type(result))   楼主请问这段代码运行出错 提示{u'log_id': 1520120155451L, u'timestamp': 1529996449, u'cached': 0, u'result': None, u'error_code': 222001, u'error_msg': u'param[image] is null'} <type 'dict'> 要如何修改呢? 谢谢
展开

改下图片格式

 

0
#6comex97回复于2018-06

# -*- coding: UTF-8 -*-  
  
from aip import AipFace  
  
# 定义常量  
APP_ID = '9851066'  
API_KEY = 'LUGBatgyRGoerR9FZbV4SQYk'  
SECRET_KEY = 'fB2MNz1c2UHLTximFlC4laXPg7CVfyjV'  
  
# 初始化AipFace对象  
aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)  
  
# 读取图片  
filePath = "WechatIMG1.jpeg"  
def get_file_content(filePath):  
    with open(filePath, 'rb') as fp:  
        return fp.read()  
  
# 定义参数变量  
options = {  
    'max_face_num': 1,  
    'face_fields': "age,beauty,expression,faceshape",  
}  
# 调用人脸属性检测接口  
result = aipFace.detect(get_file_content(filePath),options)  
  
print(result)  
print(type(result))  

楼主请问这段代码运行出错

提示{u'log_id': 1520120155451L, u'timestamp': 1529996449, u'cached': 0, u'result': None, u'error_code': 222001, u'error_msg': u'param[image] is null'}

要如何修改呢?

谢谢

0
#5comex97回复于2018-06

# -*- coding: UTF-8 -*-  
  
from aip import AipFace  
  
# 定义常量  
APP_ID = '9851066'  
API_KEY = 'LUGBatgyRGoerR9FZbV4SQYk'  
SECRET_KEY = 'fB2MNz1c2UHLTximFlC4laXPg7CVfyjV'  
  
# 初始化AipFace对象  
aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)  
  
# 读取图片  
filePath = "WechatIMG1.jpeg"  
def get_file_content(filePath):  
    with open(filePath, 'rb') as fp:  
        return fp.read()  
  
# 定义参数变量  
options = {  
    'max_face_num': 1,  
    'face_fields': "age,beauty,expression,faceshape",  
}  
# 调用人脸属性检测接口  
result = aipFace.detect(get_file_content(filePath),options)  
  
print(result)  
print(type(result))  

楼主请问这段代码运行出错

提示{u'log_id': 1520120155451L, u'timestamp': 1529996449, u'cached': 0, u'result': None, u'error_code': 222001, u'error_msg': u'param[image] is null'}

要如何修改呢?

谢谢

0
#4金牛zhange回复于2018-06
#3 xinnixer回复
请问这个用的是python的哪个版本写的?

python3.5

0
#3xinnixer回复于2018-06

请问这个用的是python的哪个版本写的?

0
#2耗子12321乐园回复于2018-05

赞一个

0
TOP
切换版块