版本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')