有没有一种方法可以通过使用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

查看:77
本文介绍了有没有一种方法可以通过使用opencv/dlib和直播视频来获取前额(边界框)的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在进行一个项目,以从实时流式视频中获取前额区域,而不仅仅是像本例中那样使用和成像并裁剪前额,例如本例中的

当我很远的时候:

代码:

  import cv2导入dlib上限= cv2.VideoCapture(0)检测器= dlib.get_frontal_face_detector()预测变量= dlib.shape_predictor("shape_predictor_81_face_landmarks.dat")而True:_,框架= cap.read()灰色= cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)人脸=检测器(灰色)#检测存在的人脸数量面对面:x1 = face.left()y1 = face.top()x2 = face.right()y2 = face.bottom()地标=预测变量(灰色,面部)x_pts = []y_pts = []对于范围n中的n(68,81):x =地标.part(n).xy =地标.part(n).yx_pts.append(x)y_pts.append(y)cv2.circle(frame,(x,y),4,(0,255,0),-1)x1 =最小值(x_pts)x2 =最大值(x_pts)y1 =最小值(y_pts)y2 =最大值(y_pts)cv2.rectangle(frame,(x1,y1),(x2,y2),(0,0,255),3)cv2.imshow("out",框架)键= cv2.waitKey(1)&0xFF#如果按了q键,则从循环中中断如果键== ord("q"):休息 

I've been working on a project to get the forehead area from a live streaming video and not just use and image and crop the forehead like from this example 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) 
            

I managed to get the forehead region using the landmarks of using https://github.com/codeniko/shape_predictor_81_face_landmarks/blob/master/shape_predictor_81_face_landmarks.dat

But what I need is the rectangle bounding box onto where the landmark is at detecting the forehead region. Is this possible to get? If not, what should I do to get the forehead region. Thanks in advance.

解决方案

you already find the desired coordinates by:

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.

Then change the y-coordinates:

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

Update

To stick to the forehead points, we need to get minimum and maximum landmark coordinates, then we need to draw rectangle.

Step1: Getting coordinates:


    1. Initialize x_pts and y_pts

    1. Store landmark(n) points into the arrays.

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)

Step 2: Drawing the rectangle around detected points


    1. Get Minimum and Maximum points

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)

Result:

When I zoom to the webcam:

When I'm far away:

Code:

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

这篇关于有没有一种方法可以通过使用opencv/dlib和直播视频来获取前额(边界框)的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆