1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| @torch.no_grad() def out_img(img0, model, device, half, stride, names, imgsz): img = letterbox(img0, imgsz, stride=stride, auto=True)[0] img = img.transpose((2, 0, 1))[::-1] img = np.ascontiguousarray(img) model(torch.zeros(1, 3, *imgsz).to(device).type_as(next(model.parameters()))) img = torch.from_numpy(img).to(device) img = img.half() if half else img.float() img = img / 255.0
if len(img.shape) == 3: img = img[None]
pred = model(img, augment=False, visualize=False)[0] pred = non_max_suppression(pred, CONF_THRES, IOU_THRES, None, False, max_det=1000) det = pred[0]
im0 = img0.copy() gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]
annotator = Annotator(im0, line_width=LINE_THICKNESS, example=str(names)) xywh_list = [] if len(det): det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() for *xyxy, conf, cls in reversed(det): c = int(cls) xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() xywh_list.append(xywh) label = None if HIDE_LABELS else (names[c] if HIDE_CONF else f'{names[c]} {conf:.2f}') annotator.box_label(xyxy, label, color=colors(c, True)) im0 = annotator.result() return im0, xywh_list
|