树莓派人脸识别源代码


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()#开始摄像

    camera.capture('faceimage.jpg')#拍照并保存

#对图片的格式进行转换

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 == 'GeYuYao':

         print("Welcome %s !" % name)

         time.sleep(1)

        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

   else: 

        print('test falut')

        time.sleep(1)

        return 0


#主函数

if __name__ == '__main__':

         while True:
            print('ready')

            time.sleep(1)

            getimage()  # 拍照

            img = transimage()  # 转换照片格式

            res = go_api(img)  # 将转换了格式的图片上传到百度云

         print('The round is over')
         time.sleep(1)


你可能感兴趣的:(树莓派,文章,人脸识别,百度)