我正在进行面部检测应用。 我需要获取图像中每个检测到的面部的边界框坐标。 这是代码,问题是它只在检测到多个面的场景中打印一个面的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张脸检测到)
有人可以帮我弄清楚如何获取所有检测到的面孔的bbox?
提前谢谢
答案 0 :(得分:1)
您应该遍历total_boxes
以打印坐标。
将for z in b :
替换为for z in total_boxes :
所以你的更新代码应该是
for box in total_boxes:
for coord in box:
print(int(coord), end=" ", flush=True)
print()