您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關(guān)使用OpenCV和face++怎么實現(xiàn)一個人臉識別解鎖功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
#從攝像頭讀取圖片并保存 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"不是同一個人"
關(guān)于使用OpenCV和face++怎么實現(xiàn)一個人臉識別解鎖功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。