小編給大家分享一下樹莓派上如何利用python+opencv+dlib實現嘴唇檢測,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
樹莓派上利用python+opencv+dlib實現嘴唇檢測
項目的目標是在樹莓派上運行python代碼以實現嘴唇檢測,本來以為樹莓派的硬件是可以流暢運行實時檢測的,但是實驗的效果表明樹莓派實時檢測是不可行,后面還需要改進。
實驗的效果如下:
這里需要用的庫有opencv,numpy,dlib。
1.1 安裝opencv
pip3 install opencv-python
1.2 安裝numpy
樹莓派中自帶了numpy庫
pip3 install numpy
1.3 安裝dlib
在樹莓派的系統里面安裝dlib比較簡單,只需要pip install
就可以了,但是在window系統中會有報錯,這個時候我們就需要安裝pip install dlib-19.17.99-cp37-cp37m-win_amd64.whl
就可以了, 需要注意的是: 不同的python版本要安裝對應版本的dlib,也就是后面的“cp37-cp37m”,查看對應python能安裝的版本號,可以使用命令行:pip debug --verbose
,可以顯示合適的安裝版本號。
在樹莓派上我安裝了cmake和dlib
pip3 install cmake pip3 install dlib
dlib提取人臉特征中包含68個點
顎點= 0–16
右眉點= 17–21
左眉點= 22–26
鼻點= 27–35
右眼點= 36–41
左眼點= 42–47
口角= 48–60
嘴唇分數= 61–67
from gpiozero import LED from time import sleep from subprocess import check_call import cv2 import numpy as np import dlib print(cv2.__version__) def search_cap_num(): for i in range(2000): cap = cv2.VideoCapture(i) cap_opened = cap.isOpened() if cap_opened == True: return i cap_num = search_cap_num() cap = cv2.VideoCapture(cap_num) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 規定上嘴唇和下嘴唇連線的路徑 lip_order_dlib = np.array([[48, 49, 50, 51, 52, 53, 54, 64, 63, 62, 61, 60, 48], [48, 59, 58, 57, 56, 55, 54, 64, 65, 66, 67, 60, 48]]) - 48 lip_order_num = lip_order_dlib.shape[1] while 1: landmarks_lip = [] ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rects = detector(gray, 1) print('faces number:' + str(len(rects))) for (i, rect) in enumerate(rects): # 標記人臉中的68個landmark點 landmarks = predictor(gray, rect) for n in range(48, 68): x = landmarks.part(n).x y = landmarks.part(n).y landmarks_lip.append((x, y)) # cv2.circle(img=img, center=(x, y), radius=3, color=(0, 255, 0), thickness=-1) for m in range(lip_order_num-1): cv2.line(frame, landmarks_lip[lip_order_dlib[0][m]], landmarks_lip[lip_order_dlib[0][m+1]], color=(0, 255, 0), thickness=2, lineType=8) for n in range(lip_order_num-1): cv2.line(frame, landmarks_lip[lip_order_dlib[1][n]], landmarks_lip[lip_order_dlib[1][n+1]], color=(0, 255, 0), thickness=2, lineType=8) cv2.imshow("face", frame) if cv2.waitKey(1) & 0xff == ord('q'): break cap.release() cv2.destroyAllWindows() # check_call(['sudo', 'poweroff'])
效果總體而言比較卡頓,感覺分析一張圖片花費時間在秒量級上。
要是僅僅是顯示攝像頭的圖片還是很快的,沒有任何卡頓,也就是說如果代碼中不存在rects = detector(gray, 1)
這種獲取人臉區域的檢測命令,那么運行速度大大提高,后面需要思考怎么在人臉檢測下提高代碼運行速度。
看完了這篇文章,相信你對“樹莓派上如何利用python+opencv+dlib實現嘴唇檢測”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。