首页 > 解决方案 > 打开 CV 在视频帧 PYTHON 中找不到超过一张脸

问题描述

我将创建一个使用人脸检测模型 (face_model) 检测人脸的函数,并使用我训练的 face_mask 模型从 face_mask 预测返回人脸位置和预测。

但是在使用视频时,每帧一次只能检测到一张脸。

这是我的代码

def detect_and_predict_mask(video_frame, face_model, mask_model):
    (h, w) = video_frame.shape[:2]
    blob = cv2.dnn.blobFromImage(video_frame, 1.0, (300, 300), (104.0, 177.0, 123.0))
    # pass the blob through the network and obtain the face detections
    face_model.setInput(blob)
    detections = face_model.forward()
    print(detections.shape)

    faces = []
    locations = []
    predictions = []

    for i in range(0, detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence > CONFIDENCE:
            face_box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (start_x, start_y, end_x, end_y) = face_box.astype("int")
            (start_x, start_y) = (max(0, start_x), max(0, start_y))
            (end_x, end_y) = (min(w - 1, end_x), min(h - 1, end_y))

            face = video_frame[start_y:end_y, start_x:end_x]
            face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
            face = cv2.resize(face, (224, 224))
            face = img_to_array(face)
            face = preprocess_input(face)

            faces.append(face)
            locations.append((start_x, start_y, end_x, end_y))
            print(f'FACES: {len(faces)}')
            print(f'locations: {locations}')

            if len(faces) > 0:
                # for faster inference we'll make batch predictions on *all* faces at the same time rather than one-by-one predictions in the above `for` loop
                faces = np.array(faces, dtype="float32")
                predictions = mask_model.predict(faces, batch_size=32)

            return locations, predictions

标签: pythonopencvmachine-learning

解决方案


推荐阅读