Realization of simple face recognition
Implementation of simple face recognition comparison based on Python and calling Baidu cloud API interface
Hello! Here is an article about "Implementation of Simple Face Recognition Comparison", which is also a small project in my school programming course. It is implemented in Python and calls the face recognition API of Baidu Smart Cloud. Hope to help you acridine!
Register a Baidu Smart Cloud account and apply for a face recognition API
Simple face recognition, of course, requires the support of "external forces". Here we choose Baidu's father's product, the core code of face recognition and comparison, and hand it over to Baidu for implementation:
- Register for a Baidu Smart Cloud account. Link: Baidu Smart Cloud Login
- After logging in, for this interface, click Product Services .
- Choose product services , artificial intelligence , and face recognition in succession .
- Choose Create App .
5. Fill in the application name , select the application type , the interface selection is the default, fill in the application description , and view the application details after creation . - Get the API Key , Secret Key and face comparison request address required by the application , write them down, and use them in the code.
code interpretation part
get API
client_id = 'API Key'
client_secret = 'Secret Key' #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)
Replace the API Key and Secret Key with what you just got when applying for the API interface.
response=requests.get(host)
access_token=eval(response.text)['access_token']
request_url='https://aip.baidubce.com/rest/2.0/face/v3/match'
API = request_url + "?access_token=" + access_token
Apply for an access_token from the server , and splicing it to request_url , which is the face comparison request address obtained when applying for the API interface .
picture encoding
f=open(r'%s' % img1,'rb')
pic1=base64.b64encode(f.read())
f.close()
f=open(r'%s' % img2,'rb')
pic2=base64.b64encode(f.read())
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"}])
Perform Base64 encoding on the two photos to be compared , see the usage documentation of the face recognition API interface
picture comparison
API=Get_API()
params=Image_coding(img1,img2)
content=requests.post(API,params).text
score=eval(content)['result']['score']
if score>=60:
print('二人相似度得分为 %s, 是同一人的可能性极大'%str(score))
else:
print('二人相似度得分为 %s, 不是同一人的可能性极大'%str(score))
Run the above two functions first, then use the API and image information to request data from the server, get the return value and extract the score , which is the comparison score of the 100-point system. Then set a threshold and output the result.
operation result
Put two photos in the root directory and run the program! (I put a selfie and a photo of Liu Yifei )
The similarity between me and Sissy is 0 , and I am heartbroken (crying, wow————!).
Code full version
Here is the full version of the code link, you can move to my Github account !
Full code link here!
If you think it can help you, please stay for a second and give a like! Like it! Like it! Bar!