有没有一种方法可以通过使用opencv / dlib和直播视频来获取额头边界框的面积

问题描述

我一直在进行一个项目,目的是从实时流式视频中获取前额区域,而不仅仅是像本示例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,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,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,255),3)

但是我需要的是矩形边界框,该矩形框位于地标检测前额区域的位置。

然后更改y坐标:

cv2.rectangle(frame,y1-100),y2-100),3)

更新

要坚持住前额点,我们需要获取最小landmark和最大x_pts坐标,然后绘制矩形。

第一步:获取坐标:


    1. 初始化y_ptslandmark(n)
    1. 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,255,0),-1) 点存储到数组中。
x1 = min(x_pts)
x2 = max(x_pts)
y1 = min(y_pts)
y2 = max(y_pts)

cv2.rectangle(frame,3)

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


    1. 获取最低和最高积分
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,-1)

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

        cv2.rectangle(frame,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

结果:

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

enter image description here

当我很远的时候:

enter image description here

代码:

<ng-template [ngVar]="variable">
your code
</ng-template>