城管三模型代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.1KB

  1. import numpy as np
  2. import torch
  3. from utils.datasets import letterbox
  4. from utils.general import non_max_suppression, overlap_box_suppression, scale_coords
  5. def yolo_process(img0, model, device, args, half):
  6. # Padded resize
  7. stride = int(model.stride.max()) # model stride
  8. img, ratio, (dw, dh) = letterbox(img0, args.yoloimg_size, stride=stride)
  9. # Convert
  10. img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
  11. img = np.ascontiguousarray(img)
  12. img = torch.from_numpy(img).to(device)
  13. img = img.half() if half else img.float() # uint8 to fp16/32
  14. img /= 255.0 # 0 - 255 to 0.0 - 1.0
  15. if img.ndimension() == 3:
  16. img = img.unsqueeze(0)
  17. pred = model(img, augment=args.augment)[0]
  18. # Apply NMS
  19. pred = non_max_suppression(pred, args.conf_thres, args.iou_thres, classes=args.classes, agnostic=args.agnostic_nms)
  20. pred = overlap_box_suppression(pred, args.ovlap_thres)
  21. ###一次检测一张图片
  22. det = pred[0]
  23. if len(det):
  24. # Rescale boxes from img_size to im0 size
  25. det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
  26. return det