0

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()
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • **(1)** It's not obvious what format your audio is in but you need a decoder/player. Is it MP3 audio files you have for each distance? If you don't have a file then you cannot play audio. **(2)** You can try finding a **text-to-speech** synthesizer for Python to "read" aloud the numbers you provide for distance. – VC.One Jun 12 '22 at 13:22
  • Hi, so i registered like 3 mp3 files, for like 80, 50 and 25 cm and i want to play em when the distance is below that , i already made the 3 mp3 but i dont know how and when to call them. I am new to Python and it s for a project for my college, ty – user19320729 Jun 12 '22 at 14:30
  • I don't use Python, I'm here for the `audio` tag part. Try starting a **Timer** (before the **While** loop) that will run a function every 3 seconds. The function's job is to just check the current `distance` value and then play the relevant mp3 sound by using an **IF** statement to decide which clip to play... – VC.One Jun 15 '22 at 11:23
  • PS: Since you're new to the programming language, I recommend you make a separate test project that is for **Timer** testing only. It could run a function every 3 seconds that increases a number then does **IF** to check the number variable's value, where if X amount then `print` some feedback... Later you can replace `print` with mp3 playback etc when transferring the working code back to your main "Detect Faces" project. – VC.One Jun 15 '22 at 11:38

0 Answers0