Skip to main content
 首页 » 编程设计

python之如何获取检测到的人脸的边界框xy坐标

2025年01月19日9kuangbin

我正在为人脸检测应用程序工作。 我需要获取图像中每个检测到的人脸的边界框坐标。 这是代码,问题是它仅在检测到多张人脸的情况下为一张人脸打印 bbox 坐标。

import mxnet as mx 
import cv2 
import os 
 
detector = MtcnnDetector(model_folder='model', ctx=mx.cpu(0)) 
img = cv2.imread("amm.png") 
results = detector.detect_face(img, False) 
t2 = time.time() -t1 
print("time: ",t2)  
if results is not None: 
total_boxes = results[0] 
points = results[1] 
draw = img.copy() 
for b in total_boxes: 
    cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])),(250, 100, 0),1) 
face_count = 0   # counting  number of detected faces within the image 
for b in total_boxes: 
     face_count +=1 
print("detected faces" , face_count) # printing number of detected faces. 
# I want to print the x and y  coordinates of every faces detected 
for z in b : 
    print(int(z), end=" ", flush=True) 

这是我得到的结果:

检测到人脸 12
199 302 257 377 0(这个 bbox 只有一张脸,而检测到 12 张脸)

有人可以帮我弄清楚如何获取所有检测到的人脸的 bboxes 吗?

提前致谢

请您参考如下方法:

您应该遍历 total_boxes 以打印坐标。

将 for z in b : 替换为 for z in total_boxes :

所以你更新后的代码应该是这样的

total_boxes 中的框: 对于盒子中的坐标: 打印(int(坐标),结束=“”,flush=True) 打印()