如何在OpenCV中获取检测时间?

u4vypkhs  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(160)

这是一个使用OpenCV的人脸距离测量程序。如果用户距离屏幕小于50 cm,则显示“太近”,如果用户距离超过100cm,则显示“太远”。如果检测时间超过10秒,我需要使其显示结果(太近/太远)。
例如:如果用户离屏幕太近(<50 cm)超过10秒,则结果将显示为太近。
我已经提供了代码,这个程序如下

import cvzone
from cvzone.FaceMeshModule import FaceMeshDetector
from plyer import notification

cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces=1)  # No of faces to be detected.

while True:
    success, img = cap.read()
    img, faces = detector.findFaceMesh(img, draw=False)

    if faces:
        face = faces[0]
        pointLeft = face[145]
        pointRight = face[374]

        # for drawing
        # cv2.line(img, pointLeft, pointRight, (0, 200, 0), 3)
        # cv2.circle(img, pointLeft, 5, (255, 0, 255), cv2.FILLED)
        # cv2.circle(img, pointRight, 5, (255, 0, 255), cv2.FILLED)

        w, _ = detector.findDistance(pointLeft, pointRight)  # width in pixels shown from the camera
        W = 6.3  # distance between eyes average of female and male
        # male average is about 6.4
        # female average is about 6.2

        # finding the focal length
        # d = 50  # distance from the cam
        # f = (w*d)/W   # focal point
        # print(f)

        # finding distance between
        f = 642
        d = (W * f) / w
        print(d)

        # cvzone.putTextRect(img, f'Distance: {int(d)}cm',
        #                    (face[10][0]-100, face[10][1] - 50),
        #                    scale=2)

        if d < 50:
            cvzone.putTextRect(img, f'Too Close',
                               (face[10][0] - 100, face[10][1] - 50),
                               scale=2)
            # notification.notify(
            #     title='Face Distance',
            #     message='Too Close!',
            #     app_icon=None,
            #     timeout=2,
            # )

        elif d > 100:
            cvzone.putTextRect(img, f'Too Far',
                               (face[10][0] - 100, face[10][1] - 50),
                               scale=2)

            # notification.notify(
            #     title='Face Distance',
            #     message='Too Far!',
            #     app_icon=None,
            #     timeout=2,
            # )

        else:
            cvzone.putTextRect(img, f'Distance: {int(d)}cm',
                               (face[10][0] - 100, face[10][1] - 50),
                               scale=2)

    cv2.imshow("Image", img)
    cv2.waitKey(1)
ohfgkhjo

ohfgkhjo1#

如果我错了请打断我,但我的印象是,你没有考虑点3D轴,你只考虑了最左点和最右点之间的距离。
如果用户有轻微的头部旋转,这将破坏您的计算,因为它只考虑到这两个点的距离屏幕坐标(两个轴)。
通过考虑所有轴,您可以计算这两个点的中间点,如果中间点的“深度”轴值太小或太大,则发送消息!

相关问题