Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions codes/17.YOLOv5 jetson nano 工地防护检测/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,25 @@ def get_person_info_list(self,person_list,hat_list,vest_list):

person_info_item[0]= person_box
# 依次与帽子计算IOU
max_hat_iou = 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在修改后的代码中,虽然通过跟踪最大IoU值可以确保选择最佳匹配对象,但hat_iou_thresh和vest_iou_thresh两个阈值被硬编码为0,这可能不是最佳实践。建议将这些阈值作为参数传入函数或从配置文件中读取,以提高代码的灵活性和可维护性。

Suggested change
max_hat_iou = 0
# 建议将阈值作为参数传入函数
def get_person_info_list(self, person_list, hat_list, vest_list, hat_iou_thresh=0.5, vest_iou_thresh=0.5):
"""
获取每个人的信息人体框安全帽防护服
@param: person_list list 人体框列表
@param: hat_list list 安全帽列表
@param: vest_list list 防护服列表
@param: hat_iou_thresh float 安全帽IOU阈值
@param: vest_iou_thresh float 防护服IOU阈值
@return person_info_list list
"""
# 使用传入的阈值参数而不是硬编码
person_info_list = []
for person in person_list:
person_info_item = [[],[],[]]
# 人体框
person_box = person[:5]
person_info_item[0]= person_box
# 依次与帽子计算IOU
max_hat_iou = 0
for hat in hat_list:
hat_box = hat[:6]
hat_iou = self.get_iou(person_box, hat_box)
if hat_iou > hat_iou_thresh and hat_iou > max_hat_iou:
person_info_item[1] = hat_box
max_hat_iou = hat_iou
# 依次与防护服计算IOU
max_vest_iou = 0
for vest in vest_list:
vest_box = vest[:5]
vest_iou = self.get_iou(person_box, vest_box)
if vest_iou > vest_iou_thresh and vest_iou > max_vest_iou:
person_info_item[2] = vest_box
max_vest_iou = vest_iou
person_info_list.append(person_info_item)
return person_info_list

for hat in hat_list:
hat_box = hat[:6]
hat_iou = self.get_iou(person_box, hat_box)

if hat_iou > hat_iou_thresh:
if hat_iou > hat_iou_thresh and hat_iou > max_hat_iou:
person_info_item[1] = hat_box
break
max_hat_iou = hat_iou

# 依次与防护服计算IOU
max_vest_iou = 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

虽然优化后的算法能确保找到最佳匹配的对象,但时间复杂度有所增加。原实现中,一旦找到满足阈值的对象就会break,而修改后的实现需要遍历所有对象以找到最大IoU值。在对象数量较多的情况下,这可能会影响性能。建议添加注释说明这一权衡,或在性能关键场景下考虑其他优化方案。

Suggested change
max_vest_iou = 0
# 可以添加注释说明为何选择遍历所有对象而不是提前break
# 这样做是为了确保找到IoU最大的匹配对象,提高检测准确性
# 但会增加时间复杂度,在对象数量多时可能影响性能

for vest in vest_list:
vest_box = vest[:5]
vest_iou = self.get_iou(person_box, vest_box)


if vest_iou > vest_iou_thresh:
if vest_iou > vest_iou_thresh and vest_iou > max_vest_iou:
person_info_item[2] = vest_box
break
max_vest_iou = vest_iou

person_info_list.append(person_info_item)

Expand Down