Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#20059 Fix YOLOv8-ONNXRuntime example links

Merged
Ghost merged 1 commits into Ultralytics:main from ultralytics:fix-example-links
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  1. # Ultralytics ๐Ÿš€ AGPL-3.0 License - https://ultralytics.com/license
  2. import argparse
  3. from pathlib import Path
  4. import cv2
  5. from sahi import AutoDetectionModel
  6. from sahi.predict import get_sliced_prediction
  7. from sahi.utils.ultralytics import download_yolo11n_model
  8. from ultralytics.utils.files import increment_path
  9. from ultralytics.utils.plotting import Annotator, colors
  10. class SAHIInference:
  11. """
  12. Runs Ultralytics YOLO11 and SAHI for object detection on video with options to view, save, and track results.
  13. This class integrates SAHI (Slicing Aided Hyper Inference) with YOLO11 models to perform efficient object detection
  14. on large images by slicing them into smaller pieces, running inference on each slice, and then merging the results.
  15. Attributes:
  16. detection_model (AutoDetectionModel): The loaded YOLO11 model wrapped with SAHI functionality.
  17. Methods:
  18. load_model: Loads a YOLO11 model with specified weights.
  19. inference: Runs object detection on a video using the loaded model.
  20. parse_opt: Parses command line arguments for the inference process.
  21. """
  22. def __init__(self):
  23. """Initializes the SAHIInference class for performing sliced inference using SAHI with YOLO11 models."""
  24. self.detection_model = None
  25. def load_model(self, weights: str) -> None:
  26. """
  27. Load a YOLO11 model with specified weights for object detection using SAHI.
  28. Args:
  29. weights (str): Path to the model weights file.
  30. """
  31. yolo11_model_path = f"models/{weights}"
  32. download_yolo11n_model(yolo11_model_path) # Download model if not present
  33. self.detection_model = AutoDetectionModel.from_pretrained(
  34. model_type="ultralytics", model_path=yolo11_model_path, device="cpu"
  35. )
  36. def inference(
  37. self,
  38. weights: str = "yolo11n.pt",
  39. source: str = "test.mp4",
  40. view_img: bool = False,
  41. save_img: bool = False,
  42. exist_ok: bool = False,
  43. ) -> None:
  44. """
  45. Run object detection on a video using YOLO11 and SAHI.
  46. The function processes each frame of the video, applies sliced inference using SAHI,
  47. and optionally displays and/or saves the results with bounding boxes and labels.
  48. Args:
  49. weights (str): Model weights path.
  50. source (str): Video file path.
  51. view_img (bool): Whether to display results in a window.
  52. save_img (bool): Whether to save results to a video file.
  53. exist_ok (bool): Whether to overwrite existing output files.
  54. """
  55. # Video setup
  56. cap = cv2.VideoCapture(source)
  57. assert cap.isOpened(), "Error reading video file"
  58. frame_width, frame_height = int(cap.get(3)), int(cap.get(4))
  59. # Output setup
  60. save_dir = increment_path(Path("ultralytics_results_with_sahi") / "exp", exist_ok)
  61. save_dir.mkdir(parents=True, exist_ok=True)
  62. video_writer = cv2.VideoWriter(
  63. str(save_dir / f"{Path(source).stem}.avi"),
  64. cv2.VideoWriter_fourcc(*"MJPG"),
  65. int(cap.get(5)),
  66. (frame_width, frame_height),
  67. )
  68. # Load model
  69. self.load_model(weights)
  70. while cap.isOpened():
  71. success, frame = cap.read()
  72. if not success:
  73. break
  74. annotator = Annotator(frame) # Initialize annotator for plotting detection results
  75. # Perform sliced prediction using SAHI
  76. results = get_sliced_prediction(
  77. frame[..., ::-1], # Convert BGR to RGB
  78. self.detection_model,
  79. slice_height=512,
  80. slice_width=512,
  81. )
  82. # Extract detection data from results
  83. detection_data = [
  84. (det.category.name, det.category.id, (det.bbox.minx, det.bbox.miny, det.bbox.maxx, det.bbox.maxy))
  85. for det in results.object_prediction_list
  86. ]
  87. # Annotate frame with detection results
  88. for det in detection_data:
  89. annotator.box_label(det[2], label=str(det[0]), color=colors(int(det[1]), True))
  90. # Display results if requested
  91. if view_img:
  92. cv2.imshow(Path(source).stem, frame)
  93. # Save results if requested
  94. if save_img:
  95. video_writer.write(frame)
  96. # Break loop if 'q' is pressed
  97. if cv2.waitKey(1) & 0xFF == ord("q"):
  98. break
  99. # Clean up resources
  100. video_writer.release()
  101. cap.release()
  102. cv2.destroyAllWindows()
  103. def parse_opt(self) -> argparse.Namespace:
  104. """
  105. Parse command line arguments for the inference process.
  106. Returns:
  107. (argparse.Namespace): Parsed command line arguments.
  108. """
  109. parser = argparse.ArgumentParser()
  110. parser.add_argument("--weights", type=str, default="yolo11n.pt", help="initial weights path")
  111. parser.add_argument("--source", type=str, required=True, help="video file path")
  112. parser.add_argument("--view-img", action="store_true", help="show results")
  113. parser.add_argument("--save-img", action="store_true", help="save results")
  114. parser.add_argument("--exist-ok", action="store_true", help="existing project/name ok, do not increment")
  115. return parser.parse_args()
  116. if __name__ == "__main__":
  117. inference = SAHIInference()
  118. inference.inference(**vars(inference.parse_opt()))
Discard
Tip!

Press p or to see the previous file or, n or to see the next file