天天看點

基于OpenCV和Dlib+fr的人臉檢測以及基于Dlib人臉對齊

在媒體大資料這門課中,師哥師姐們向我們介紹了基于各個常用庫的人臉檢測和人臉對齊,并做了一系列的實驗。

一、 基于OpenCV的人臉檢測

1. 安裝OpenCV:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
           

2. 擷取分類器檔案的位置:

檢視python的路徑:

where python
           

在Python路徑下/Lib/sitepackages/cv2/data/haarcascade_frontalface_default.xml' 即為分類器檔案的位置

3. 代碼測試:

import cv2

filename = "test3.jpg"
###建立⼀個級聯分類器,加載⼀個 .xml⽂件
face_cascade =cv2.CascadeClassifier('D:\Anaconda3\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
###加載圖像
img = cv2.imread(filename)
###轉換為灰階圖像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
###使⽤上⾯建立的分類器來檢測⼈臉,faces為識别出臉部的矩形框向量組,⼏張臉就有⼏個矩 形框,向量組⾥就有⼏組向量,儲存⼈臉的坐标和⼤⼩。
faces = face_cascade.detectMultiScale(gray,1.3,5)
### scaleFactor表示⼈臉檢測過程中每次疊代時圖⽚的壓縮率(1.3)
### minNeighbors:每個⼈臉矩形保留近鄰數⽬的最⼩值(5)
###在原圖上繪制矩形
###周遊faces矩形框向量組⾥每⼀組矩形(即每⼀張臉)

for (x, y, w, h) in faces:
    img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

cv2.imshow('Person Detected!', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
           

檢測結果:

基于OpenCV和Dlib+fr的人臉檢測以及基于Dlib人臉對齊

 二、 基于face_recognition的人臉檢測

基于OpenCV有時不夠精确,⼈臉識别還可以使⽤⼀種準确性更⾼的⽅法——Face-Recognition,

Face-Recognition的使⽤依賴Dlib,是以需要先安裝Dlib:

1. Dlib安裝:

通過以下指令安裝運⾏環境:

##需要安裝的環境:python+opencv+dlib+face_recognition(前兩個上⾯已經安裝好)
1. pip install cmake
2. pip install dlib (若報錯,參考該連結進⾏安裝)
https://blog.csdn.net/qq_58691861/article/details/119116148
https://zhuanlan.zhihu.com/p/464846060
3.pip install face_recognition
           

 2. 安裝完成後,運作代碼:

import face_recognition
import cv2
###加載圖像⽂件(.jpg,.png等),傳回的資料是Numpy數組,記錄了圖⽚的所有像素的特征向量
image = face_recognition.load_image_file("test3.jpg")

###定位圖中所有的⼈臉的像素位置,傳回值為清單形式,清單中每⼀⾏是⼀張⼈臉的位置資訊,包括 【top, right, bottom, left】 這是⼀組元組。
face_locations_noCNN=face_recognition.face_locations(image)
face_recognition.face_locations(image,model='cnn')


print("face_location_noCNN:")
print(face_locations_noCNN)

face_num2=len(face_locations_noCNN)
print(face_num2)

###檢視原始圖檔
org = cv2.imread("test3.jpg")
img = cv2.imread("test3.jpg")
cv2.imshow("test3.jpg",img)
cv2.waitKey()

###進行人臉檢測
for i in range(0,face_num2):
    top = face_locations_noCNN[i][0]
    right = face_locations_noCNN[i][1]
    bottom = face_locations_noCNN[i][2]
    left = face_locations_noCNN[i][3]
    start = (left, top)
    end = (right, bottom)
    color = (0,255,255)
    thickness = 2
    cv2.rectangle(org, start, end, color, thickness)
cv2.imshow("no cnn ",org)
cv2.waitKey(0)
cv2.destroyAllWindows()
           

檢測結果:

基于OpenCV和Dlib+fr的人臉檢測以及基于Dlib人臉對齊

 可以看出,相比于OpenCV, 基于face_recognition的人臉檢測的準确度更高

三、 人臉特征點檢測

Dlib有專⻔的函數和模型,能夠實作⼈臉 68 個特征點的定位。 ⽀持更加精确的标定點。标定點分為5 點和 68 點,可以圈出⾯部眼⿐嘴的位置。 1. 下載下傳預訓練模型: shape_predictor_68_face_landmarks.dat:         連結:https://pan.baidu.com/s/1e7oyn-bc3r4USGwo4alZ5w         提取碼:wppp shape_predictor_5_face_landmarks.dat         連結:https://pan.baidu.com/s/1CxUl5NEVOFokcQb4pKry-Q         提取碼:y0f3 這兩個包放在 python 路徑下的 site-packages ⾥ 2. 運作代碼:

import cv2
import dlib
path = "test4.jpg"
#讀取圖⽚
img = cv2.imread(path)
#灰階化處理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#獲得臉部位置檢測器


detector = dlib.get_frontal_face_detector()
dets = detector(gray, 1)

predictor =dlib.shape_predictor("D:\Anaconda3\Lib\site-packages\shape_predictor_68_face_landmarks.dat")

for face in dets:
    shape = predictor(img, face) # 尋找⼈臉的68個标定點
    for pt in shape.parts():
        pt_pos = (pt.x, pt.y)
        #參數:圖⽚,圓⼼, 半徑, 字型顔⾊,字型粗細)
        cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
    cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

           

檢測結果:

基于OpenCV和Dlib+fr的人臉檢測以及基于Dlib人臉對齊

繼續閱讀