日韩性视频-久久久蜜桃-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识别面部特征的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 亚洲自偷自偷偷色无码中文 | 免费看a网站 | 国产精品人| 在线看h网站 | www欧美日韩 | 毛片网页| av在线免播放器 | 色欧美在线 | 日本美女在线 | www狠狠爱 | 91操操操| 国内偷拍一区 | 久久精品www人人爽人人 | 伊人网站在线观看 | 日韩在线观看视频免费 | 国产欧美第一页 | 日日操狠狠干 | 欧美搞逼视频 | 在线免费观看国产 | 天天操天| 日本成人精品在线 | 福利小视频在线播放 | 在线免费黄色网 | 黄色小视频网 | 中国女人av | 欧美精品91 | 好色av | 日韩精品影视 | 日本在线一级片 | 福利片av| 最新天堂在线视频 | 精品国产乱码久久久久久蜜臀网站 | 黄色av片三级三级三级免费看 | 色婷婷综合久久久中文字幕 | 亚洲综合在线五月 | av噜噜色 | 99干99| 欧美久草| 久久免费公开视频 | 久久接色 | 精品少妇人妻一区二区黑料社区 | 韩国黄色av | 精品国产av一区二区 | 一级在线播放 | 男人天堂你懂的 | 日产精品一区二区 | 正在播放老肥熟妇露脸 | 三级视频小说 | 午夜鲁鲁 | 日韩视频a| 国产精品毛片久久 | 成人看片 | 轻轻色在线观看 | 久久av一区 | 在线亚洲+欧美+日本专区 | 男女做那个的全过程 | 波多野结衣丝袜 | 欧美日韩亚洲一区二区三区 | 啪视频在线观看 | 羞羞软件| 91精品综合久久久久久 | 成年人免费小视频 | 美女福利一区 | 免费观看在线播放 | 久久性精品 | 韩国av电影在线观看 | 国产精品2 | 日韩在线免费 | 拔擦8x成人一区二区三区 | 在线尤物 | 日韩精品一区在线观看 | 饥渴丰满的少妇喷潮 | 在线天堂v| 欧美另类色图 | 欧美大片91 | 黄色永久视频 | 自拍 亚洲 欧美 | 国产尤物视频在线 | 99久久久无码国产精品免费麻豆 | 伊人天堂网 | 亚洲精品久久视频 | 一区二区三区中文字幕在线观看 | 毛片在线播放视频 | 穿扒开跪着折磨屁股视频 | 性做久久久久久久久 | 日本激情在线 | 国产精品伦理一区二区 | 麻豆md0049免费| 日日日夜夜操 | 免费在线观看www | 男男play呻吟动漫网站 | 草草影院在线观看视频 | 白丝一区| 日韩人妻精品一区二区三区视频 | 91视频一区二区 | 欧美国产日韩在线观看成人 | 污污网站在线看 | 中文理论片 | www.av在线播放 |