一、庫介紹
opencv,face_recognition,numpy,以及dlib
注意:
安裝opencv速度可能過慢,需要更換國內鏡像源,參考:https://www.jb51.net/article/208359.htm
附帶Python3.7,64位版本 dlib whl下載路徑:dlib-19_jb51.rar
二、庫安裝
pip install opencv-python
pip install face_recognition
pip install numpy
dlib庫需進入whl文件路徑下安裝
pip install dlib-19.17.99-cp37-cp37m-win_amd64.whl
三、face_recognition庫簡單介紹
face_recognition的load_image_file方法會加載圖片,并返回一個ndarray類型的數據
face_path = "C://Users//25103//Desktop//Python人臉識別//face//徐先生.jpg"
image = face_recognition.load_image_file(face_path)
face_recognition的face_encoding方法,可從返回的ndarray類型數據中提取人臉特征,可同時提取多個特征,返回值為列表類型
face_encoding = face_recognition.face_encodings(image)[0]
face_recognition的face_location方法可以獲取圖片中所有人臉的位置,其返回值為一個列表
face_locations = face_recognition.face_locations(rgb_frame)
四、代碼實現以及注釋講解
# coding = utf-8
import dlib
import cv2
import face_recognition
import os
# 創建視頻對象
video_capture = cv2.VideoCapture(0)
# 加載需要識別的人臉圖片(這張圖片需要僅有一張臉)
# face_recognition的load_image_file方法會加載圖片,并返回一個ndarray類型的數據
# ndarray類型就是NumPy的數組類型,其中的元素類型可以一致也可以不一致
face_path = "C://Users//25103//Desktop//Python人臉識別//face//徐先生.jpg"
image = face_recognition.load_image_file(face_path)
# face_recognition的face_encoding方法,可從返回的ndarray類型數據中提取人臉特征,可同時提取多個特征,返回值為列表類型
# 因為照片中只有一個人臉,所以我們取列表的第一個值
face_encoding = face_recognition.face_encodings(image)[0]
while True:
# 從視頻對象中讀取一幀照片
ret,frame = video_capture.read()
# 將照片縮小,加快處理速度,這里將其縮小為原圖的1/4
# frame = cv2.rectangle(frame,(0,0),fx=0.25,fy=0.25)
# 因為cv2用的是BGR色彩,我們組要將其轉化為RGB進行處理
rgb_frame = frame[:,:,::-1] # 列表轉置操作
# face_recognition的face_location方法可以獲取圖片中所有人臉的位置,其返回值為一個列表
face_locations = face_recognition.face_locations(rgb_frame)
print("共從視頻中找到了{}張人臉".format(len(face_locations)))
# 獲取視頻中所有人臉的特征
face_encodings = face_recognition.face_encodings(rgb_frame,face_locations)
for face in face_encodings:
# 比較兩個特征值——encoding1與encoding2,匹配返回True,否則返回False。tolerance越低,顧名思義,容錯率越低,返回值為列表類型
match = face_recognition.compare_faces([face_encoding],face,tolerance=0.4)
name = "不認識的人"
if match[0]:
# face為圖片名稱
name = os.path.basename(face_path[0:-4])
print("找到了{}".format(name))
到此這篇關于Python三十行代碼實現簡單人臉識別的示例代碼的文章就介紹到這了,更多相關Python 簡單人臉識別內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python opencv人臉識別考勤系統的完整源碼
- 10分鐘學會使用python實現人臉識別(附源碼)
- 用Python實現簡單的人臉識別功能步驟詳解
- python基于opencv實現人臉識別
- python實現圖片,視頻人臉識別(dlib版)
- python實現圖片,視頻人臉識別(opencv版)
- python調用百度API實現人臉識別
- 使用python-cv2實現Harr+Adaboost人臉識別的示例
- python3.8動態人臉識別的實現示例
- Python3 利用face_recognition實現人臉識別的方法
- python實現的人臉識別打卡系統