python人脸识别pdf百度云_调用百度云接口实现人脸识别与文字识别
import requests
import base64
import json
def Get_API(): #To obtain API
#注冊百度智能云臉比對接口,獲取API密鑰和秘鑰。
#client_id = 'API Key'
client_id = '******'
#client_secret = 'Secret Key'
client_secret = '******'
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s'%(client_id,client_secret)
#構造一個函數,從服務器請求Access_token的值,并生成人臉比較API。
response=requests.get(host) #從requests.get(url)中構造一個url對象,該對象向服務器請求一個資源
access_token=eval(response.text)['access_token'] #返回請求的str的值,并取出“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("ziqiwen.jpg","huangzz.jpg") #To run the program
總結
以上是生活随笔為你收集整理的python人脸识别pdf百度云_调用百度云接口实现人脸识别与文字识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 宽字符注入_sql注入 宽
- 下一篇: python内置函数教程_Python内