python - 有没有一种方法可以通过使用opencv/dlib和直播视频来获取额头(边界框)的区域

标签 python python-3.x 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, 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坐标:
cv2.rectangle(frame, (x1, y1-100), (x2, y2-100), (0, 0, 255), 3)
更新
要坚持到额头,我们需要获得最小和最大landmark坐标,然后需要绘制矩形。
步骤1:获取坐标:

  • 初始化x_ptsy_pts


  • 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:在检测到的点周围绘制矩形

  • 获取最低和最高分


  • 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)
    
    结果:
    当我放大网络摄像头时:
    enter image description here
    当我很远的时候:
    enter image description here
    码:
    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
    

    关于python - 有没有一种方法可以通过使用opencv/dlib和直播视频来获取额头(边界框)的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63770831/

    相关文章:

    python - 删除每个单词除第一个字母外的所有字母并保留标点符号

    image-processing - 帮助 OpenCV cvInitUndistortRectifyMap 和单个相机

    python-3.x - 如何在FastAPI中将文件多文件上传字段设置为可选字段

    python - Python 在全局范围内提供一些默认方法的原因是什么?

    python - 查找给定 torrent 文件的播种者/对等者 IP 地址的最佳方法是什么

    python - 如何使用 Python 解锁锁定的文件和文件夹 (mac)

    python - Pandas str.extract 可一起用于过滤数据和交叉表吗?

    python - Opencv Python - 来自特征匹配+单应性的相似度分数

    python-2.7 - OpenCV:无法加载 OpenCL 运行时

    python - Django 1.2 + South 0.7 + django-annoying 的 AutoOneToOneField 导致 TypeError : 'LegacyConnection' object is not iterable