tensorflow调用百度云API实现人脸对比简单实例

tensorflow调用百度云API实现人脸对比简单实例

首先还是在百度智能云官网注册/登陆账号,再申请一个人脸识别的API,记好API和密码,也要放到代码里面.
详细申请操作请详看tensorflow调用百度云API语音识别简单实例
接下来上代码,需要对应的库自己去下载吧.

# -*- coding: utf-8 -*-
"""
"""

# encoding:utf-8
import requests
import base64
import json

def Get_API():  #To obtain API

    #Register Baidu intelligent cloud face comparison interface to obtain API Key and Secret Key.
    client_id = '***你的API'    
    client_secret = '你的密码'    #Please replace when using
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s'%(client_id,client_secret)

    #Construct a function,request the value of the Access_token from the server and generate the face comparison API.
    response=requests.get(host)  #Construct a url object from requests.get(url) that requests a resource from the server
    access_token=eval(response.text)['access_token']  #Returns the value of the requested str and takes out the "access_token"
    request_url='https://aip.baidubce.com/rest/2.0/face/v3/match'
    API = request_url + "?access_token=" + access_token      #Stitching API
    #API = "https://aip.baidubce.com/rest/2.0/face/v3/match"+"?access_token="+access_token    

    return API 


def Image_coding(img1,img2):   #Base64 encoding of the images

    f=open(r'%s' % img1,'rb')     
    pic1=base64.b64encode(f.read())     #Image1 Base64 encoding
    f.close()    
    f=open(r'%s' % img2,'rb')     
    pic2=base64.b64encode(f.read())    #Image2 Base64 encoding
    f.close()
    params=json.dumps([
        {"image":str(pic1,"utf-8"),"image_type":'BASE64',"face_type":"LIVE"},
        {"image":str(pic2,"utf-8"),"image_type":'BASE64',"face_type":"IDCARD"}])

    #Add a "liveness_control" option to identify if the person in the picture is a real person
    '''params=json.dumps([
        {"image":str(pic1,"utf-8"),"image_type":'BASE64',"face_type":"LIVE","liveness_control": "HIGH"},
        {"image":str(pic2,"utf-8"),"image_type":'BASE64',"face_type":"IDCARD","liveness_control": "HIGH"}])'''
    
    return params  #Return image parameters


def Image_contrast(img1,img2):   #Analyze images and compare them

    API=Get_API()    #obtain API
    params=Image_coding(img1,img2)   #Base64 encoding of the images
    content=requests.post(API,params).text   #Requests for posts are sent as form forms
    score=eval(content)['result']['score']   #Calculate the score
    if score>=60:     #Set the threshold
        print('二人相似度得分为 %s, 是同一人的可能性极大'%str(score))
    else:
        print('二人相似度得分为 %s, 不是同一人的可能性极大'%str(score))

Image_contrast("1.png","4.jpg")     #To run the program

同路经下有两张图片,为同一人
在这里插入图片描述
运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值