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()