书接上回树莓派上利用python+opencv+dlib实现嘴唇检测
我发现卡顿主要原因就是dlib检测过程,但是实力不够还改变不了哈哈,那么我就想着要不然做一个界面让这个卡顿在人们的操作习惯中不那么卡顿,做界面主要使用pygame。
项目的主要目标是加入pygame来定义按钮,让检测操作仅仅发生在点击按钮之后,效果如下:
; 1、安装pygame
python3 -m pip install -U pygame --user
或者是直接写后面的也可以
pip install -U pygame --user
2、安装python3-sdl2
2.1 说明
这个安装是在树莓派上面的,在windows上面不需要安装这个
2.2 原因
在树莓派上运行,发现pygame中字体文件也就是ttf文件读取不出来,会存在如下错误: ImportError: libSDL_ttf-2.0.so.0: cannot open shared object file: No such file or directory
2.3 解决方法
网络上的主要解决方法有三种,大家都可以尝试一下看看哪一种合适,我的是要安装python3-sdl2
sudo apt-get install libsdl-ttf2.0-0
sudo apt-get install libsdl2-mixer-2.0-0
sudo apt-get install python3-sdl2
3、代码
from time import sleep
from subprocess import check_call
import cv2 as cv
import numpy as np
import dlib
import threading
import pygame
from pygame.locals import *
import sys
class SurfaceButton:
def __init__(self, screen, font, color, button_size, text, x=0, y=0):
self.screen = screen
self.color = color
self.button_size = button_size
self.text = font.render(text, True, (158, 16, 16))
self.surface = pygame.Surface(screen.get_size())
self.surface.set_colorkey((0, 0, 0))
self.surfaceX = x
self.surfaceY = y
def draw(self):
if len(self.button_size) == 4:
pygame.draw.rect(self.surface, self.color, pygame.Rect(self.button_size), width=0)
self.surface.blit(self.text, (
self.button_size[0] - self.text.get_width() // 2, self.button_size[1] - self.text.get_height() // 2))
self.surface.convert_alpha()
self.screen.blit(self.surface, (self.surfaceX, self.surfaceY))
elif len(self.button_size) == 3:
pygame.draw.circle(self.surface, self.color, self.button_size[:2], self.button_size[2], width=0)
self.surface.blit(self.text, (
self.button_size[0] - self.text.get_width() // 2, self.button_size[1] - self.text.get_height() // 2))
self.surface.convert_alpha()
self.screen.blit(self.surface, (self.surfaceX, self.surfaceY))
class MyThread(threading.Thread):
def __init__(self, func, args=()):
super(MyThread, self).__init__()
self.func = func
self.args = args
def run(self):
self.result = self.func(*self.args)
def get_result(self):
try:
return self.result
except Exception as e:
return e
def lip_detector(screen, img):
landmarks_lip = []
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
rects = detector(gray, 1)
print('检测到了 %d 个人脸' % len(rects))
for (i, rect) in enumerate(rects):
landmarks = predictor(gray, rect)
landmarks_lip_tem = []
for n in range(48, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
landmarks_lip_tem.append((x, y))
landmarks_lip.append(landmarks_lip_tem)
draw_lip(screen, img, landmarks_lip)
def draw_lip(screen, img, landmarks_lip):
img_makeup = img.copy()
for i in range(np.array(landmarks_lip).shape[0]):
for m in range(2):
for n in range(lip_order_num - 1):
cv.line(img_makeup, landmarks_lip[i][lip_order_dlib[m][n]], landmarks_lip[i][lip_order_dlib[m][n + 1]],
color=(0, 255, 0),
thickness=2, lineType=8)
screen.blit(cvimage_to_pygame(cv.cvtColor(img_makeup, cv.COLOR_BGR2RGB)), (0, 0))
def search_cap_num():
for i in range(2000):
cap = cv.VideoCapture(i)
cap_opened = cap.isOpened()
if cap_opened == True:
return i
def set_screen_size():
ret, img = cap.read()
return img.shape[1::-1]
def cvimage_to_pygame(image):
"""Convert cvimage into a pygame image"""
return pygame.image.frombuffer(image.tobytes(), image.shape[1::-1], "RGB")
def deal_with_event():
global button_makeup_clicked, button_quit_clicked, \
button_circle_quit, button_circle_makeup
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
if pressed_array[0]:
mouse_pos = pygame.mouse.get_pos()
mouse_x = mouse_pos[0]
mouse_y = mouse_pos[1]
if (not button_makeup_clicked) and \
button_circle_makeup.button_size[0] - button_circle_makeup.button_size[2] mouse_x \
button_circle_makeup.button_size[0] + button_circle_makeup.button_size[2] and \
button_circle_makeup.button_size[1] - button_circle_makeup.button_size[2] mouse_y \
button_circle_makeup.button_size[1] + button_circle_makeup.button_size[2]:
button_makeup_clicked = True
button_quit_clicked = False
elif (not button_quit_clicked) and \
button_circle_quit.button_size[0] - button_circle_quit.button_size[2] mouse_x \
button_circle_quit.button_size[0] + button_circle_quit.button_size[2] and \
button_circle_quit.button_size[1] - button_circle_quit.button_size[2] mouse_y \
button_circle_quit.button_size[1] + button_circle_quit.button_size[2]:
button_quit_clicked = True
button_makeup_clicked = False
cap_num = search_cap_num()
cap = cv.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]
pygame.init()
screen_video = pygame.display.set_mode(set_screen_size(), 0, 32)
screen_width = screen_video.get_size()[0]
screen_height = screen_video.get_size()[1]
white = (255, 255, 255)
button_circle_pos = [(screen_width - screen_width // 6), screen_height - screen_height // 7]
button_circle_raduis = [screen_height // 10]
button_circle_size = tuple(button_circle_pos + button_circle_raduis)
button_makeup_clicked = False
button_quit_clicked = True
font = pygame.font.Font("font.ttf", 24)
button_circle_makeup = SurfaceButton(screen_video, font, white, button_circle_size, "化妆")
button_circle_quit = SurfaceButton(screen_video, font, white, button_circle_size, "退出")
pygame.display.set_caption("Hello, World!")
img = {}
while cap.isOpened():
if button_makeup_clicked:
lip_detector(screen_video, img)
button_circle_quit.draw()
pygame.display.flip()
while True:
deal_with_event()
if button_quit_clicked:
break
elif button_quit_clicked:
ret, img = cap.read()
img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
screen_video.blit(cvimage_to_pygame(img_rgb), (0, 0))
button_circle_makeup.draw()
deal_with_event()
pygame.display.flip()
3.1 代码逻辑说明
其中 MyThread
类不需要管,我一开始想尝试多线程,后来觉得没有必要。
上述代码主要实现的功能是加入两个按钮,一个是化妆,一个是退出。
一开始的时候按钮是化妆,这个时候界面上显示的是利用opencv获取的图像信息,整个过程不会用到dlib中有关人脸检测和嘴唇特征点检测的代码。
当点击了化妆之后,按钮变成了退出,我们获取此时的一张图片,这个图片用来检测,我们只检测一次,绘制出图片上面存在的嘴唇特征,然后就开始等待退出按钮被点击。
当退出按钮点击之后,按钮变成化妆,界面上重新显示的是利用opencv获取的图像信息。
Original: https://blog.csdn.net/weixin_43977647/article/details/121124576
Author: 若水上善666
Title: 树莓派上利用python+opencv+dlib+pygame实现嘴唇检测
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/780062/
转载文章受原作者版权保护。转载请注明原作者出处!