OpenCV+face++實現(xiàn)實時人臉識別解鎖功能
本文實例為大家分享了OpenCV+face++實現(xiàn)實時人臉識別解鎖功能的具體代碼,供大家參考,具體內(nèi)容如下
1.背景
最近做一個小東西,需要登錄功能,一開始做的就是普通的密碼登錄功能,但是之前看到過python可以做人臉識別,所以我就開了下腦洞,能不能實現(xiàn)一個自己的刷臉解鎖功能。
2.知識儲備
- python基礎(chǔ)語法
- opencv
- face++文檔
- requests庫
3.基本思路
準備一張你想要被識別出的人臉照片,后面刷臉就是按照這張照片來識別,如果和照片中是同一個人就解鎖,刷臉就是打開攝像頭獲取電腦面前人的人臉,然后與之前那張照片比對。
4.代碼講解
看下識別的效果:
第一個函數(shù)就是打開攝像頭并保存圖片:
#從攝像頭讀取圖片并保存 def getpicture(): cap = cv2.VideoCapture(0)#打開攝像頭 cascade = cv2.CascadeClassifier("E:\OpenCV\sources\data\haarcascades\haarcascade_frontalface_default.xml")#這里是是自己的人臉識別xml路徑 while True: # get a frame ret, frame = cap.read()#捕獲圖片 # show a frame gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)#轉(zhuǎn)為灰度圖 rect = cascade.detectMultiScale(gray, scaleFactor=1.15, minNeighbors=5, minSize=(5, 5),flags=cv2.cv.CV_HAAR_SCALE_IMAGE) # 使用模板匹配圖形 for x, y, z, w in rect: cv2.rectangle(frame, (x, y), (x + z, y + w), (0, 0, 255), 2)# 函數(shù)的參數(shù)分別為:圖像,左上角坐標,右下角坐標,顏色,寬度 cv2.imshow("capture", frame) if cv2.waitKey(1) & 0xFF == ord('q'):#按下q拍照 cv2.imwrite("images\client.jpg", frame)#相對路徑,儲存圖片 break cap.release() cv2.destroyAllWindows()
第二個函數(shù)是將樣本圖片與攝像頭讀取的圖片上傳到face++進行處理,并拿到它的face_token,該函數(shù)主要用到的就是requests庫與face++的api。
def upload_img(fileDir, oneface=True): url = '%s/detect?api_key=%s&api_secret=%s' % ( BASE_URL, API_KEY, API_SECRET) #注意參數(shù)名與api文檔一致 files = {'image_file': (os.path.basename(fileDir), open(fileDir, 'rb'), mimetypes.guess_type(fileDir)[0]), } r = requests.post(url, files=files) faces = r.json().get('faces') #print faces if faces is None: print('There is no face found in %s' % fileDir) else: return faces[0]['face_token']#返回face_token
第三個函數(shù)是比較兩張圖片的face_token:
def compare(face_token1,face_token2): url = '%s/compare' % BASE_URL params = BASE_PARAMS params['face_token1'] = face_token1 params['face_token2'] = face_token2 r = requests.post(url, params) #print r.status_code #print r.json() return r.json().get('confidence')#返回兩張照片的相似度
最后判斷一下compare()函數(shù)的返回值就知道兩張圖片是不是同一個人了,再程序中加一個判斷語句就可以實現(xiàn)基本的解鎖功能了。
完整代碼:
#! usr/bin/env python #-*- coding:utf-8 -*- import requests import os import mimetypes #判斷文件類型 import cv2 import time import win32api import win32con BASE_URL = "https://api-cn.faceplusplus.com/facepp/v3" API_KEY = "g_vhMthXCQEzF0gZG5-o0ICNDhr3-80b" API_SECRET = "2HD5ysubTeZTwo20JJTudY0cvZN1BPLt" BASE_PARAMS = { 'api_key':'g_vhMthXCQEzF0gZG5-o0ICNDhr3-80b', 'api_secret':'2HD5ysubTeZTwo20JJTudY0cvZN1BPLt' } def upload_img(fileDir, oneface=True): url = '%s/detect?api_key=%s&api_secret=%s' % ( BASE_URL, API_KEY, API_SECRET) #注意參數(shù)名與api文檔一致 files = {'image_file': (os.path.basename(fileDir), open(fileDir, 'rb'), mimetypes.guess_type(fileDir)[0]), } r = requests.post(url, files=files) faces = r.json().get('faces') #print faces if faces is None: print('There is no face found in %s' % fileDir) else: return faces[0]['face_token'] def compare(face_token1,face_token2): url = '%s/compare' % BASE_URL params = BASE_PARAMS params['face_token1'] = face_token1 params['face_token2'] = face_token2 r = requests.post(url, params) #print r.status_code #print r.json() return r.json().get('confidence') def getpicture(): cap = cv2.VideoCapture(0) cascade = cv2.CascadeClassifier("E:\OpenCV\sources\data\haarcascades\haarcascade_frontalface_default.xml")#這里是是自己的人臉識別xml路徑 while True: # get a frame ret, frame = cap.read() # show a frame gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rect = cascade.detectMultiScale(gray, scaleFactor=1.15, minNeighbors=5, minSize=(5, 5),flags=cv2.cv.CV_HAAR_SCALE_IMAGE) for x, y, z, w in rect: cv2.rectangle(frame, (x, y), (x + z, y + w), (0, 0, 255), 2) cv2.imshow("capture", frame) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.imwrite("images\client.jpg", frame)#相對路徑 break cap.release() cv2.destroyAllWindows() getpicture() print u" 數(shù)據(jù)讀取中。。。。\n" face1 = upload_img(u"images\demo4.jpg") print u" 正在校對人臉。。。。。\n" time.sleep(5)#防止出現(xiàn)qps print u" 再等一下。。。。。\n" face2 = upload_img(u"images\client.jpg") confidence = compare(face1,face2) if confidence>=70: #print u"同一個人" #win32api.ShellExecute(0,'op','genealogy.exe','','',1) win32api.MessageBox(0, u"刷臉成功", u"家譜管理系統(tǒng)", win32con.MB_OK) #這里寫你想要繼續(xù)執(zhí)行的代碼 else: win32api.MessageBox(0, u"刷臉失敗", u"家譜管理系統(tǒng)", win32con.MB_OK) #print u"不是同一個人"
運行效果:
因為樣本圖片是我,所以用白巖松是不能成功的。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MxNet預(yù)訓練模型到Pytorch模型的轉(zhuǎn)換方式
這篇文章主要介紹了MxNet預(yù)訓練模型到Pytorch模型的轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05python通過微信發(fā)送郵件實現(xiàn)電腦關(guān)機
這篇文章主要為大家詳細介紹了python通過微信發(fā)送郵件實現(xiàn)電腦關(guān)機,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Python使用collections模塊實現(xiàn)擴展數(shù)據(jù)類
Python?標準庫提供了一個?collections?模塊,里面提供了很多的數(shù)據(jù)類,在工作中使用這些類能夠簡化我們的開發(fā),本文就來看看collections是如何實現(xiàn)擴展數(shù)據(jù)類的吧2023-06-06