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)])

   版本4:

如果知道标签,直接在classes里边改成标签序号即可,如coco数据集有80种类别:

person, bicycle, car, motorcycle, airplane, bus, train, truck, boat, traffic light, fire hydrant, stop sign, parking meter, bench, bird, cat, dog, horse, sheep, cow, elephant, bear, zebra, giraffe, backpack, umbrella, handbag, tie, suitcase, frisbee, skis, snowboard, sports_ball, kite, baseball bat, baseball glove, skateboard, surfboard, tennis racket, bottle, wine glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange, broccoli, carrot, hot_dog, pizza, donut, cake, chair, couch, potted plant, bed, dining table, toilet, tv, laptop, mouse, remote, keyboard, cell phone, microwave, oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy bear, hair drier, toothbrush。

假设我们只想要检测行人,已知person为第一个,索引为0, 所以直接在classes后加上default=[0]

同理,如需检测person和car,将classes后加上default=[0,2]

    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', nargs='+', type=str, default='./yolov5s.pt', help='model.pt path(s)')
    parser.add_argument('--source', type=str, default='./a2.mp4',help='source')
    parser.add_argument('--classes', nargs='+',default=[0], type=int, help='filter by class: --class 0, or --class 0 2 3')


          
       

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

积极向上的mr.d

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值