表情识别(一)——使用Dlib、opencv和Python识别面部特征
生活随笔
收集整理的這篇文章主要介紹了
表情识别(一)——使用Dlib、opencv和Python识别面部特征
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 基本步驟
- 1、定位圖片中的臉
- 2、在面部ROI中檢測出關鍵的面部結構
- 什么是ROI
- 補充函數rect_to_bb,將rect轉成坐標點
- 補充函數shape__to__np
- 補充函數resive
- 主要代碼
- 導入相關的包
- 初始化面部檢測器和面部特征預測器
- 打開圖片并讀取,將之轉換為的灰度圖片,固定大小
- 調用加載好的檢測器,對目標進行檢測
- 遍歷所有是別人出來的人臉
- 輸出修改之后的圖片
- 最終的代碼
- 實驗效果
- 分析與總結
基本步驟
1、定位圖片中的臉
- 面部檢測可以使用很多方式實現,比如說OpenCV內嵌的Haar級聯,預訓練好的HOG+ 先行SVM對象檢測,或者使用深度學習算法進行面部識別。無論什么方法,我們最終都是要獲得標定面部的選區
2、在面部ROI中檢測出關鍵的面部結構
- 主要給出對應面部選區,我們就可以進行面部特征點的檢測。有很多不同的面部特征點檢測器,但是所有的方法基本上都是針對以下幾個器官:嘴、左右眼睫毛、左右眼、鼻子、下巴等。
- Dlib庫中常用的面部特征檢測器是One millisecond face alignment with an ensemble of regression trees。這個方法使用了一個人工標注的測試集,標注的內容是圍繞面部特征的特定的坐標,坐標表示的是像素點之間的距離。有了這個訓練集,就可以訓練出來一個集成的回歸樹,用來檢測面部的特征。這個特征檢測器的可以進行實時高精度檢測。
- 如果想要的更加深入的了解這個技術,可以通過連接,讀相關的文章,配合Dlib的官方文檔。
- 文章的連接
- Dlib官方文檔
- 在Dlib庫中的預先訓練好的面部特征檢測是針對人臉結構上的68個特征點,分布如下。
什么是ROI
- 圖像處理中,從被處理圖像以方框、圓、橢圓等不規則多邊形方式勾勒出的需要處理的區域,成為感興趣區域,ROI。
補充函數rect_to_bb,將rect轉成坐標點
- 描述:將檢測器檢測出來的rect轉換成具體的長方形框的坐標點
- 原理:detecor返回的值是rect,數據的形式是(x,y,height,width)
補充函數shape__to__np
- 描述:將包含的68個面部區域的坐標點的shape轉為numpy數組
補充函數resive
- 描述:將圖片按照要求設定大小
- 參數:image是cv2.imread的對象
- width和height是新指定的大小參數
主要代碼
導入相關的包
# import the necessary packages import numpy as np import argparse import dlib import cv2初始化面部檢測器和面部特征預測器
# initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor # 初始化基于HOG預測的預先訓練好的檢測器 detector = dlib.get_frontal_face_detector() # 使用的shape-predictor去加載的下載的面部特征訓練器 # 括號里的是檢測器的路徑 predictor = dlib.shape_predictor("the path of the detector")打開圖片并讀取,將之轉換為的灰度圖片,固定大小
# 使用opencv打開圖片 image = cv2.imread("1.jpg") # 統一圖片的大小 image = imutils.resize(image,width = 500) # 將圖片轉為灰度圖片,將BGR圖片轉為灰度圖片 gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)- 具體resize函數
- imread的具體的輸出,每一個像素點是以(r,g,b)的形式進行保存的,結果如下
原圖片的shape輸出,對應的是heightweightchannel,總共是rgb三個顏色的通道,像素點是711*474
- 修改之后的圖片尺寸
- 轉換之后的灰度圖片,僅僅只有一個單通道
調用加載好的檢測器,對目標進行檢測
- 第一個參數是需要檢測的圖片
- 第二個參數是圖片的層數,這里是單層圖片,只有一個灰度層
- rects的結果是的坐標和weight和height兩對參數
遍歷所有是別人出來的人臉
for (i,rect) in enumerate(rects):# i對應的是目標的索引,rect對應是每一個框的起始點坐標和長寬# 定位人臉的關鍵點,返回的定位之后的68個關鍵點的位置shape = predictor(gray,rect)# shape是輸出之后坐標點,是(68,2),68個點,每個點都是二維的,將所有的坐標點轉為numpy數組shape = face_utils.shape_to_np(shape)# 將rect檢測人臉的邊框轉為繪制矩形框的具體位置(x,y,w,h) = face_utils.rect_to_bb(rect)# 繪制人臉的矩形框cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)# 設置矩形框的文字部分cv2.putText(image,"Face #{}".format(i+1),(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)# 循環遍歷所有的點,將之在原圖上進行標注for (x,y) in shape:cv2.circle(image,(x,y),1,(0,0,255),-1)- 具體的函數face_utils.shape_to_np的具體代碼,將shape的輸出結果轉為numpy數組
輸出修改之后的圖片
# show the output image with the face detections + facial landmarks cv2.imshow("Output",image) cv2.waitKey(0)最終的代碼
# import the necessary packages# import argparse import cv2 import dlibimport imutils # the package below from the writer from imutils import face_utils# intialize dlib face detector adn then create the facial landmark predictor detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")# load the image,resize it and convert it into grayscale # resice the size of the image into 500 width # image = cv2.imread(args["image"]) image = cv2.imread("1.jpg") image = imutils.resize(image,width = 500) gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)# detect face in the grayscale face # detecting the bounding box of faces in our image # the second parameter is the number of image pyramid layer # prior applying the detector we must upscaling the image rects = detector(gray,1)# Given the coordinates of the face in the image # loop over the face detections for (i,rect) in enumerate(rects):# determine the facial landmarks for the face region# convert the coordiantes of the facial landmark to numpy array# predictor is to detect the facila landmarkshape = predictor(gray,rect)# convert the dlib objects to a numpy arrayshape = face_utils.shape_to_np(shape)# convert dlib's rectangle to a OpenCV-style bounding box(x,y,w,h)# then draw the face bounding box(x,y,w,h) = face_utils.rect_to_bb(rect)cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)# show the face numbercv2.putText(image,"Face #{}".format(i+1),(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)# loop over the (x,y)-coordinates for the facial lanmarks# and draw the on the imagefor (x,y) in shape:cv2.circle(image,(x,y),1,(0,0,255),-1)# show the output image with the face detections + facial landmarks cv2.imshow("Output",image) cv2.waitKey(0)實驗效果
分析與總結
- 里面有一個的imutils的包下載地址,不過下不了也沒關系,我已經把對應原函數附在了對應調用的地方,你們可以自己改一下
總結
以上是生活随笔為你收集整理的表情识别(一)——使用Dlib、opencv和Python识别面部特征的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: macos安装vscode_VS Cod
- 下一篇: python云端系统开发入门_Pytho