屏幕距离识别加语音提醒

发布于:2024-04-04 ⋅ 阅读:(126) ⋅ 点赞:(0)

语音播放

import pygame
pygame.mixer.init()   #初始化
pygame.mixer.music.load(r"7359.wav")   #文件位置
pygame.mixer.music.play(1)   #播放语音
while pygame.mixer.music.get_busy():  # 在音频播放为完成之前不退出程序
    pass

完整代码

import cv2
import cvzone
from cvzone.FaceMeshModule import FaceMeshDetector
import pygame
import threading
from PIL import Image, ImageDraw, ImageFont
import numpy as np
# 初始化pygame.mixer
pygame.mixer.init()
# 加载音频文件
pygame.mixer.music.load('7359.wav')
# 设置摄像头
cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces=1)
# 定义播放音频的函数
def play_audio():
    pygame.mixer.music.play(1)
    while pygame.mixer.music.get_busy():
        continue
# 开始检测人脸
while True:
    success, img = cap.read()
    img, faces = detector.findFaceMesh(img, draw=False)
    if faces:
        face = faces[0]
        pointLeft = face[145]
        pointRight = face[374]
        w, _ = detector.findDistance(pointLeft, pointRight)
        W = 6.3
        f = 600
        d = (W * f) / w
        if d < 34:
            # 检查是否正在播放音频
            if not pygame.mixer.music.get_busy():
                # 使用线程播放音频,避免阻塞主程序
                audio_thread = threading.Thread(target=play_audio)
                audio_thread.start()
        # 将 Depth 文本显示为汉语,颜色为红色
        pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        draw = ImageDraw.Draw(pil_img)
        font = ImageFont.truetype("msyh.ttc", 36)  # 使用微软雅黑字体,大小为36
        draw.text((face[10][0] - 95, face[10][1] - 5), f'距离:{int(d)}厘米', font=font, fill=(255, 0, 0))
        img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
    cv2.imshow("Image", img)
    cv2.waitKey(1)

w