樹莓派上利用python+opencv+dlib實(shí)現(xiàn)嘴唇檢測(cè)的實(shí)現(xiàn)
樹莓派上利用python+opencv+dlib實(shí)現(xiàn)嘴唇檢測(cè)
項(xiàng)目的目標(biāo)是在樹莓派上運(yùn)行python代碼以實(shí)現(xiàn)嘴唇檢測(cè),本來以為樹莓派的硬件是可以流暢運(yùn)行實(shí)時(shí)檢測(cè)的,但是實(shí)驗(yàn)的效果表明樹莓派實(shí)時(shí)檢測(cè)是不可行,后面還需要改進(jìn)。
實(shí)驗(yàn)的效果如下:

1、安裝相關(guān)庫文件
這里需要用的庫有opencv,numpy,dlib。
1.1 安裝opencv
pip3 install opencv-python
1.2 安裝numpy
樹莓派中自帶了numpy庫
pip3 install numpy
1.3 安裝dlib
在樹莓派的系統(tǒng)里面安裝dlib比較簡(jiǎn)單,只需要pip install就可以了,但是在window系統(tǒng)中會(huì)有報(bào)錯(cuò),這個(gè)時(shí)候我們就需要安裝pip install dlib-19.17.99-cp37-cp37m-win_amd64.whl就可以了, 需要注意的是: 不同的python版本要安裝對(duì)應(yīng)版本的dlib,也就是后面的“cp37-cp37m”,查看對(duì)應(yīng)python能安裝的版本號(hào),可以使用命令行:pip debug --verbose,可以顯示合適的安裝版本號(hào)。
在樹莓派上我安裝了cmake和dlib
pip3 install cmake pip3 install dlib
2、代碼部分
dlib提取人臉特征中包含68個(gè)點(diǎn)

顎點(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
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")
# 規(guī)定上嘴唇和下嘴唇連線的路徑
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):
# 標(biāo)記人臉中的68個(gè)landmark點(diǎn)
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'])
3、實(shí)驗(yàn)效果

效果總體而言比較卡頓,感覺分析一張圖片花費(fèi)時(shí)間在秒量級(jí)上。
要是僅僅是顯示攝像頭的圖片還是很快的,沒有任何卡頓,也就是說如果代碼中不存在rects = detector(gray, 1)這種獲取人臉區(qū)域的檢測(cè)命令,那么運(yùn)行速度大大提高,后面需要思考怎么在人臉檢測(cè)下提高代碼運(yùn)行速度。
到此這篇關(guān)于樹莓派上利用python+opencv+dlib實(shí)現(xiàn)嘴唇檢測(cè)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python+opencv+dlib嘴唇檢測(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pycharm2020.1安裝無法啟動(dòng)問題即設(shè)置中文插件的方法
這篇文章主要介紹了Pycharm2020.1安裝無法啟動(dòng)問題即設(shè)置中文插件的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-08-08
在Keras中實(shí)現(xiàn)保存和加載權(quán)重及模型結(jié)構(gòu)
這篇文章主要介紹了在Keras中實(shí)現(xiàn)保存和加載權(quán)重及模型結(jié)構(gòu),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python中IO多路復(fù)用模塊selector的用法詳解
selector?是一個(gè)實(shí)現(xiàn)了IO復(fù)用模型的python包,實(shí)現(xiàn)了IO多路復(fù)用模型的?select、poll?和?epoll?等函數(shù),下面就跟隨小編一起來學(xué)習(xí)一下它的具體使用吧2024-02-02
Python OpenCV 彩色與灰度圖像的轉(zhuǎn)換實(shí)現(xiàn)
為了加快處理速度在圖像處理算法中,往往需要把彩色圖像轉(zhuǎn)換為灰度圖像,本文主要介紹了Python OpenCV 彩色與灰度圖像的轉(zhuǎn)換實(shí)現(xiàn),感興趣的可以了解一下2021-06-06

