我遇到需要检测某人的head
的情况。为此,我先使用人员检测模型,然后使用人员边界框提取人员的头部。
person_detector.setInput(blob)
person_detections = person_detector.forward()
boundingboxes = np.array([])
for i in np.arange(0, person_detections.shape[2]):
person_det_confidence = person_detections[0, 0, i, 2]
if person_det_confidence > 0.40:
# extract the index of the class label from the
# detections list
idx = int(person_detections[0, 0, i, 1])
# if the class label is not a person, ignore it
if CLASSES[idx] != "person":
continue
# compute the (x, y)-coordinates of the bounding box
# for the object
person_box = person_detections[0, 0, i, 3:7] * np.array([W, H, W, H])
(startX, startY, endX, endY) = person_box.astype("int")
# Only append the person which are inside ROI
if roix1 < startX < roix2:
rects.append(person_box)
boundingboxes = np.array(rects)
boundingboxes = boundingboxes.astype(int)
person_rects = helper.non_max_suppression_fast(boundingboxes, 0.3)
head_rects = []
for (startX, startY, endX, endY) in person_rects:
img = frame[startY: startY + endY, startX: startX + endX]
(startX, startY, endX, endY) = detect_head(img)
p = (startX, startY, endX, endY)
head_rects.append(p)
在上面的代码person_rects
中包含检测到的人物。我们将其传递给函数detect_head
,该函数返回保存在head_rects中的头部边界框。
head_rects
如下所示:
startX = 0
startY = 4
endX = 384
endY = 61
现在,如果我使用上述坐标绘制矩形:
cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2)
绘制错误:
在上图中,您可以看到框架的左上方绘制了红色矩形,而矩形应该是女士站在框架中的位置。原因是,检测到的头部的坐标是相对于人的,而不是相对于框架的,因此在显示时会被错误地绘制。谁能给我一些有关如何纠正它的想法。请帮忙。谢谢