樹莓派+百度雲打造人臉識別門禁系統
阿新 • • 發佈:2018-12-06
先註冊一個百度雲賬號:
然後點選左上角的百度雲進入首頁:
在首頁中選擇產品,人工智慧,人臉識別,點選進入:
選擇立即使用:
在以下頁面中由於沒用應用,因此點選建立應用,然後直接寫上應用名和應用描述就行了,人臉識別的功能都全部預設有了,由於我已經建立好了,因此我點選管理應用檢視我的應用:
請務必記好你的AppID、API Key和Secret Key,在程式中我們將用它們作為唯一的標識,然後點選下載SDK進入以下介面:
我們選擇python Http SDK,下載以後將它匯入到樹莓派中然後詳細檢視它的使用說明。
值得注意的是,安裝的是aip不是api:
將包解壓然後進入到解壓的目錄執行命令就可以了。
建立好應用以後,接下來就需要將人臉上傳了,在這個介面中點選人臉管理:
點選你的人臉庫名稱,新建一個組:
記好你的組ID,在編寫程式碼的時候要用到,然後點進去建立使用者:
這樣一來所有工作都做完了接下來附上我的程式碼:
from aip import AipFace from picamera import PiCamera import urllib.request import RPi.GPIO as GPIO import base64 import time #百度人臉識別API賬號資訊 APP_ID = '15050553' API_KEY = 'rlRrtRL5oRdXGh71jgg1OmyN' SECRET_KEY ='dK5TpuTAZn2nw5eVpspZLmF5Qs1Uu8A1' client = AipFace(APP_ID, API_KEY, SECRET_KEY)#建立一個客戶端用以訪問百度雲 #影象編碼方式 IMAGE_TYPE='BASE64' camera = PiCamera()#定義一個攝像頭物件 #使用者組 GROUP = 'lihuaqiang' #照相函式 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 == '_01lihuaqiang': print("歡迎%s !" % name) time.sleep(3) if name == '_01jishiershi': print("歡迎%s !" % name) time.sleep(3) if name == "_01quhao": 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)
因為沒有買鎖,因此用列印的提示資訊來代替開鎖,如果真的要做一個門禁可以買一個電磁鎖,很方便的。