树莓派上利用python+opencv+dlib+pygame实现嘴唇检测

书接上回树莓派上利用python+opencv+dlib实现嘴唇检测

我发现卡顿主要原因就是dlib检测过程,但是实力不够还改变不了哈哈,那么我就想着要不然做一个界面让这个卡顿在人们的操作习惯中不那么卡顿,做界面主要使用pygame。

项目的主要目标是加入pygame来定义按钮,让检测操作仅仅发生在点击按钮之后,效果如下:

树莓派上利用python+opencv+dlib+pygame实现嘴唇检测
树莓派上利用python+opencv+dlib+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/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 纯Python开发电脑桌面挂件,冰墩墩雪容融完美组合

    桌面太单调?今天就带大家,一起用Python的PyQt5开发一个有趣的自定义桌面动画挂件,看看实现的动画挂件效果! 下面,我们开始介绍这个自定义桌面动画挂件的制作过程。 一、核心功…

    Python 2023年11月9日
    047
  • 如何使用 Python 实现彩票自由(大乐透)

    前言 没有朋友喜欢买股票,我就不玩了,不想玩了,实在是拉不回来了,只能玩一个比较简单的刮擦乐。虽然我不会买股票。 [En] There are no friends like to…

    Python 2023年5月24日
    041
  • flask操作数据库

    flask操作数据库 准备工作 安装flask-script(版本过高可能会报错) pip install flask-script==2.0.3 使用flask-script里的…

    Python 2023年10月30日
    029
  • windows7安装python框架_windows7下安装python3的scrapy框架

    强大的Anaconda和Spyder。不过如何在这个平台上安装Scrapy呢。 打开MS-DOS(win+R输入cmd回车) 然后输入: conda install -c scra…

    Python 2023年10月6日
    047
  • 〖Python语法进阶篇⑨〗 – 初探正则

    抵扣说明: 1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。 Original: https://blo…

    Python 2023年11月8日
    031
  • Pandas条件筛选 | Python技能树征题

    相关知识 筛选是数据处理中非常频繁使用的功能,而Pandas对表格型数据( Pandas.DataFrame)已经封装了非常完善的条件筛选功能,他们支持下面五种比较运算符和两种逻辑…

    Python 2023年8月21日
    046
  • 【Flask实战】Apache+WSGI在内网Windows环境下部署Flask项目(艰难爬坑总结)

    📖Flask实战的经验总结📖📱有问题随时与我联系,一起学习交流📱❤️喜欢的话点个三连吧❤️ 文章目录 1.前言 2.简单理解Flask框架,web服务器(Apache)和WSGI的…

    Python 2023年8月10日
    061
  • 【Python】秀人集-写真集-爬虫-2.0

    好久不见呀,各位。[/坏笑] 自从上一篇文章发表以来,已经有很长时间了,所以我现在将带着承诺的2.0版本出来。毕竟,评论区已经开始蜂拥而至,不能再拖延了。 [En] It has …

    Python 2023年5月23日
    0114
  • Python—-抽象类的使用方法和使用场景

    推荐文章 很多小伙伴都发现了,用户自主「申请上首页」的按钮取消了,那博主们写的文章还有上首页曝光的机会吗?我们的回答是”当然有!!!”虽然我们取消了上首页申…

    Python 2023年5月24日
    0109
  • python pip install 总是出错的解决方法_pip安装总是失败怎么办? 3个方法帮你解决…

    前言 想必刚接触Python的小伙伴一定被安装库折磨惨啦… … 都说pip install XX要安装啥就安装啥,为啥到我这就不行了呢??? 难道Pytho…

    Python 2023年8月14日
    0186
  • Pandas模块的使用

    Pandas 数据结构 – Series Pandas Series 类似表格中的一个列(column),类似于一维数组,可以保存任何数据类型。 Series 由索引(…

    Python 2023年8月7日
    060
  • pytest测试框架下

    ​​ 活动地址:CSDN21天学习挑战赛 学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。各位小伙伴,如果您: 想系统/深入学习某技术知识点&#82…

    Python 2023年9月14日
    053
  • Python贪吃蛇

    贪吃蛇游戏最初为单机模式,后续又陆续推出团战模式、赏金模式、挑战模式等多种玩法。 1976年,Gremlin平台推出了一款经典街机游戏Blockade。游戏中,两名玩家分别控制一个…

    Python 2023年9月22日
    090
  • 【赵渝强老师】Redis的慢查询日志

    Redis慢查询日志帮助开发和运维人员定位系统存在的慢操作。慢查询日志就是系统在命令执行前后计算每条命令的执行时间,当超过预设阀值,就将这条命令的相关信息(慢查询ID,发生时间戳,…

    Python 2023年6月3日
    075
  • 定制云函数天气推送

    sansui-Weather v2.0 介绍: 在寒冷的冬日给你爱的人暖暖的提示(可增加情话api,每天推送情话及其他定制化的推送)Python脚本实现天气查询应用,提醒她注意保暖…

    Python 2023年11月9日
    040
  • CONDA常用指令及测试pytorch是否安装成功

    conda info –envs #查看现有环境 conda remove -n 环境名 –all # 删除环境conda create -n hsnet …

    Python 2023年9月8日
    061
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球