yolov5目标检测中如何实现检测框中心坐标按从小到大输出
在YOLOv5中,可以通过修改代码来实现检测框中心坐标按从小到大输出的功能。具体步骤如下:
-
打开
detect.py文件,该文件位于YOLOv5代码的yolov5目录下。 -
找到
detect.py文件中的plot_one_box函数,该函数用于绘制检测框。 -
在
plot_one_box函数中添加一个参数center_sort,用于控制是否按照中心坐标排序。修改后的函数如下所示:
def plot_one_box(x, im, color=None, label=None, line_thickness=None, center_sort=False):
# ... (省略部分代码)
if center_sort:
centers = [(x[0] + x[2]) / 2, (x[1] + x[3]) / 2]
center_distance = centers[0] + centers[1] # 计算中心坐标距离,可以根据需要修改计算方式
return center_distance
# ... (省略部分代码)
- 在
detect.py文件中的detect函数中,找到以下代码块:
for i, det in enumerate(detections): # detections per image
# ... (省略部分代码)
# plot_boxes函数会遍历所有的检测框并调用plot_one_box函数绘制每个检测框
plot_boxes(img, det, names=self.names, color=self.colors, line_thickness=3)
# ... (省略部分代码)
- 将以上代码块修改为如下形式:
# 检测框按中心坐标排序
detections = sorted(detections, key=lambda x: plot_one_box(x, img, center_sort=True))
for i, det in enumerate(detections): # detections per image
# ... (省略部分代码)
# plot_boxes函数会遍历所有的检测框并调用plot_one_box函数绘制每个检测框
plot_boxes(img, det, names=self.names, color=self.colors, line_thickness=3)
# ... (省略部分代码)
- 保存文件并重新运行YOLOv5目标检测程序。此时,检测框的中心坐标将按从小到大的顺序输出
原文地址: https://www.cveoy.top/t/topic/h8FI 著作权归作者所有。请勿转载和采集!