有没有办法通过使用 opencv/dlib 和直播视频来获取前额(边界框)的面积

Is there a way to get the area of the forehead (bounding box) by using opencv/dlib and for a live stream video

我一直在做一个项目,从实时流媒体视频中获取前额区域,而不仅仅是像这个例子中那样使用和成像并裁剪前额 How can i detect the forehead region using opencv and dlib?

cap = cv2.VideoCapture(0)

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predict_path)


while True:
    _, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = detector(gray) #detects number of faces present

    for face in faces:
        x1 = face.left()
        y1 = face.top()
        x2 = face.right()
        y2 = face.bottom()
        
        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)
        
        landmarks = predictor(gray, face)

        for n in range(68, 81):
            x = landmarks.part(n).x
            y = landmarks.part(n).y

            cv2.circle(frame, (x, y), 4, (0, 255, 0), -1) 
            

我设法使用 https://github.com/codeniko/shape_predictor_81_face_landmarks/blob/master/shape_predictor_81_face_landmarks.dat

的地标获得了前额区域

但我需要的是在检测前额区域时地标所在位置的矩形边界框。这有可能得到吗?如果没有,我应该怎么做才能得到前额区域。提前致谢。

您已经通过以下方式找到了所需的坐标:

for face in faces:
    x1 = face.left()
    y1 = face.top()
    x2 = face.right()
    y2 = face.bottom()

    cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)

But what I need is the rectangle bounding box onto where the landmark is at detecting the forehead region.

然后改变y-coordinates:

cv2.rectangle(frame, (x1, y1-100), (x2, y2-100), (0, 0, 255), 3)

更新

为了固定额头点,我们需要得到最小和最大landmark坐标,然后我们需要绘制矩形。

第一步:获取坐标:


    1. 初始化x_ptsy_pts
    1. landmark(n) 点存储到数组中。
for n in range(68, 81):
    x = landmarks.part(n).x
    y = landmarks.part(n).y

    x_pts.append(x)
    y_pts.append(y)

    cv2.circle(frame, (x, y), 4, (0, 255, 0), -1)

第 2 步:在检测到的点周围绘制矩形


    1. 获得最低和最高分
x1 = min(x_pts)
x2 = max(x_pts)
y1 = min(y_pts)
y2 = max(y_pts)

cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)

结果:

当我缩放到网络摄像头时:

当我在远方时:

代码:

import cv2
import dlib

cap = cv2.VideoCapture(0)

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_81_face_landmarks.dat")

while True:
    _, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = detector(gray)  # detects number of faces present

    for face in faces:
        x1 = face.left()
        y1 = face.top()
        x2 = face.right()
        y2 = face.bottom()

        landmarks = predictor(gray, face)

        x_pts = []
        y_pts = []

        for n in range(68, 81):
            x = landmarks.part(n).x
            y = landmarks.part(n).y

            x_pts.append(x)
            y_pts.append(y)

            cv2.circle(frame, (x, y), 4, (0, 255, 0), -1)

        x1 = min(x_pts)
        x2 = max(x_pts)
        y1 = min(y_pts)
        y2 = max(y_pts)

        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)

    cv2.imshow("out", frame)
    key = cv2.waitKey(1) & 0xFF

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break