Reference for ultralytics/data/loaders.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/data/loaders.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.data.loaders.SourceTypes
dataclass
Class to represent various types of input sources for predictions.
Source code in ultralytics/data/loaders.py
ultralytics.data.loaders.LoadStreams
Stream Loader for various types of video streams.
Suitable for use with yolo predict source='rtsp://example.com/media.mp4'
, supports RTSP, RTMP, HTTP, and TCP streams.
Attributes:
Name | Type | Description |
---|---|---|
sources |
str
|
The source input paths or URLs for the video streams. |
imgsz |
int
|
The image size for processing, defaults to 640. |
vid_stride |
int
|
Video frame-rate stride, defaults to 1. |
buffer |
bool
|
Whether to buffer input streams, defaults to False. |
running |
bool
|
Flag to indicate if the streaming thread is running. |
mode |
str
|
Set to 'stream' indicating real-time capture. |
imgs |
list
|
List of image frames for each stream. |
fps |
list
|
List of FPS for each stream. |
frames |
list
|
List of total frames for each stream. |
threads |
list
|
List of threads for each stream. |
shape |
list
|
List of shapes for each stream. |
caps |
list
|
List of cv2.VideoCapture objects for each stream. |
bs |
int
|
Batch size for processing. |
Methods:
Name | Description |
---|---|
__init__ |
Initialize the stream loader. |
update |
Read stream frames in daemon thread. |
close |
Close stream loader and release resources. |
__iter__ |
Returns an iterator object for the class. |
__next__ |
Returns source paths, transformed, and original images for processing. |
__len__ |
Return the length of the sources object. |
Source code in ultralytics/data/loaders.py
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
__init__(sources='file.streams', imgsz=640, vid_stride=1, buffer=False)
Initialize instance variables and check for consistent input stream shapes.
Source code in ultralytics/data/loaders.py
__iter__()
__len__()
__next__()
Returns source paths, transformed and original images for processing.
Source code in ultralytics/data/loaders.py
close()
Close stream loader and release resources.
Source code in ultralytics/data/loaders.py
update(i, cap, stream)
Read stream i
frames in daemon thread.
Source code in ultralytics/data/loaders.py
ultralytics.data.loaders.LoadScreenshots
YOLOv8 screenshot dataloader.
This class manages the loading of screenshot images for processing with YOLOv8.
Suitable for use with yolo predict source=screen
.
Attributes:
Name | Type | Description |
---|---|---|
source |
str
|
The source input indicating which screen to capture. |
imgsz |
int
|
The image size for processing, defaults to 640. |
screen |
int
|
The screen number to capture. |
left |
int
|
The left coordinate for screen capture area. |
top |
int
|
The top coordinate for screen capture area. |
width |
int
|
The width of the screen capture area. |
height |
int
|
The height of the screen capture area. |
mode |
str
|
Set to 'stream' indicating real-time capture. |
frame |
int
|
Counter for captured frames. |
sct |
mss
|
Screen capture object from |
bs |
int
|
Batch size, set to 1. |
monitor |
dict
|
Monitor configuration details. |
Methods:
Name | Description |
---|---|
__iter__ |
Returns an iterator object. |
__next__ |
Captures the next screenshot and returns it. |
Source code in ultralytics/data/loaders.py
__init__(source, imgsz=640)
Source = [screen_number left top width height] (pixels).
Source code in ultralytics/data/loaders.py
__iter__()
__next__()
mss screen capture: get raw pixels from the screen as np array.
Source code in ultralytics/data/loaders.py
ultralytics.data.loaders.LoadImages
YOLOv8 image/video dataloader.
This class manages the loading and pre-processing of image and video data for YOLOv8. It supports loading from various formats, including single image files, video files, and lists of image and video paths.
Attributes:
Name | Type | Description |
---|---|---|
imgsz |
int
|
Image size, defaults to 640. |
files |
list
|
List of image and video file paths. |
nf |
int
|
Total number of files (images and videos). |
video_flag |
list
|
Flags indicating whether a file is a video (True) or an image (False). |
mode |
str
|
Current mode, 'image' or 'video'. |
vid_stride |
int
|
Stride for video frame-rate, defaults to 1. |
bs |
int
|
Batch size, set to 1 for this class. |
cap |
VideoCapture
|
Video capture object for OpenCV. |
frame |
int
|
Frame counter for video. |
frames |
int
|
Total number of frames in the video. |
count |
int
|
Counter for iteration, initialized at 0 during |
Methods:
Name | Description |
---|---|
_new_video |
Create a new cv2.VideoCapture object for a given video path. |
Source code in ultralytics/data/loaders.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
__init__(path, imgsz=640, vid_stride=1)
Initialize the Dataloader and raise FileNotFoundError if file not found.
Source code in ultralytics/data/loaders.py
__iter__()
__len__()
__next__()
Return next image, path and metadata from dataset.
Source code in ultralytics/data/loaders.py
ultralytics.data.loaders.LoadPilAndNumpy
Load images from PIL and Numpy arrays for batch processing.
This class is designed to manage loading and pre-processing of image data from both PIL and Numpy formats. It performs basic validation and format conversion to ensure that the images are in the required format for downstream processing.
Attributes:
Name | Type | Description |
---|---|---|
paths |
list
|
List of image paths or autogenerated filenames. |
im0 |
list
|
List of images stored as Numpy arrays. |
imgsz |
int
|
Image size, defaults to 640. |
mode |
str
|
Type of data being processed, defaults to 'image'. |
bs |
int
|
Batch size, equivalent to the length of |
count |
int
|
Counter for iteration, initialized at 0 during |
Methods:
Name | Description |
---|---|
_single_check |
Validate and format a single image to a Numpy array. |
Source code in ultralytics/data/loaders.py
__init__(im0, imgsz=640)
Initialize PIL and Numpy Dataloader.
Source code in ultralytics/data/loaders.py
__iter__()
__len__()
__next__()
Returns batch paths, images, processed images, None, ''.
ultralytics.data.loaders.LoadTensor
Load images from torch.Tensor data.
This class manages the loading and pre-processing of image data from PyTorch tensors for further processing.
Attributes:
Name | Type | Description |
---|---|---|
im0 |
Tensor
|
The input tensor containing the image(s). |
bs |
int
|
Batch size, inferred from the shape of |
mode |
str
|
Current mode, set to 'image'. |
paths |
list
|
List of image paths or filenames. |
count |
int
|
Counter for iteration, initialized at 0 during |
Methods:
Name | Description |
---|---|
_single_check |
Validate and possibly modify the input tensor. |
Source code in ultralytics/data/loaders.py
__init__(im0)
Initialize Tensor Dataloader.
Source code in ultralytics/data/loaders.py
__iter__()
__len__()
ultralytics.data.loaders.autocast_list(source)
Merges a list of source of different types into a list of numpy arrays or PIL images.
Source code in ultralytics/data/loaders.py
ultralytics.data.loaders.get_best_youtube_url(url, use_pafy=True)
Retrieves the URL of the best quality MP4 video stream from a given YouTube video.
This function uses the pafy or yt_dlp library to extract the video info from YouTube. It then finds the highest quality MP4 format that has video codec but no audio codec, and returns the URL of this video stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
The URL of the YouTube video. |
required |
use_pafy |
bool
|
Use the pafy package, default=True, otherwise use yt_dlp package. |
True
|
Returns:
Type | Description |
---|---|
str
|
The URL of the best quality MP4 video stream, or None if no suitable stream is found. |