日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

表情识别(一)——使用Dlib、opencv和Python识别面部特征

發布時間:2023/12/20 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 表情识别(一)——使用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)
def rect_to_bb(rect):# take a bounding predicted by dlib and convert it# to the format (x, y, w, h) as we would normally do# with OpenCVx = rect.left()y = rect.top()w = rect.right() - xh = rect.bottom() - y# return a tuple of (x, y, w, h)return (x, y, w, h)
補充函數shape__to__np
  • 描述:將包含的68個面部區域的坐標點的shape轉為numpy數組
def shape_to_np(shape, dtype="int"):# initialize the list of (x, y)-coordinatescoords = np.zeros((68, 2), dtype=dtype)# loop over the 68 facial landmarks and convert them# to a 2-tuple of (x, y)-coordinatesfor i in range(0, 68):coords[i] = (shape.part(i).x, shape.part(i).y)# return the list of (x, y)-coordinatesreturn coords
補充函數resive
  • 描述:將圖片按照要求設定大小
  • 參數:image是cv2.imread的對象
  • width和height是新指定的大小參數
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):# initialize the dimensions of the image to be resized and# grab the image sizedim = None(h, w) = image.shape[:2]# if both the width and height are None, then return the# original imageif width is None and height is None:return image# check to see if the width is Noneif width is None:# calculate the ratio of the height and construct the# dimensionsr = height / float(h)dim = (int(w * r), height)# otherwise, the height is Noneelse:# calculate the ratio of the width and construct the# dimensionsr = width / float(w)dim = (width, int(h * r))# resize the imageresized = cv2.resize(image, dim, interpolation=inter)# return the resized imagereturn resized

主要代碼

導入相關的包
# 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函數
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):# initialize the dimensions of the image to be resized and# grab the image sizedim = None(h, w) = image.shape[:2]# if both the width and height are None, then return the# original imageif width is None and height is None:return image# check to see if the width is Noneif width is None:# calculate the ratio of the height and construct the# dimensionsr = height / float(h)dim = (int(w * r), height)# otherwise, the height is Noneelse:# calculate the ratio of the width and construct the# dimensionsr = width / float(w)dim = (width, int(h * r))# resize the imageresized = cv2.resize(image, dim, interpolation=inter)# return the resized imagereturn resized
  • imread的具體的輸出,每一個像素點是以(r,g,b)的形式進行保存的,結果如下


原圖片的shape輸出,對應的是heightweightchannel,總共是rgb三個顏色的通道,像素點是711*474

  • 修改之后的圖片尺寸

  • 轉換之后的灰度圖片,僅僅只有一個單通道

調用加載好的檢測器,對目標進行檢測
  • 第一個參數是需要檢測的圖片
  • 第二個參數是圖片的層數,這里是單層圖片,只有一個灰度層
# 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)

  • 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數組
def shape_to_np(shape, dtype="int"):# initialize the list of (x, y)-coordinatescoords = np.zeros((68, 2), dtype=dtype)# loop over the 68 facial landmarks and convert them# to a 2-tuple of (x, y)-coordinatesfor i in range(0, 68):coords[i] = (shape.part(i).x, shape.part(i).y)# return the list of (x, y)-coordinatesreturn coords
輸出修改之后的圖片
# 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识别面部特征的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产真实伦对白全集 | 夜夜干天天操 | 中文字幕在线免费观看视频 | 天天射美女 | 中文字幕一区二区三区门四区五区 | 色天堂影院 | 张柏芝54张无删码视频 | 鲁一鲁一鲁一鲁一av | 国产精品熟女一区二区不卡 | 美女色诱男人激情视频 | 久久精品视频在线免费观看 | 黄色高潮视频 | 日本免费在线观看 | 六月婷婷色 | 色臀av | 一区二区三区毛片 | 中文字幕中文在线 | 在线视频综合网 | 国内黄色一级片 | 日韩欧美在线观看 | 成人中文字幕在线观看 | 九一精品一区 | 欧美成人免费观看视频 | 国产精品人人妻人人爽 | 欧美一区二区三区网站 | 亚洲夜夜爽 | 国产成人免费在线视频 | 久热这里有精品 | 国产区一区二区三区 | 久久在线免费 | 久久精品中文 | wwwxxxx在线观看 | 久久99精品久久久久子伦 | 婷婷狠狠 | 亚洲成a人片77777kkkk | 亚洲国产婷婷香蕉久久久久久99 | 永久免费视频网站 | 激情黄色小说网站 | 久久精品视频一区二区 | 日韩欧美亚洲视频 | 日本黄色播放器 | 伊人色av | 成人福利视频导航 | 无码国产69精品久久久久同性 | 亚洲视频一区二区三区 | 黄网在线免费 | 欧洲色网| 久久亚洲一区 | 性做久久久久久久免费看 | 一二三区在线播放 | 99精品视频在线播放免费 | 天堂网在线观看视频 | 久久中文字幕av | 久热精品免费视频 | 黄色网入口 | 久久久精品| 国产丰满果冻videossex | 91成品人影院 | 精品国产乱码 | 精品国产黄色片 | 国产精品久久久久毛片大屁完整版 | 欧美国产日韩一区二区 | 中文字幕女同女同女同 | 成年人的免费视频 | 国产老熟妇精品观看 | 丰满少妇在线观看bd | 中文字幕乱码中文乱码b站 国产一区二区三区在线观看视频 | 青青青手机视频 | 国产 xxxx| 国产不卡一| 免费无码国产v片在线观看 三级全黄做爰在线观看 | 永久免费在线看片 | aa视频网站 | 亚洲免费av网 | 成熟女人毛片www免费版在线 | 黑人玩弄人妻一区二区三区免费看 | 色七七久久 | 亚洲视频网站在线观看 | 欧美日韩一区二区三区免费 | 精品视频一二三区 | 日韩国产欧美综合 | 欧美10p| 久久婷婷丁香 | 牛夜精品久久久久久久99黑人 | 奇米影视777在线观看 | 久久久一区二区三区四区 | 伊人论坛| 瑟瑟视频在线免费观看 | 国产探花视频在线观看 | 国产婷婷色一区二区三区在线 | 日韩一卡 | 一本大道av| 国产视频一区在线 | 久久久久久久一区二区三区 | 欧美精品一区二区三区视频 | 欧美精品一区二区视频 | 天天插天天 | av不卡高清| 国产精品香蕉国产 |