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
SourceTypes(
stream: bool = False,
screenshot: bool = False,
from_img: bool = False,
tensor: bool = False,
)
Class to represent various types of input sources for predictions.
ultralytics.data.loaders.LoadStreams
Stream Loader for various types of video streams, Supports RTSP, RTMP, HTTP, and TCP streams.
Attributes:
Name | Type | Description |
---|---|---|
sources |
str
|
The source input paths or URLs for the video streams. |
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 |
---|---|
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
__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
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. |
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
__iter__
__next__
Screen capture with 'mss' to get raw pixels from the screen as np array.
Source code in ultralytics/data/loaders.py
ultralytics.data.loaders.LoadImagesAndVideos
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 |
---|---|---|
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
__iter__
__len__
__next__
Returns the next batch of images or video frames along with their paths and metadata.
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. |
mode |
str
|
Type of data being processed, defaults to 'image'. |
bs |
int
|
Batch size, equivalent to the length of |
Methods:
Name | Description |
---|---|
_single_check |
Validate and format a single image to a Numpy array. |
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
__iter__
__len__
ultralytics.data.loaders.autocast_list
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
Retrieves the URL of the best quality MP4 video stream from a given YouTube video.
This function uses the specified method to extract the video info from YouTube. It supports the following methods: - "pytube": Uses the pytube library to fetch the video streams. - "pafy": Uses the pafy library to fetch the video streams. - "yt-dlp": Uses the yt-dlp library to fetch the video streams.
The function then finds the highest quality MP4 format that has a 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 |
method |
str
|
The method to use for extracting video info. Default is "pytube". Other options are "pafy" and "yt-dlp". |
'pytube'
|
Returns:
Type | Description |
---|---|
str
|
The URL of the best quality MP4 video stream, or None if no suitable stream is found. |