Python中提取人臉特征的三種方法詳解_第1頁
Python中提取人臉特征的三種方法詳解_第2頁
Python中提取人臉特征的三種方法詳解_第3頁
Python中提取人臉特征的三種方法詳解_第4頁
Python中提取人臉特征的三種方法詳解_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第Python中提取人臉特征的三種方法詳解目錄1.直接使用dlib2.使用深度學習方法查找人臉,dlib提取特征3.使用insightface提取人臉特征安裝InsightFace提取特征

1.直接使用dlib

安裝dlib方法:

Win10安裝dlibGPU過程詳解

思路:

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

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

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

新建face_embedding1.py,插入代碼:

importdlib,numpy

importcv2

#人臉關鍵點檢測器

predictor_path="shape_predictor_68_face_landmarks.dat"

#人臉識別模型、提取特征值

face_rec_model_path="dlib_face_recognition_resnet_model_v1.dat"

predictor_path是戀人關鍵點檢測器模型的路徑。

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

#加載模型

detector=dlib.get_frontal_face_detector()#人臉檢測

sp=dlib.shape_predictor(predictor_path)#關鍵點檢測

facerec=dlib.face_recognition_model_v1(face_rec_model_path)#編碼

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

image_path='train_images/11.jpg'

image=cv2.imread(image_path)

image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

#人臉檢測

dets=detector(image,1)

iflen(dets)==1:

print('檢測到人臉')

shape=sp(image,dets[0])#關鍵點

#提取特征

face_descriptor=pute_face_descriptor(image,shape)#獲取到128位的編碼

v=numpy.array(face_descriptor)

print(v)

讀取圖片。然后將圖片轉為RGB格式。

檢測人臉。

獲取人臉的68個關鍵點。

獲取128位人臉編碼。

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

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

思路:

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

新建face_embedding2.py,插入代碼:

importdlib,numpy

importcv2

#人臉關鍵點檢測器

predictor_path="shape_predictor_68_face_landmarks.dat"

#人臉識別模型、提取特征值

face_rec_model_path="dlib_face_recognition_resnet_model_v1.dat"

prototxt_path='to.txt'

model_path='res10_300x300_ssd_iter_140000_fp16.caffemodel'

導入需要的包。

定義模型的路徑。

net=cv2.dnn.readNetFromCaffe(prototxt_path,model_path)

sp=dlib.shape_predictor(predictor_path)#關鍵點檢測

facerec=dlib.face_recognition_model_v1(face_rec_model_path)#編碼

初始化人臉檢測模型、關鍵點檢測模型、人臉特征提取模型。

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

foriinrange(0,detections.shape[2]):

#extracttheconfidence(i.e.,probability)associatedwiththe

#prediction

confidence=detections[0,0,i,2]

#filteroutweakdetectionsbyensuringthe`confidence`is

#greaterthantheminimumconfidence

ifconfidence0.5:

#computethe(x,y)-coordinatesoftheboundingboxforthe

#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()計算人臉的位置。

遍歷檢測結果

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

將坐標轉為dlib.rectangle對象。

shape=sp(image,rect)

print(shape)

#提取特征

face_descriptor=pute_face_descriptor(image,shape)#獲取到128位的編碼

v=numpy.array(face_descriptor)

print(v)

計算人臉的關鍵點。

提取人臉的特征。

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

3.使用insightface提取人臉特征

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

安裝InsightFace

pipinstallinsightface

pipinstallonnxruntime-gpu==1.9.0

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

提取特征

新建face_embedding3.py插入代碼:

importinsightface

importcv2

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)

初始化FaceAnalys

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論