foreword
hi hi everyone
Let's implement face recognition with Python today?
ask you a question
What is Baidu Aip module?
Cough cough, Baidu AI platform provides a lot of API interfaces for developers to quickly call and use in
projects
In addition to face recognition, the same is true for calling other api functions.
Preparation
local environment
System: win11
Python version: 3.9.7
Editor: VS2022
Install baidu-aip module
win + R enter cmd to open the command prompt
Execute and install Baidu AI module
pip install baidu-aip
Log in to Baidu AI platform to create applications
Open the Baidu AI platform to log in
and find face recognition in the console
create application
Create applications according to your own requirements
Finally get the AppID API Key Secret Key of the application
Make a note of the value and so on will use
AppID:10000000
API Key:xxxxxxxxxxxxxxxxxxxxxxxx
Secret Key:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
code flow
Import baidu-aip module
Original code. Click to receive [Remarks: Su]
Open VS2022 (VSCode PyCharm Sypder is equivalent) to create a py file
input
from aip import AipFace
Declare the AppID API Key Secret Key obtained above
APP_ID = '10000000'
API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxx'
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
Initialize Baidu AIP face recognition module
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
Create a face detection function
def face_detect(image):
result = client.detect(image, image_type='BASE64')
print(result)
return result
The input picture image must be in BASE64 format
Convert pictures to BASE64 format
Import the base64 package
import base64
Open the image as BASE64 format
But importing into Baidu AI needs to be in string format, so it is returned as a string
def imageToBase64(imagePath):
with open(imagePath, 'rb') as f:
image = base64.b64encode(f.read())
return str(image, encoding='utf-8')
Open the image for detection
First prepare a picture pic1.jpg
Call functions
face_detect(imageToBase64("pic1.jpg"))
Prompt that the call is successful:
the problem encountered
Prompt at runtime:
requests.exceptions.ProxyError: HTTPSConnectionPool(host='aip.baidubce.com', port=443)
win + R, enter regedit to open the registry, find
\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
Change the value of ProxyEnable to 0
and run again
Extend to use other functions
In addition to face detection, you can also use functions such as face comparison and face search, and the calling method is the same, such as face comparison.
def face_match(image1, image2):
result = client.match([
{
'image': image1,
'image_type': 'BASE64',
},
{
'image': image2,
'image_type': 'BASE64',
}
])
print(result)
return result
face search
def face_search(image,group_id_list):
result = client.search(image, image_type='BASE64',group_id_list=group_id_list)
print(result)
return result
APP_ID API_KEY SECRET_KEY needs to be modified to your own
at last
Today's sharing ends here
If you have any questions about the article, or other questions about python, you can leave a message in the comment area or private message me. If you think the
article I shared is good, you can follow me or give the article a thumbs up (/≧▽≦)/