亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python中提取人臉特征的三種方法詳解

 更新時間:2022年05月11日 14:23:22   作者:AI浩  
這篇文章主要和大家分享三個Python中提取人臉特征的方法,文中的示例代碼講解詳細,對我們學(xué)習Python有一定的幫助,需要的可以參考一下

1.直接使用dlib

安裝dlib方法:

Win10安裝dlib GPU過程詳解

思路:

1、使用dlib.get_frontal_face_detector()方法檢測人臉的位置。

2、使用 dlib.shape_predictor()方法得到人臉的關(guān)鍵點。

3、使用dlib.face_recognition_model_v1()方法提取特征。

新建face_embedding1.py,插入代碼:

import dlib,numpy
import cv2
# 人臉關(guān)鍵點檢測器
predictor_path = "shape_predictor_68_face_landmarks.dat"
# 人臉識別模型、提取特征值
face_rec_model_path = "dlib_face_recognition_resnet_model_v1.dat"

predictor_path是戀人關(guān)鍵點檢測器模型的路徑。

face_rec_model_path是提取人臉特征的路徑。

# 加載模型
detector = dlib.get_frontal_face_detector() #人臉檢測
sp = dlib.shape_predictor(predictor_path) #關(guān)鍵點檢測
facerec = dlib.face_recognition_model_v1(face_rec_model_path)# 編碼

分別初始化人臉檢測、關(guān)鍵點檢測、特征編碼方法。

image_path='train_images/11.jpg'
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 人臉檢測
dets = detector(image, 1)
if len(dets)==1:
    print('檢測到人臉')
shape = sp(image, dets[0])# 關(guān)鍵點
# 提取特征
face_descriptor = facerec.compute_face_descriptor(image, shape)#獲取到128位的編碼
v = numpy.array(face_descriptor)
print(v)

讀取圖片。然后將圖片轉(zhuǎn)為RGB格式。

檢測人臉。

獲取人臉的68個關(guān)鍵點。

獲取128位人臉編碼。

使用感受: 使用dlib.get_frontal_face_detector()檢測人臉效果一般,模糊的人臉檢測不出來。速度上也是比較慢。

2.使用深度學(xué)習方法查找人臉,dlib提取特征

思路:

這種方法使用 cv2自帶的dnn.readNetFromCaffe方法,加載深度學(xué)習模型實現(xiàn)人臉的檢測。然后繼續(xù)使用dlib提取人臉特征。

新建face_embedding2.py,插入代碼:

import dlib,numpy
import cv2

# 人臉關(guān)鍵點檢測器
predictor_path = "shape_predictor_68_face_landmarks.dat"
# 人臉識別模型、提取特征值
face_rec_model_path = "dlib_face_recognition_resnet_model_v1.dat"
prototxt_path = 'deploy.proto.txt'
model_path = 'res10_300x300_ssd_iter_140000_fp16.caffemodel'

導(dǎo)入需要的包。

定義模型的路徑。

net = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
sp = dlib.shape_predictor(predictor_path) #關(guān)鍵點檢測
facerec = dlib.face_recognition_model_v1(face_rec_model_path)# 編碼

初始化人臉檢測模型、關(guān)鍵點檢測模型、人臉特征提取模型。

image_path='train_images/11.jpg'
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,
                             (300, 300), (104.0, 177.0, 123.0))
net.setInput(blob)
detections = net.forward()
startX, startY, endX, endY = 0, 0, 0, 0
for i in range(0, detections.shape[2]):
    # extract the confidence (i.e., probability) associated with the
    # prediction
    confidence = detections[0, 0, i, 2]
    # filter out weak detections by ensuring the `confidence` is
    # greater than the minimum confidence
    if confidence > 0.5:
        # compute the (x, y)-coordinates of the bounding box for the
        # object
        box = detections[0, 0, i, 3:7] * numpy.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")
        break
rect = dlib.rectangle(startX, startY, endX, endY)

這部分的代碼主要是人臉檢測邏輯。

讀取圖片,并將其改為RGB格式。

獲取圖片的大小。

初始化blob。

net.forward()計算人臉的位置。

遍歷檢測結(jié)果

  • 如果置信度大于0.5,則認為是合格的人臉。
  • 計算出人臉的坐標。

將坐標轉(zhuǎn)為dlib.rectangle對象。

shape = sp(image, rect)
print(shape)
# 提取特征
face_descriptor = facerec.compute_face_descriptor(image, shape)#獲取到128位的編碼
v = numpy.array(face_descriptor)
print(v)

計算人臉的關(guān)鍵點。

提取人臉的特征。

使用感受:使用深度學(xué)習模型提取人臉特征,無論速度還是準確率都有很大的提高,即使很模糊的圖像依然能檢測到。

3.使用insightface提取人臉特征

InsightFace 是一個開源的 2D&3D 深度人臉分析工具箱,其中高效地實現(xiàn)了豐富多樣的人臉識別、人臉檢測和人臉對齊算法,并且針對訓(xùn)練和部署進行了優(yōu)化,在多項算法測評、比賽獲得優(yōu)勝。

安裝InsightFace

pip install insightface
pip install onnxruntime-gpu==1.9.0 

注意:onnxruntime安裝1.9以下的版本。

提取特征

新建face_embedding3.py 插入代碼:

import insightface
import cv2

model = insightface.app.FaceAnalysis()
model.prepare(ctx_id=0, det_thresh=0.45)
face_img = cv2.imread('train_images/11.jpg')
res = model.get(face_img)
print('embedding: ', res[0].embedding)  

初始化FaceAnalysis()模型。

設(shè)置置信度位0.45。

讀取圖片

使用模型預(yù)測。

打印人臉特征res[0].embedding。

除了能人臉特征外,還有一些其他的屬性,比如:bbox、kps、landmark_3d_68、landmark_2d_106、age、gender ??梢酝ㄟ^res[0].keys()查看。

使用感受:速度比較慢,精度還行。

到此這篇關(guān)于Python中提取人臉特征的三種方法詳解的文章就介紹到這了,更多相關(guān)Python提取人臉特征內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論