你是不是也一直怀疑谁和谁像...(应用百度api的人脸对比)

你是不是也一直感觉身边某个人和自己认识的某个人很像
你是不是经常感觉某个明星和自己初恋很像
你是不是…
***

今天试了一下人脸对比,调用的百度api(代码里的小伙伴可以直接拿来用,注意别搞坏了哈哈,想自己注册了方法如下<感谢百度大大>)

注册方法:
1、登录百度AI平台,网址:http://ai.baidu.com/tech/face,(官网)https://login.bce.baidu.com/?account=,(直接注册登录)

登录后需要点击“创建应用”填写命名一下,完成后返回,点击“管理应用”,就可以看到已经申请的[应用名称、AppID、API Key、Secret Key].

2、获取Access Token
1.向授权服务地址:https://aip.baidubce.com/oauth/2.0/token?发送请求 并在此URL后带上以下参数:

· grant_type: 必须参数,固定为client_credentials;

· client_id: 必须参数,应用的API Key;

· client_secret: 必须参数,应用的Secret Key;

例如:你申请的API Key是1111111111111,Secret Key是22222222222222222,进行如下拼接:
api = “https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=1111111111111&client_secret=22222222222222222”

今天学到并注意了eval函数

代码如下,奥利给!

import requests
import base64
import json
import time
import os
# 1,准备好申请的人脸识别api,API Key, Secret Key
api1 = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=gCEDWoKDEuG8vIZeziHDpcNd&client_secret=XDHj9Q1wVbUtBX4gMWQ2aRAZq31BwXxA"



# 2,获取token值,拼接API
def get_token():
    response = requests.get(api1)
    # print(response)
    access_token = eval(response.text)['access_token']
    api2="https://aip.baidubce.com/rest/2.0/face/v3/match"+"?access_token="+access_token
    return api2

# 3,读取图片数据
def read_img(img1,img2):
    with open(img1,'rb') as f:
        pic1=base64.b64encode(f.read())
    with open(img2,'rb') as f:
        pic2=base64.b64encode(f.read())
        pic3 = pic2
    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"}

    ])
    return params

# 4,发起请求拿到对比结果
def analyse_img(file1,file2):
    params=read_img(file1,file2)
    api=get_token()
    content=requests.post(api,params).text
    # print(content)
    score = eval(content)['result']['score']
    print("score:",score)
    if score>80:
        print(str(file1)+"和"+str(file2)+'图片识别相似度度为'+str(score)+',是同一人')
    else:
        print(str(file1)+"和"+str(file2)+'图片识别相似度度为'+str(score)+',不是同一人')

def getPathFile(path):
    Path = []
    for paths in path:
        print(paths)
        try:
            pathDir = os.listdir(paths)
            for allDir in pathDir:
                child = os.path.join('%s/%s' % (paths, allDir))
                #跳过文件夹以及非图片文件,将后缀名改为自己需要的文件类型即可实现自己的过滤
                if os.path.isfile(child) and (".jpg" in str(allDir) or (".png" in str(allDir))):
                    Path.append(child)

        except:
            pass
    print(Path)
    return Path

if __name__ == "__main__":

    # f_list = ['bav_1.jpg','bav_2.jpg','bbav_1.jpg','c_1.jpg','gw_1.jpg','gw_2.jpg',
    #           'lxw_1.jpg','lxw_2.jpg','lxw_3.jpg','sav_1.jpg','song_1.jpg','song_2.jpg','song_3.jpg']
    fflist = ['D:\PANDOWNLOADS\Photo\####','D:\PANDOWNLOADS\Photo\unn',
              'D:\PANDOWNLOADS\Photo\####','D:\PANDOWNLOADS\Photo\ppp']
    f_list = getPathFile(fflist)

    for a  in range(len(f_list)-1):
        p1 = f_list[a]
        print("第"+str(a)+"组 ##################################")
        for b in range(a+1,len(f_list)):
            p2 = f_list[b]
            try:
                analyse_img(p1,p2)
                time.sleep(1)
            except:
                print(str(p1)+"和"+str(p2)+"对比失败")
                pass

        print("##################################")
        print(" ")
        time.sleep(3)  # 注意休息哦!


参考文章:
https://blog.csdn.net/swan_tang/article/details/88769612
在此致以真诚感谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值