yolov5检测单一类别

版本1:

 label = f'{names[int(cls)]} {conf:.2f} '
    if label is not None:
        if (label.split())[0] == 'person':
             plot_one_box(xyxy, im0, label=label, color=colors[int(cls)],line_thickness=3)
        if (label.split())[0] == 'car' or (label.split())[0] =='truck':
             plot_one_box(xyxy, im0, label=label, color=colors[int(cls)],line_thickness=3)

 版本2:

if names[int(cls)] == "person":
    c = int(cls)  # integer class  整数类 1111111111
    label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')  # 111
    txt = '{0}'.format(label)
    # annotator.box_label(xyxy, txt, color=(255, 0, 255))
    annotator.box_label(xyxy, txt, color=colors(c, True))
if names[int(cls)] == "chair":
    c = int(cls)  # integer class  整数类 1111111111
    label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}') # 111
    txt = '{0}'.format(label)
    #annotator.box_label(xyxy, txt, color=(255, 0, 255))
    annotator.box_label(xyxy, txt, color = colors(c, True))

  版本3:

 for *xyxy, conf, cls in reversed(det):
                    conf2 = float(f'{conf:.2f}')
                    if conf2 > 0.4:   # 置信度小于0.4时不显示
                        # person,显示person标签的框,并单独做person的测距
                        if names[int(cls)] == 'person':
                            if save_txt:  # Write to file
                                xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(
                                    -1).tolist()  # normalized xywh
                                line = (cls, *xywh, conf) if opt.save_conf else (cls, *xywh)  # label format
                                with open(txt_path + '.txt', 'a') as f:
                                    f.write(('%g ' * len(line)).rstrip() % line + '\n')

                            if save_img or view_img:  # Add bbox to image
                                label = f'{names[int(cls)]} {conf:.2f}'
                                plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3,
                                             name=names[int(cls)])   # 画框函数
                        # car,显示car标签的框,并单独做car的测距
                        if names[int(cls)] == 'car':
                            if save_txt:  # Write to file
                                xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(
                                    -1).tolist()  # normalized xywh
                                line = (cls, *xywh, conf) if opt.save_conf else (cls, *xywh)  # label format
                                with open(txt_path + '.txt', 'a') as f:
                                    f.write(('%g ' * len(line)).rstrip() % line + '\n')

                            if save_img or view_img:  # Add bbox to image
                                label = f'{names[int(cls)]} {conf:.2f}'
                                plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3,
                                             name=names[int(cls)])


          
       

你可能感兴趣的:(YOLO,yolov5报错,python)