Face recognition based on Raspberry Pi (Linux system Baidu Smart Cloud Platform)

Preface
There are many solutions for making face recognition with Raspberry Pi, and there are many related examples on the Internet. Today I am going to talk about calling Baidu Smart Cloud's face recognition API, which is a good solution to a certain extent. !

1. Build a platform on Baidu Smart Cloud and apply for AppID, API Key and Secret Key
①Log in to Baidu Cloud to find the product and service, and then find the face recognition
Insert picture description here
②Click on the face recognition, enter the management console of this function, create an application
Insert picture description here
③write
Insert picture description here
Insert picture description here
Click on your own application name (you decide), there are two application scenarios of company and individual below, please select individual ④ Click the application name you created, as shown in the figure, face detection
Insert picture description here
⑤ New group
Insert picture description here
⑥ New group, name your own , But it must be recorded.
Insert picture description here
⑦Click the group you created to enter
Insert picture description here
⑧Create a user, and add your own face photos in, such as frontal photos and side photos under sufficient light conditions, so that it can be accurately recognized during recognition
Insert picture description here
Insert picture description here
⑨Find HTTP SDK, And download
Insert picture description here
Insert picture description here
the SDK that I use, and provide the link as: https://note.youdao.com/ynoteshare1/index.html?id=4bf2685cba09201ca35718881931cdab&type=note

2. Project deployment
2.1 sdk is transferred to the Raspberry Pi, I remotely control the Raspberry Pi through VNC and transfer files

2.2 Install pip and setuptools

2.2.1 Install setuptools

a) Go to the official website (https://pypi.org/project/setuptools/) or enter the following command to download setuptools

wget https://files.pythonhosted.org/packages/37/1b/b25507861991beeade31473868463dad0e58b1978c209de27384ae541b0b/setuptools-40.6.3.zip

b) Unzip

unzip setuptools-40.6.3.zip

c) Installation

cd setuptools-40.6.3
sudo python setup.py build
sudo python setup.py install

2.2.2 Install pip
1) Download
to the official website (https://pypi.org/project/pip/) to download the pip installation package

2) Unzip

tar zxvf pip-20.2.4.tar.gz

3) Installation

cd ../pip-20.2.4/
sudo python setup.py install

If the official website cannot be opened, please refer to this link: https://www.cnblogs.com/mlh-bky/p/10290764.html to
install python2

2.3 Install the Face Recognition SDK
After installing pip and setuptools on your Raspberry Pi, cd to the SDK directory of the Raspberry Pi and execute the following command:

pip install baidu-aip
python setup.py install

Third, test whether the face recognition is successful.
Above we have configured the basic face recognition environment of the Raspberry Pi. Now we can test whether the system can run normally. It can be directly in the sdk folder aip-python-sdk-2.2.15 Create a test file suv.py, copy the following code into it, remember to modify the parameters, how to modify it has been written in the code

from aip import AipFace
from picamera import PiCamera
import urllib.request
import RPi.GPIO as GPIO
import base64
import time
#百度人脸识别API账号信息
APP_ID = '改为自己的APP_ID '
API_KEY = '改为自己的API_KEY'
SECRET_KEY ='改为自己的SECRET_KEY'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)#创建一个客户端用以访问百度云
#图像编码方式
IMAGE_TYPE='BASE64'
camera = PiCamera()#定义一个摄像头对象
#用户组
GROUP = '改为自己的用户组'
 
#照相函数
def getimage():
    camera.resolution = (1024,768)#摄像界面为1024*768
    camera.start_preview()#开始摄像
    time.sleep(2)
    camera.capture('faceimage.jpg')#拍照并保存
    time.sleep(2)
#对图片的格式进行转换
def transimage():
    f = open('faceimage.jpg','rb')
    img = base64.b64encode(f.read())
    return img
#上传到百度api进行人脸检测
def go_api(image):
    result = client.search(str(image, 'utf-8'), IMAGE_TYPE, GROUP);#在百度云人脸库中寻找有没有匹配的人脸
    if result['error_msg'] == 'SUCCESS':#如果成功了
        name = result['result']['user_list'][0]['user_id']#获取名字
        score = result['result']['user_list'][0]['score']#获取相似度
        if score > 80:#如果相似度大于80
            if name == 'yusheng_02':
 
                print("欢迎%s !" % name)
                time.sleep(3)
            if name == 'xiaoming':
                print("欢迎%s !" % name)
                time.sleep(3)
            if name == "xiaoyu":
                print("欢迎%s !" % name)
        else:
            print("对不起,我不认识你!")
            name = 'Unknow'
            return 0
        curren_time = time.asctime(time.localtime(time.time()))#获取当前时间
 
        #将人员出入的记录保存到Log.txt中
        f = open('Log.txt','a')
        f.write("Person: " + name + "     " + "Time:" + str(curren_time)+'\n')
        f.close()
        return 1
    if result['error_msg'] == 'pic not has face':
        print('检测不到人脸')
        time.sleep(2)
        return 0
    else:
        print(result['error_code']+' ' + result['error_code'])
        return 0
#主函数
if __name__ == '__main__':
    while True:
        print('准备')
        if True:
            getimage()#拍照
            img = transimage()#转换照片格式
            res = go_api(img)#将转换了格式的图片上传到百度云
            if(res == 1):#是人脸库中的人
                print("开门")
            else:
                print("关门")
            print('稍等三秒进入下一个')
            time.sleep(3)

When you save the file to py raspberry pie, you can run directly (Tonny will try on the left, so that the camera can be turned off)
Insert picture description here
to recognize faces after the success output to open the door, if he did not recognize faces will output closed
Insert picture description here
last
After finishing the face recognition work, you can expand many projects on this basis, the most common one is the face recognition access control.

Guess you like

Origin blog.csdn.net/weixin_43271542/article/details/109202483