You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MediaPipe实时人脸检测中绘制人脸框不显示问题求助

MediaPipe实时人脸检测中绘制人脸框不显示问题求助

我正在尝试用MediaPipe库写一个通过摄像头实时检测人脸的简单代码,但遇到了一个棘手的问题:当我试图在mp_image上绘制人脸框时,框根本不显示出来。有没有小伙伴能帮我排查一下问题呀?

我先把代码贴出来:

#------ importamos las librerias -----------#
import cv2
import mediapipe as mp
import numpy as np


#--------- Declarar el detector --------#
detector = mp.tasks.vision.FaceDetector
dibujo = mp.solutions.drawing_utils

#----realizar la video captura----#
cap = cv2.VideoCapture(0)

#------Inicializamos parametros de configuracion-----#
BaseOptions = mp.tasks.BaseOptions
FaceDetectorOptions = mp.tasks.vision.FaceDetectorOptions
FaceDetectorResult = mp.tasks.vision.FaceDetectorResult
VisionRunningMode = mp.tasks.vision.RunningMode



#------- Crear una instancia para la detencion en tiempo real ------#
def print_result(result: FaceDetectorResult, output_image, timestamp_ms: int):
    print('face detector result: {}'.format(result))

    # Verificar si existen detecciones para dibujarlas
    if result.detections:
        # dibujar cada rostro detectado
        for detection in result.detections:

            #PROBLEMAS CON LOS TIPO DE DATOS
            output_image = np.array(output_image)
            print(f"image = {type(output_image)}")
            det_pb2 = detection.to_pb2()
            dibujo.draw_detection(output_image, det_pb2)
            print('detected faces')

#-----------Iniciamos la configuracion----------------#
options = FaceDetectorOptions(
    base_options = BaseOptions(model_asset_path='model.tflite'),
    running_mode = VisionRunningMode.LIVE_STREAM,
    min_detection_confidence = 0.5,
    result_callback = print_result
)


# Obtén la tasa de cuadros por segundo (FPS) para calcular el timestamp
fps = cap.get(cv2.CAP_PROP_FPS)
frame_number = 0

with detector.create_from_options(options) as rostros:


    while True:

        # La lectura de la video captura
        ret, frame = cap.read()

        # Eliminar el error de movimiento
        frame = cv2.flip(frame,1)

        # Correcion de color
        rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # Obtener frame_timestamp_ms
        frame_timestamp_ms = int((frame_number / fps) * 1000)
        frame_number += 1

        # Convertimos el frame a modelo de imagen mp.image de mediapipe
        mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb)
        # Mandamos la deteccion del rostro en frame
        rostros.detect_async(mp_image,frame_timestamp_ms)


        #Mostramos los fotogramas
        cv2.imshow("Camara", mp_image)

        #Leemos el teclado
        t = cv2.waitKey(1)
        if t == 27:
            break

cap.release()
cv2.destroyAllWindows()

我已经尝试过一些方法,比如修改图像的数据类型、调整检测结果的类型,但都没能解决问题,人脸框还是不显示。麻烦大家帮我看看问题出在哪了,谢谢啦!

备注:内容来源于stack exchange,提问作者Terry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.16 02:53:15