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

huangapple go评论163阅读模式
英文:

How to get the detection time in OpenCV?

问题

这是一个使用OpenCV进行面部距离测量的程序。如果用户离屏幕小于50厘米,则显示“太近”,如果用户距离超过100厘米,则显示“太远”。我需要修改它,以便在检测时间超过10秒时显示结果(太近/太远)。

例如:如果用户离屏幕太近(<50厘米)超过10秒,则会显示结果为太近。

我已经提供了此程序的代码如下:

# 你提供的代码部分不需要翻译
英文:

This is a face distance measurement program with OpenCV. It shows "Too close" if the user is less than 50cm from the screen and "Too Far" if the user is more than 100cm distance. I need to make it to display the result (Too close/Too far) if the detection time is more than 10 seconds.

Ex: If the user stays too close (<50cm) to the screen for more than 10s, then it will show result as too close.

I have provided the code to this program below


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&#39;Distance: {int(d)}cm&#39;,
        #                    (face[10][0]-100, face[10][1] - 50),
        #                    scale=2)

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

        elif d &gt; 100:
            cvzone.putTextRect(img, f&#39;Too Far&#39;,
                               (face[10][0] - 100, face[10][1] - 50),
                               scale=2)

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

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

    cv2.imshow(&quot;Image&quot;, img)
    cv2.waitKey(1)

答案1

得分: 1

如果我理解正确,您好像没有考虑到3D坐标轴,只考虑了最左边点和最右边点之间的距离。

如果用户略微转动头部,这将使您的计算出现错误,因为它只考虑了这两个点的距离屏幕坐标(两个轴)。

通过考虑所有坐标轴,您可以计算出这两个点的中间点,然后只在中间点的“深度”轴(通常是[z]轴)值太小或太大时发送消息。

实际上,我正在研究如何获取它们的“z”值,如果您感兴趣的话,我会告诉您(如果我找到获取它们的方法的话)。祝好运!

英文:

Stop me if I'm wrong but I have the impression that you don't take into account the points 3D axes, you only take into account the distance between the leftmost point and the rightmost point.

If the user has a slight head rotation, this will corrupt your calculation since it only takes into account the distance from those two points screen coordinates (two axes).

By taking into account all axis, you could calculate the middle of these same two points and send your messages only if the "depth" axis (generally [z]) value of your middle point is too small/big.

Actually working on how getting the 'z' value of them, I'll let you know if you're interested (and if I find the way to get them ofc). Good luck!

huangapple
  • 本文由 发表于 2023年3月8日 19:15:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定