predict_largepic.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license
  2. """
  3. Run YOLOv5 detection inference on images, videos, directories, globs, YouTube, webcam, streams, etc.
  4. Usage - sources:
  5. $ python detect.py --weights yolov5s.pt --source 0 # webcam
  6. img.jpg # image
  7. vid.mp4 # video
  8. screen # screenshot
  9. path/ # directory
  10. list.txt # list of images
  11. list.streams # list of streams
  12. 'path/*.jpg' # glob
  13. 'https://youtu.be/Zgi9g1ksQHc' # YouTube
  14. 'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream
  15. Usage - formats:
  16. $ python detect.py --weights yolov5s.pt # PyTorch
  17. yolov5s.torchscript # TorchScript
  18. yolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnn
  19. yolov5s_openvino_model # OpenVINO
  20. yolov5s.engine # TensorRT
  21. yolov5s.mlmodel # CoreML (macOS-only)
  22. yolov5s_saved_model # TensorFlow SavedModel
  23. yolov5s.pb # TensorFlow GraphDef
  24. yolov5s.tflite # TensorFlow Lite
  25. yolov5s_edgetpu.tflite # TensorFlow Edge TPU
  26. yolov5s_paddle_model # PaddlePaddle
  27. """
  28. import argparse
  29. import os
  30. import platform
  31. import sys
  32. from pathlib import Path
  33. import numpy as np
  34. import torch
  35. FILE = Path(__file__).resolve()
  36. ROOT = FILE.parents[0] # YOLOv5 root directory
  37. if str(ROOT) not in sys.path:
  38. sys.path.append(str(ROOT)) # add ROOT to PATH
  39. ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
  40. from models.common import DetectMultiBackend
  41. from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages, LoadImagesWhole, LoadScreenshots, LoadStreams
  42. from utils.general import (LOGGER, Profile, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,
  43. increment_path, non_max_suppression, print_args, scale_boxes, strip_optimizer, xyxy2xywh)
  44. from utils.plots import Annotator, colors, save_one_box
  45. from utils.torch_utils import select_device, smart_inference_mode
  46. @smart_inference_mode()
  47. def run(
  48. weights=ROOT / 'weights\\yolov5s.pt', # model path or triton URL
  49. source=ROOT / 'data\\HRSID_YOLO\\test\\images', # file/dir/URL/glob/screen/0(webcam)
  50. data=ROOT / 'data/coco128.yaml', # dataset.yaml path
  51. imgsz=(640, 640), # inference size (height, width)
  52. conf_thres=0.25, # confidence threshold
  53. iou_thres=0.45, # NMS IOU threshold
  54. max_det=1000, # maximum detections per image
  55. device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
  56. view_img=False, # show results
  57. save_txt=False, # save results to *.txt
  58. save_conf=False, # save confidences in --save-txt labels
  59. save_crop=False, # save cropped prediction boxes
  60. nosave=False, # do not save images/videos
  61. classes=None, # filter by class: --class 0, or --class 0 2 3
  62. agnostic_nms=False, # class-agnostic NMS
  63. augment=False, # augmented inference
  64. visualize=False, # visualize features
  65. update=False, # update all models
  66. project=ROOT / 'runs/predict-seg', # save results to project/name
  67. name='exp1', # save results to project/name
  68. exist_ok=False, # existing project/name ok, do not increment
  69. line_thickness=3, # bounding box thickness (pixels)
  70. hide_labels=False, # hide labels
  71. hide_conf=False, # hide confidences
  72. half=False, # use FP16 half-precision inference
  73. dnn=False, # use OpenCV DNN for ONNX inference
  74. vid_stride=1, # video frame-rate stride
  75. retina_masks = False,
  76. batch_size = 16
  77. ):
  78. source = str(source)
  79. save_img = not nosave and not source.endswith('.txt') # save inference images
  80. is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
  81. is_url = source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
  82. webcam = source.isnumeric() or source.endswith('.streams') or (is_url and not is_file)
  83. screenshot = source.lower().startswith('screen')
  84. if is_url and is_file:
  85. source = check_file(source) # download
  86. # Directories
  87. save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
  88. (save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
  89. (save_dir / 'jsons' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) #make dir
  90. # 书写日志
  91. logFile = save_dir / 'detect.txt'
  92. import logging
  93. # 创建一个FileHandler,并将日志写入指定的日志文件中
  94. fileHandler = logging.FileHandler(logFile, mode='a')
  95. fileHandler.setLevel(logging.INFO)
  96. f_for_val = open(save_dir / 'detect_for_val.txt', "w", encoding="utf-8")
  97. print(save_dir / 'detect_for_val.txt')
  98. # 定义Handler的日志输出格式
  99. # formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  100. formatter = logging.Formatter('%(asctime)s - %(message)s')
  101. fileHandler.setFormatter(formatter)
  102. # 添加Handler
  103. LOGGER.addHandler(fileHandler)
  104. # Load model
  105. device = select_device(device)
  106. model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
  107. stride, names, pt = model.stride, model.names, model.pt
  108. imgsz = check_img_size(imgsz, s=stride) # check image size
  109. # Dataloader
  110. bs = 1 # batch_size
  111. if webcam:
  112. view_img = check_imshow(warn=True)
  113. dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)
  114. bs = len(dataset)
  115. elif screenshot:
  116. dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt)
  117. else:
  118. dataset = LoadImagesWhole(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)
  119. vid_path, vid_writer = [None] * bs, [None] * bs
  120. # Run inference
  121. model.warmup(imgsz=(1 if pt else bs, 3, *imgsz)) # warmup
  122. seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
  123. for path, im, im0s, vid_cap, s in dataset:
  124. with dt[0]:
  125. im = torch.from_numpy(im).to(model.device)
  126. im = im.half() if model.fp16 else im.float() # uint8 to fp16/32
  127. im /= 255 # 0 - 255 to 0.0 - 1.0
  128. if len(im.shape) == 3:
  129. im = im[None] # expand for batch dim
  130. # Inference
  131. with dt[1]:
  132. visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False
  133. pred = model(im, augment=augment, visualize=visualize)
  134. # NMS
  135. with dt[2]:
  136. pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
  137. # Second-stage classifier (optional)
  138. # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)
  139. # Process predictions
  140. for i, det in enumerate(pred): # per image
  141. seen += 1
  142. if webcam: # batch_size >= 1
  143. p, im0, frame = path[i], im0s[i].copy(), dataset.count
  144. s += f'{i}: '
  145. else:
  146. p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
  147. p = Path(p) # to Path
  148. save_path = str(save_dir / p.name) # im.jpg
  149. txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # im.txt
  150. labelme_path = str(save_dir / 'jsons' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # im.json
  151. label_dict = {}
  152. label_dict['version'] = '5.0.1'
  153. label_dict['flags'] = {}
  154. label_dict['imageData'] = None
  155. label_dict['imagePath'] = p.name
  156. label_dict['imageHeight'] = im0.shape[0]
  157. label_dict['imageWidth'] = im0.shape[1]
  158. label_dict['shapes'] = []
  159. s += '%gx%g ' % im.shape[2:] # print string
  160. gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
  161. imc = im0.copy() if save_crop else im0 # for save_crop
  162. annotator = Annotator(im0, line_width=line_thickness, example=str(names))
  163. if len(det):
  164. # Rescale boxes from img_size to im0 size
  165. det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round()
  166. # Print results
  167. for c in det[:, 5].unique():
  168. n = (det[:, 5] == c).sum() # detections per class
  169. s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
  170. # Write results
  171. for *xyxy, conf, cls in reversed(det):
  172. if save_txt: # Write to file
  173. xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
  174. line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format
  175. with open(f'{txt_path}.txt', 'a') as f:
  176. f.write(('%g ' * len(line)).rstrip() % line + '\n')
  177. if save_img or save_crop or view_img: # Add bbox to image
  178. c = int(cls) # integer class
  179. label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
  180. annotator.box_label(xyxy, label, color=colors(c, True))
  181. if save_crop:
  182. save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True)
  183. if save_txt: # Write to file
  184. import json
  185. with open(f'{labelme_path}.json', 'w') as f:
  186. json.dump(label_dict, f)
  187. # Stream results
  188. im0 = annotator.result()
  189. if view_img:
  190. if platform.system() == 'Linux' and p not in windows:
  191. windows.append(p)
  192. cv2.namedWindow(str(p), cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux)
  193. cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0])
  194. cv2.imshow(str(p), im0)
  195. cv2.waitKey(1) # 1 millisecond
  196. # Save results (image with detections)
  197. if save_img:
  198. if dataset.mode == 'image':
  199. cv2.imwrite(save_path, im0)
  200. else: # 'video' or 'stream'
  201. if vid_path[i] != save_path: # new video
  202. vid_path[i] = save_path
  203. if isinstance(vid_writer[i], cv2.VideoWriter):
  204. vid_writer[i].release() # release previous video writer
  205. if vid_cap: # video
  206. fps = vid_cap.get(cv2.CAP_PROP_FPS)
  207. w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  208. h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  209. else: # stream
  210. fps, w, h = 30, im0.shape[1], im0.shape[0]
  211. save_path = str(Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos
  212. vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
  213. vid_writer[i].write(im0)
  214. # Print time (inference-only)
  215. LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms")
  216. f_for_val.writelines(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms\n")
  217. f_for_val.close()
  218. # Print results
  219. t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image
  220. LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {(1, 3, *imgsz)}' % t)
  221. if save_txt or save_img:
  222. s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
  223. LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
  224. if update:
  225. strip_optimizer(weights[0]) # update model (to fix SourceChangeWarning)
  226. def parse_opt():
  227. parser = argparse.ArgumentParser()
  228. parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'weights\\2024-06-13_1438.pt', help='model path or triton URL')
  229. parser.add_argument('--source', type=str, default=ROOT / 'data\\HRSID_YOLO\\test\\images', help='file/dir/URL/glob/screen/0(webcam)')
  230. parser.add_argument('--data', type=str, default=ROOT / 'data\HRSID_YOLO\dataset.yaml', help='(optional) dataset.yaml path')
  231. parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
  232. parser.add_argument('--conf-thres', type=float, default=0.4, help='confidence threshold')
  233. parser.add_argument('--iou-thres', type=float, default=0.5, help='NMS IoU threshold')
  234. parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
  235. parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
  236. parser.add_argument('--view-img', action='store_true', help='show results')
  237. parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
  238. parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
  239. parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')
  240. parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
  241. parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')
  242. parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
  243. parser.add_argument('--augment', action='store_true', help='augmented inference')
  244. parser.add_argument('--visualize', action='store_true', help='visualize features')
  245. parser.add_argument('--update', action='store_true', help='update all models')
  246. parser.add_argument('--project', default=ROOT / 'runs/predict-seg', help='save results to project/name')
  247. parser.add_argument('--name', default='exp', help='save results to project/name')
  248. parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
  249. parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
  250. parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
  251. parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
  252. parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
  253. parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
  254. parser.add_argument('--vid-stride', type=int, default=1, help='video frame-rate stride')
  255. parser.add_argument('--retina-masks', action='store_true', help='whether to plot masks in native resolution')
  256. parser.add_argument('--batch-size', type=int, default=1, help='total batch size for all GPUs, -1 for autobatch')
  257. opt = parser.parse_args()
  258. opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand
  259. print_args(vars(opt))
  260. return opt
  261. def main(opt):
  262. check_requirements(exclude=('tensorboard', 'thop'))
  263. run(**vars(opt))
  264. if __name__ == '__main__':
  265. opt = parse_opt()
  266. main(opt)