1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        使用Python+OpenCV+Dlib實(shí)現(xiàn)人臉檢測(cè)與人臉特征關(guān)鍵點(diǎn)識(shí)別

        共 6643字,需瀏覽 14分鐘

         ·

        2020-08-25 10:50

        點(diǎn)擊上方小白學(xué)視覺”,選擇加"星標(biāo)"或“置頂

        重磅干貨,第一時(shí)間送達(dá)

        今天,我們將學(xué)習(xí)如何檢測(cè)圖像中的人臉并提取面部特征,如眼睛、鼻子、嘴巴等。我們可以將這些信息作為一個(gè)預(yù)處理步驟來完成,例如捕捉照片中人物的人臉(手動(dòng)或通過機(jī)器學(xué)習(xí)),創(chuàng)建效果來“增強(qiáng)”我們的圖像(類似于Snapchat等應(yīng)用程序中的效果),對(duì)人臉進(jìn)行情感分析等等。
        今天我們將通過引入DLib和從圖像中提取面部特征來將其提升到一個(gè)新的水平。
        • 相關(guān)閱讀:https://towardsdatascience.com/essential-opencv-functions-to-get-you-started-into-computer-vision-743df932e60
        Dlib是一個(gè)高級(jí)的機(jī)器學(xué)習(xí)庫(kù),它是為解決復(fù)雜的現(xiàn)實(shí)世界問題而創(chuàng)建的。這個(gè)庫(kù)是用C++編程語言創(chuàng)建的,它與C/C++、Python和java一起工作。
        • Dlib:http://dlib.net/
        值得注意的是,本教程可能需要對(duì)OpenCV庫(kù)有一定的了解,例如如何處理圖像、打開相機(jī)、圖像處理和一些小技巧。

        它是如何工作的?

        我們的臉有幾個(gè)可以識(shí)別的特征,比如眼睛、嘴巴、鼻子等等。當(dāng)我們使用DLib算法檢測(cè)這些特征時(shí),我們實(shí)際上得到了每個(gè)特征點(diǎn)的映射。該映射由67個(gè)點(diǎn)(稱為地標(biāo)點(diǎn))組成,可識(shí)別以下特征:
        • 顎點(diǎn)= 0–16
        • 右眉點(diǎn)= 17–21
        • 左眉點(diǎn)= 22–26
        • 鼻點(diǎn)= 27–35
        • 右眼點(diǎn)= 36–41
        • 左眼點(diǎn)= 42–47
        • 口角= 48–60
        • 嘴唇分?jǐn)?shù)= 61–67
        現(xiàn)在讓我們來了解如何提取特征。

        安裝要求

        與往常一樣,本文將用代碼演示示例,并將逐步指導(dǎo)你實(shí)現(xiàn)一個(gè)完整的人臉特征識(shí)別示例。但是在開始之前,你需要啟動(dòng)一個(gè)新的Python項(xiàng)目并安裝3個(gè)不同的庫(kù):
        • opencv python
        • dlib
        如果像我一樣使用pipenv,可以使用以下命令安裝所有這些文件:
        pipenv install opencv-python, dlib
        如果你使用的是Mac和某些版本的Linux,則在安裝dlib時(shí)可能會(huì)遇到一些問題,如果遇到的是編譯錯(cuò)誤,請(qǐng)檢查使用的CMake庫(kù)版本。在Mac中,確保你有可用的CMake,并且使用正確的版本運(yùn)行:
        brew install cmake
        對(duì)于其他操作系統(tǒng),請(qǐng)?jiān)诰€檢查以獲得特定支持。

        步驟1:載入并顯示圖片

        我們將從小處著手并以代碼為基礎(chǔ),直到有一個(gè)可以正常工作的示例為止。
        通常,我喜歡使用繪圖來渲染圖像,但是由于我們?cè)谥蟮奈恼轮袦?zhǔn)備了一些很酷的東西,因此我們將做一些不同的事情,并且將創(chuàng)建一個(gè)窗口來展示我們的工作結(jié)果。
        讓我們一起看看代碼吧!
        import cv2# read the imageimg = cv2.imread("face.jpg")# show the imagecv2.imshow(winname="Face", mat=img)# Wait for a key press to exitcv2.waitKey(delay=0)# Close all windowscv2.destroyAllWindows()
        很簡(jiǎn)單,對(duì)吧?我們只是用imread加載圖像,然后告訴OpenCV在winname中顯示圖像,這將打開窗口并給它一個(gè)標(biāo)題。
        之后,我們需要暫停執(zhí)行,因?yàn)楫?dāng)腳本停止時(shí),窗口會(huì)被破壞,所以我們使用cv2.waitKey來保持窗口,直到按下某個(gè)鍵,然后銷毀窗口并退出腳本。
        如果使用代碼并在代碼目錄中添加了一個(gè)名為face.jpg的圖像,你應(yīng)該得到如下內(nèi)容:
        原始圖像:

        步驟2:人臉識(shí)別

        到目前為止,我們還沒有對(duì)圖像做任何處理,只是把它呈現(xiàn)在一個(gè)窗口中,這是非常無聊的,但是現(xiàn)在我們將開始加入其它的內(nèi)容,我們將從識(shí)別圖像中選擇一張臉開始。
        為此,我們將使用名為get_frontial_face_detector()的Dlib函數(shù),非常直觀,但是有一個(gè)警告提示這個(gè)函數(shù)只適用于灰度圖像,所以我們必須首先使用OpenCV。
        get_frontial_face_detector()會(huì)返回一個(gè)檢測(cè)器,該檢測(cè)器是一個(gè)我們可以用來檢索人臉信息的函數(shù),每個(gè)面都是一個(gè)對(duì)象,其中包含可以找到圖像的位置點(diǎn)。
        但我們最好在代碼上看看:
        import cv2import dlib# Load the detectordetector = dlib.get_frontal_face_detector()# read the imageimg = cv2.imread("face.jpg")# Convert image into grayscalegray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)# Use detector to find landmarksfaces = detector(gray)for face in faces: x1 = face.left() # left point y1 = face.top() # top point x2 = face.right() # right point y2 = face.bottom() # bottom point # Draw a rectangle cv2.rectangle(img=img, pt1=(x1, y1), pt2=(x2, y2), color=(0, 255, 0), thickness=4)# show the imagecv2.imshow(winname="Face", mat=img)# Wait for a key press to exitcv2.waitKey(delay=0)# Close all windowscv2.destroyAllWindows()
        上面的代碼將從圖像中檢索所有面部,并在每個(gè)面部上渲染一個(gè)矩形,從而產(chǎn)生如下圖像:
        到目前為止,我們?cè)跈z測(cè)人臉方面做得很好,但是我們?nèi)匀恍枰恍┕ぷ鱽硖崛∷刑卣鳎ǖ貥?biāo))。接下來讓我們開始吧。

        步驟3:識(shí)別人臉特征

        你喜歡魔術(shù)嗎?到目前為止,DLib的工作方式相當(dāng)神奇,只需幾行代碼我們就可以實(shí)現(xiàn)很多,而現(xiàn)在我們遇到了一個(gè)全新的問題,它還會(huì)繼續(xù)這么簡(jiǎn)單嗎?
        回答是肯定的!原來DLib提供了一個(gè)名為shape_predictor()的函數(shù),它將為我們提供所有的魔法,但是需要一個(gè)預(yù)先訓(xùn)練的模型才能工作。
        有幾種模型可以與shape_predictor一起工作,我正在使用的模型可以在這里下載,也可以嘗試其他模型。
        讓我們看看新代碼現(xiàn)在是什么樣子
        import cv2import dlib# Load the detectordetector = dlib.get_frontal_face_detector()# Load the predictorpredictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")# read the imageimg = cv2.imread("face.jpg")# Convert image into grayscalegray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)# Use detector to find landmarksfaces = detector(gray)for face in faces: x1 = face.left() # left point y1 = face.top() # top point x2 = face.right() # right point y2 = face.bottom() # bottom point # Look for the landmarks landmarks = predictor(image=gray, box=face) x = landmarks.part(27).x y = landmarks.part(27).y # Draw a circle cv2.circle(img=img, center=(x, y), radius=5, color=(0, 255, 0), thickness=-1)# show the imagecv2.imshow(winname="Face", mat=img)# Wait for a key press to exitcv2.waitKey(delay=0)# Close all windowscv2.destroyAllWindows()
        像以前一樣,我們總是在同一個(gè)代碼上構(gòu)建代碼,現(xiàn)在使用我們的預(yù)測(cè)函數(shù)為每個(gè)人臉找到特征。但現(xiàn)在我還在做一些奇怪的事情,比如如下代碼的數(shù)值27是用來干嘛的?
        landmarks = predictor(image=gray, box=face)x = landmarks.part(27).xy = landmarks.part(27).y
        我們的預(yù)測(cè)函數(shù)會(huì)返回一個(gè)包含68個(gè)點(diǎn)的對(duì)象,根據(jù)我們之前看到的圖片,如果你注意到的話,會(huì)發(fā)現(xiàn)點(diǎn)27正好在眼睛之間,所以如果所有的計(jì)算正確,你應(yīng)該看到一個(gè)綠點(diǎn)在眼睛之間,如下圖所示:
        我們已經(jīng)很接近了,現(xiàn)在讓我們渲染所有的點(diǎn),而不是只渲染一個(gè):
        import cv2import numpy as npimport dlib# Load the detectordetector = dlib.get_frontal_face_detector()# Load the predictorpredictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")# read the imageimg = cv2.imread("face.jpg")# Convert image into grayscalegray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)# Use detector to find landmarksfaces = detector(gray)for face in faces: x1 = face.left() # left point y1 = face.top() # top point x2 = face.right() # right point y2 = face.bottom() # bottom point # Create landmark object landmarks = predictor(image=gray, box=face) # Loop through all the points for n in range(0, 68): x = landmarks.part(n).x y = landmarks.part(n).y # Draw a circle cv2.circle(img=img, center=(x, y), radius=3, color=(0, 255, 0), thickness=-1)# show the imagecv2.imshow(winname="Face", mat=img)# Delay between every framcv2.waitKey(delay=0)# Close all windowscv2.destroyAllWindows()
        但是如果你對(duì)所有的點(diǎn)都不感興趣呢?實(shí)際上,你可以調(diào)整你的范圍間隔來獲得上面術(shù)語表中指定的任何特征,就像我在這里做的那樣:
        太棒了,但我們能做點(diǎn)更酷的事嗎?

        步驟4:實(shí)時(shí)檢測(cè)

        是的,你沒看錯(cuò)!這可能就是你想要的效果!下一步是連接我們的網(wǎng)絡(luò)攝像頭,從你的視頻流中進(jìn)行實(shí)時(shí)地關(guān)鍵點(diǎn)識(shí)別。
        你可以通過使用相機(jī)遍歷視頻幀或使用視頻文件來對(duì)面部進(jìn)行實(shí)時(shí)面部關(guān)鍵點(diǎn)檢測(cè)。
        如果要使用自己的攝像機(jī),請(qǐng)參考以下代碼,如果使用的是視頻文件,請(qǐng)確保將數(shù)字0更改為視頻路徑。
        如果要結(jié)束窗口,請(qǐng)按鍵盤上的ESC鍵:
        import cv2import dlib
        # Load the detectordetector = dlib.get_frontal_face_detector()
        # Load the predictorpredictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
        # read the imagecap = cv2.VideoCapture(0)
        while True: _, frame = cap.read() # Convert image into grayscale gray = cv2.cvtColor(src=frame, code=cv2.COLOR_BGR2GRAY)
        # Use detector to find landmarks faces = detector(gray)
        for face in faces: x1 = face.left() # left point y1 = face.top() # top point x2 = face.right() # right point y2 = face.bottom() # bottom point
        # Create landmark object landmarks = predictor(image=gray, box=face)
        # Loop through all the points for n in range(0, 68): x = landmarks.part(n).x y = landmarks.part(n).y
        # Draw a circle cv2.circle(img=frame, center=(x, y), radius=3, color=(0, 255, 0), thickness=-1)
        # show the image cv2.imshow(winname="Face", mat=frame)
        # Exit when escape is pressed if cv2.waitKey(delay=1) == 27: break
        # When everything done, release the video capture and video write objectscap.release()
        # Close all windowscv2.destroyAllWindows()
        最后的結(jié)果是:

        在弱光條件下,盡管上面的圖像中有一些錯(cuò)誤,但其結(jié)果也相當(dāng)準(zhǔn)確,如果照明效果好的話結(jié)果會(huì)更加準(zhǔn)確。

        結(jié)論

        OpenCV和DLib是兩個(gè)功能非常強(qiáng)大的庫(kù),它們簡(jiǎn)化了ML和計(jì)算機(jī)視覺的工作,今天我們只是觸及了最基本的東西,還有很多東西需要從中學(xué)習(xí)。
        非常感謝你的閱讀!
        參考鏈接:https://towardsdatascience.com/detecting-face-features-with-python-30385aee4a8e



        ☆ END ☆
        瀏覽 70
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            精品妇女一区二区三区 | 狠狠色噜噜狠狠狠狠色综合久97 | 亚洲小说欧美激情另类 | 日韩特黄一级 | 日韩看片 | 性生大片免费观看性 | 91精品国产99久久久久久女少 | 日韩人妻无码一区二区三区四区 | 久久伊99综合婷婷久久伊 | 天天操天干 |