我正在开发一个检测面孔和距离的程序,我想同时播放距离的音频信息

发布于 2025-02-07 00:19:41 字数 1483 浏览 4 评论 0原文

我有一个对象检测代码,其中我计算了从脸到相机的距离?我可以使用什么功能以及如何播放每2-3秒钟的音频?谢谢 以下是代码

import cv2

import cvzone

from cvzone.FaceMeshModule import FaceMeshDetector

import os

class Face:
    window_name = "Detected Objects in webcam"
    video = cv2.VideoCapture(0)

while video.isOpened():
    ret, frame = video.read()
    detector = FaceMeshDetector(maxFaces=3)
    img, faces = detector.findFaceMesh(frame, draw=False)

    if not ret:
        break

    image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cascade_classifier = cv2.CascadeClassifier(
        f"{cv2.data.haarcascades}haarcascade_frontalface_default.xml")
    detected_objects = cascade_classifier.detectMultiScale(
        image, minSize=(20, 20))

    if len(detected_objects) != 0:
        for (x, y, height, width) in detected_objects:
            cv2.rectangle(
                frame, (x, y), ((x + height), (y + width)), (0, 255, 0), 5
            )

    for face in faces:
        r_point = face[374]
        l_point = face[145]
        point_width, _ = detector.findDistance(l_point, r_point)
        width = 6.3

        distance = (width * 600) / point_width
        # text_to_speech(math.floor(distance))
        dis = str(distance)
        cvzone.putTextRect(
            frame, f"Depth:{distance:.2f} cm", (face[10][0]-95, face[10][1]-5), scale=1.8
        )

    cv2.imshow(window_name, frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

cv2.destroyAllWindows()

I have an object detection code, in which i calculate the distance from the face to the camera ? What functions can I use and how to play an audio that says every 2-3 seconds the distance? Thank you
below is the code

import cv2

import cvzone

from cvzone.FaceMeshModule import FaceMeshDetector

import os

class Face:
    window_name = "Detected Objects in webcam"
    video = cv2.VideoCapture(0)

while video.isOpened():
    ret, frame = video.read()
    detector = FaceMeshDetector(maxFaces=3)
    img, faces = detector.findFaceMesh(frame, draw=False)

    if not ret:
        break

    image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cascade_classifier = cv2.CascadeClassifier(
        f"{cv2.data.haarcascades}haarcascade_frontalface_default.xml")
    detected_objects = cascade_classifier.detectMultiScale(
        image, minSize=(20, 20))

    if len(detected_objects) != 0:
        for (x, y, height, width) in detected_objects:
            cv2.rectangle(
                frame, (x, y), ((x + height), (y + width)), (0, 255, 0), 5
            )

    for face in faces:
        r_point = face[374]
        l_point = face[145]
        point_width, _ = detector.findDistance(l_point, r_point)
        width = 6.3

        distance = (width * 600) / point_width
        # text_to_speech(math.floor(distance))
        dis = str(distance)
        cvzone.putTextRect(
            frame, f"Depth:{distance:.2f} cm", (face[10][0]-95, face[10][1]-5), scale=1.8
        )

    cv2.imshow(window_name, frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

cv2.destroyAllWindows()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文