opencv识别面部并测算据摄像头的距离—代码篇

opencv识别面部并测算据摄像头的距离—代码篇_第1张图片

from cvzone.FaceMeshModule import FaceMeshDetector
import cv2
import cvzone

cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces=1)
while True:
    success, img = cap.read()
    img, faces = detector.findFaceMesh(img, draw=False)
    if faces:
        face = faces[0]
        pointLeft = face[145]
        pointRight = face[374]
        # 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)
        W = 6.3
        f = 840
        d = (W * f) / w
        print(d)
        cvzone.putTextRect(img, f'Depth: {int(d)}cm',
                           (face[10][0] - 100, face[10][1] - 50),
                           scale=2)
    cv2.imshow("Image", img)
    cv2.waitKey(1)

觉得有用的小伙伴可以关注一下哦,其原理后续会出,其测距原理可以参考一下我之前写的一篇博客

https://blog.csdn.net/hyayq8124/article/details/122444133

你可能感兴趣的:(opencv,opencv,计算机视觉,人工智能)