dgenerate-ultralytics-headless 8.3.134__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dgenerate_ultralytics_headless-8.3.134.dist-info/METADATA +400 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/RECORD +272 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/WHEEL +5 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/entry_points.txt +3 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/licenses/LICENSE +661 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/top_level.txt +1 -0
- tests/__init__.py +22 -0
- tests/conftest.py +83 -0
- tests/test_cli.py +138 -0
- tests/test_cuda.py +215 -0
- tests/test_engine.py +131 -0
- tests/test_exports.py +236 -0
- tests/test_integrations.py +154 -0
- tests/test_python.py +694 -0
- tests/test_solutions.py +187 -0
- ultralytics/__init__.py +30 -0
- ultralytics/assets/bus.jpg +0 -0
- ultralytics/assets/zidane.jpg +0 -0
- ultralytics/cfg/__init__.py +1023 -0
- ultralytics/cfg/datasets/Argoverse.yaml +77 -0
- ultralytics/cfg/datasets/DOTAv1.5.yaml +37 -0
- ultralytics/cfg/datasets/DOTAv1.yaml +36 -0
- ultralytics/cfg/datasets/GlobalWheat2020.yaml +68 -0
- ultralytics/cfg/datasets/HomeObjects-3K.yaml +33 -0
- ultralytics/cfg/datasets/ImageNet.yaml +2025 -0
- ultralytics/cfg/datasets/Objects365.yaml +443 -0
- ultralytics/cfg/datasets/SKU-110K.yaml +58 -0
- ultralytics/cfg/datasets/VOC.yaml +106 -0
- ultralytics/cfg/datasets/VisDrone.yaml +77 -0
- ultralytics/cfg/datasets/african-wildlife.yaml +25 -0
- ultralytics/cfg/datasets/brain-tumor.yaml +23 -0
- ultralytics/cfg/datasets/carparts-seg.yaml +44 -0
- ultralytics/cfg/datasets/coco-pose.yaml +42 -0
- ultralytics/cfg/datasets/coco.yaml +118 -0
- ultralytics/cfg/datasets/coco128-seg.yaml +101 -0
- ultralytics/cfg/datasets/coco128.yaml +101 -0
- ultralytics/cfg/datasets/coco8-multispectral.yaml +104 -0
- ultralytics/cfg/datasets/coco8-pose.yaml +26 -0
- ultralytics/cfg/datasets/coco8-seg.yaml +101 -0
- ultralytics/cfg/datasets/coco8.yaml +101 -0
- ultralytics/cfg/datasets/crack-seg.yaml +22 -0
- ultralytics/cfg/datasets/dog-pose.yaml +24 -0
- ultralytics/cfg/datasets/dota8-multispectral.yaml +38 -0
- ultralytics/cfg/datasets/dota8.yaml +35 -0
- ultralytics/cfg/datasets/hand-keypoints.yaml +26 -0
- ultralytics/cfg/datasets/lvis.yaml +1240 -0
- ultralytics/cfg/datasets/medical-pills.yaml +22 -0
- ultralytics/cfg/datasets/open-images-v7.yaml +666 -0
- ultralytics/cfg/datasets/package-seg.yaml +22 -0
- ultralytics/cfg/datasets/signature.yaml +21 -0
- ultralytics/cfg/datasets/tiger-pose.yaml +25 -0
- ultralytics/cfg/datasets/xView.yaml +155 -0
- ultralytics/cfg/default.yaml +127 -0
- ultralytics/cfg/models/11/yolo11-cls-resnet18.yaml +17 -0
- ultralytics/cfg/models/11/yolo11-cls.yaml +33 -0
- ultralytics/cfg/models/11/yolo11-obb.yaml +50 -0
- ultralytics/cfg/models/11/yolo11-pose.yaml +51 -0
- ultralytics/cfg/models/11/yolo11-seg.yaml +50 -0
- ultralytics/cfg/models/11/yolo11.yaml +50 -0
- ultralytics/cfg/models/11/yoloe-11-seg.yaml +48 -0
- ultralytics/cfg/models/11/yoloe-11.yaml +48 -0
- ultralytics/cfg/models/12/yolo12-cls.yaml +32 -0
- ultralytics/cfg/models/12/yolo12-obb.yaml +48 -0
- ultralytics/cfg/models/12/yolo12-pose.yaml +49 -0
- ultralytics/cfg/models/12/yolo12-seg.yaml +48 -0
- ultralytics/cfg/models/12/yolo12.yaml +48 -0
- ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +53 -0
- ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +45 -0
- ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +45 -0
- ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +57 -0
- ultralytics/cfg/models/v10/yolov10b.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10l.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10m.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10n.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10s.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10x.yaml +45 -0
- ultralytics/cfg/models/v3/yolov3-spp.yaml +49 -0
- ultralytics/cfg/models/v3/yolov3-tiny.yaml +40 -0
- ultralytics/cfg/models/v3/yolov3.yaml +49 -0
- ultralytics/cfg/models/v5/yolov5-p6.yaml +62 -0
- ultralytics/cfg/models/v5/yolov5.yaml +51 -0
- ultralytics/cfg/models/v6/yolov6.yaml +56 -0
- ultralytics/cfg/models/v8/yoloe-v8-seg.yaml +45 -0
- ultralytics/cfg/models/v8/yoloe-v8.yaml +45 -0
- ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +28 -0
- ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +28 -0
- ultralytics/cfg/models/v8/yolov8-cls.yaml +32 -0
- ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +58 -0
- ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +60 -0
- ultralytics/cfg/models/v8/yolov8-ghost.yaml +50 -0
- ultralytics/cfg/models/v8/yolov8-obb.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-p2.yaml +57 -0
- ultralytics/cfg/models/v8/yolov8-p6.yaml +59 -0
- ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +60 -0
- ultralytics/cfg/models/v8/yolov8-pose.yaml +50 -0
- ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +59 -0
- ultralytics/cfg/models/v8/yolov8-seg.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-world.yaml +51 -0
- ultralytics/cfg/models/v8/yolov8-worldv2.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8.yaml +49 -0
- ultralytics/cfg/models/v9/yolov9c-seg.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9c.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9e-seg.yaml +64 -0
- ultralytics/cfg/models/v9/yolov9e.yaml +64 -0
- ultralytics/cfg/models/v9/yolov9m.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9s.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9t.yaml +41 -0
- ultralytics/cfg/trackers/botsort.yaml +22 -0
- ultralytics/cfg/trackers/bytetrack.yaml +14 -0
- ultralytics/data/__init__.py +26 -0
- ultralytics/data/annotator.py +66 -0
- ultralytics/data/augment.py +2945 -0
- ultralytics/data/base.py +438 -0
- ultralytics/data/build.py +258 -0
- ultralytics/data/converter.py +754 -0
- ultralytics/data/dataset.py +834 -0
- ultralytics/data/loaders.py +676 -0
- ultralytics/data/scripts/download_weights.sh +18 -0
- ultralytics/data/scripts/get_coco.sh +61 -0
- ultralytics/data/scripts/get_coco128.sh +18 -0
- ultralytics/data/scripts/get_imagenet.sh +52 -0
- ultralytics/data/split.py +125 -0
- ultralytics/data/split_dota.py +325 -0
- ultralytics/data/utils.py +777 -0
- ultralytics/engine/__init__.py +1 -0
- ultralytics/engine/exporter.py +1519 -0
- ultralytics/engine/model.py +1156 -0
- ultralytics/engine/predictor.py +502 -0
- ultralytics/engine/results.py +1840 -0
- ultralytics/engine/trainer.py +853 -0
- ultralytics/engine/tuner.py +243 -0
- ultralytics/engine/validator.py +377 -0
- ultralytics/hub/__init__.py +168 -0
- ultralytics/hub/auth.py +137 -0
- ultralytics/hub/google/__init__.py +176 -0
- ultralytics/hub/session.py +446 -0
- ultralytics/hub/utils.py +248 -0
- ultralytics/models/__init__.py +9 -0
- ultralytics/models/fastsam/__init__.py +7 -0
- ultralytics/models/fastsam/model.py +61 -0
- ultralytics/models/fastsam/predict.py +181 -0
- ultralytics/models/fastsam/utils.py +24 -0
- ultralytics/models/fastsam/val.py +40 -0
- ultralytics/models/nas/__init__.py +7 -0
- ultralytics/models/nas/model.py +102 -0
- ultralytics/models/nas/predict.py +58 -0
- ultralytics/models/nas/val.py +39 -0
- ultralytics/models/rtdetr/__init__.py +7 -0
- ultralytics/models/rtdetr/model.py +63 -0
- ultralytics/models/rtdetr/predict.py +84 -0
- ultralytics/models/rtdetr/train.py +85 -0
- ultralytics/models/rtdetr/val.py +191 -0
- ultralytics/models/sam/__init__.py +6 -0
- ultralytics/models/sam/amg.py +260 -0
- ultralytics/models/sam/build.py +358 -0
- ultralytics/models/sam/model.py +170 -0
- ultralytics/models/sam/modules/__init__.py +1 -0
- ultralytics/models/sam/modules/blocks.py +1129 -0
- ultralytics/models/sam/modules/decoders.py +515 -0
- ultralytics/models/sam/modules/encoders.py +854 -0
- ultralytics/models/sam/modules/memory_attention.py +299 -0
- ultralytics/models/sam/modules/sam.py +1006 -0
- ultralytics/models/sam/modules/tiny_encoder.py +1002 -0
- ultralytics/models/sam/modules/transformer.py +351 -0
- ultralytics/models/sam/modules/utils.py +394 -0
- ultralytics/models/sam/predict.py +1605 -0
- ultralytics/models/utils/__init__.py +1 -0
- ultralytics/models/utils/loss.py +455 -0
- ultralytics/models/utils/ops.py +268 -0
- ultralytics/models/yolo/__init__.py +7 -0
- ultralytics/models/yolo/classify/__init__.py +7 -0
- ultralytics/models/yolo/classify/predict.py +88 -0
- ultralytics/models/yolo/classify/train.py +233 -0
- ultralytics/models/yolo/classify/val.py +215 -0
- ultralytics/models/yolo/detect/__init__.py +7 -0
- ultralytics/models/yolo/detect/predict.py +124 -0
- ultralytics/models/yolo/detect/train.py +217 -0
- ultralytics/models/yolo/detect/val.py +451 -0
- ultralytics/models/yolo/model.py +354 -0
- ultralytics/models/yolo/obb/__init__.py +7 -0
- ultralytics/models/yolo/obb/predict.py +66 -0
- ultralytics/models/yolo/obb/train.py +81 -0
- ultralytics/models/yolo/obb/val.py +283 -0
- ultralytics/models/yolo/pose/__init__.py +7 -0
- ultralytics/models/yolo/pose/predict.py +79 -0
- ultralytics/models/yolo/pose/train.py +154 -0
- ultralytics/models/yolo/pose/val.py +394 -0
- ultralytics/models/yolo/segment/__init__.py +7 -0
- ultralytics/models/yolo/segment/predict.py +113 -0
- ultralytics/models/yolo/segment/train.py +123 -0
- ultralytics/models/yolo/segment/val.py +428 -0
- ultralytics/models/yolo/world/__init__.py +5 -0
- ultralytics/models/yolo/world/train.py +119 -0
- ultralytics/models/yolo/world/train_world.py +176 -0
- ultralytics/models/yolo/yoloe/__init__.py +22 -0
- ultralytics/models/yolo/yoloe/predict.py +169 -0
- ultralytics/models/yolo/yoloe/train.py +298 -0
- ultralytics/models/yolo/yoloe/train_seg.py +124 -0
- ultralytics/models/yolo/yoloe/val.py +191 -0
- ultralytics/nn/__init__.py +29 -0
- ultralytics/nn/autobackend.py +842 -0
- ultralytics/nn/modules/__init__.py +182 -0
- ultralytics/nn/modules/activation.py +53 -0
- ultralytics/nn/modules/block.py +1966 -0
- ultralytics/nn/modules/conv.py +712 -0
- ultralytics/nn/modules/head.py +880 -0
- ultralytics/nn/modules/transformer.py +713 -0
- ultralytics/nn/modules/utils.py +164 -0
- ultralytics/nn/tasks.py +1627 -0
- ultralytics/nn/text_model.py +351 -0
- ultralytics/solutions/__init__.py +41 -0
- ultralytics/solutions/ai_gym.py +116 -0
- ultralytics/solutions/analytics.py +252 -0
- ultralytics/solutions/config.py +106 -0
- ultralytics/solutions/distance_calculation.py +124 -0
- ultralytics/solutions/heatmap.py +127 -0
- ultralytics/solutions/instance_segmentation.py +84 -0
- ultralytics/solutions/object_blurrer.py +90 -0
- ultralytics/solutions/object_counter.py +195 -0
- ultralytics/solutions/object_cropper.py +84 -0
- ultralytics/solutions/parking_management.py +273 -0
- ultralytics/solutions/queue_management.py +93 -0
- ultralytics/solutions/region_counter.py +120 -0
- ultralytics/solutions/security_alarm.py +154 -0
- ultralytics/solutions/similarity_search.py +172 -0
- ultralytics/solutions/solutions.py +724 -0
- ultralytics/solutions/speed_estimation.py +110 -0
- ultralytics/solutions/streamlit_inference.py +196 -0
- ultralytics/solutions/templates/similarity-search.html +160 -0
- ultralytics/solutions/trackzone.py +88 -0
- ultralytics/solutions/vision_eye.py +68 -0
- ultralytics/trackers/__init__.py +7 -0
- ultralytics/trackers/basetrack.py +124 -0
- ultralytics/trackers/bot_sort.py +260 -0
- ultralytics/trackers/byte_tracker.py +480 -0
- ultralytics/trackers/track.py +125 -0
- ultralytics/trackers/utils/__init__.py +1 -0
- ultralytics/trackers/utils/gmc.py +376 -0
- ultralytics/trackers/utils/kalman_filter.py +493 -0
- ultralytics/trackers/utils/matching.py +157 -0
- ultralytics/utils/__init__.py +1435 -0
- ultralytics/utils/autobatch.py +106 -0
- ultralytics/utils/autodevice.py +174 -0
- ultralytics/utils/benchmarks.py +695 -0
- ultralytics/utils/callbacks/__init__.py +5 -0
- ultralytics/utils/callbacks/base.py +234 -0
- ultralytics/utils/callbacks/clearml.py +153 -0
- ultralytics/utils/callbacks/comet.py +552 -0
- ultralytics/utils/callbacks/dvc.py +205 -0
- ultralytics/utils/callbacks/hub.py +108 -0
- ultralytics/utils/callbacks/mlflow.py +138 -0
- ultralytics/utils/callbacks/neptune.py +140 -0
- ultralytics/utils/callbacks/raytune.py +43 -0
- ultralytics/utils/callbacks/tensorboard.py +132 -0
- ultralytics/utils/callbacks/wb.py +185 -0
- ultralytics/utils/checks.py +897 -0
- ultralytics/utils/dist.py +119 -0
- ultralytics/utils/downloads.py +499 -0
- ultralytics/utils/errors.py +43 -0
- ultralytics/utils/export.py +219 -0
- ultralytics/utils/files.py +221 -0
- ultralytics/utils/instance.py +499 -0
- ultralytics/utils/loss.py +813 -0
- ultralytics/utils/metrics.py +1356 -0
- ultralytics/utils/ops.py +885 -0
- ultralytics/utils/patches.py +143 -0
- ultralytics/utils/plotting.py +1011 -0
- ultralytics/utils/tal.py +416 -0
- ultralytics/utils/torch_utils.py +990 -0
- ultralytics/utils/triton.py +116 -0
- ultralytics/utils/tuner.py +159 -0
@@ -0,0 +1,676 @@
|
|
1
|
+
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
2
|
+
|
3
|
+
import glob
|
4
|
+
import math
|
5
|
+
import os
|
6
|
+
import time
|
7
|
+
import urllib
|
8
|
+
from dataclasses import dataclass
|
9
|
+
from pathlib import Path
|
10
|
+
from threading import Thread
|
11
|
+
|
12
|
+
import cv2
|
13
|
+
import numpy as np
|
14
|
+
import torch
|
15
|
+
from PIL import Image
|
16
|
+
|
17
|
+
from ultralytics.data.utils import FORMATS_HELP_MSG, IMG_FORMATS, VID_FORMATS
|
18
|
+
from ultralytics.utils import IS_COLAB, IS_KAGGLE, LOGGER, ops
|
19
|
+
from ultralytics.utils.checks import check_requirements
|
20
|
+
from ultralytics.utils.patches import imread
|
21
|
+
|
22
|
+
|
23
|
+
@dataclass
|
24
|
+
class SourceTypes:
|
25
|
+
"""
|
26
|
+
Class to represent various types of input sources for predictions.
|
27
|
+
|
28
|
+
This class uses dataclass to define boolean flags for different types of input sources that can be used for
|
29
|
+
making predictions with YOLO models.
|
30
|
+
|
31
|
+
Attributes:
|
32
|
+
stream (bool): Flag indicating if the input source is a video stream.
|
33
|
+
screenshot (bool): Flag indicating if the input source is a screenshot.
|
34
|
+
from_img (bool): Flag indicating if the input source is an image file.
|
35
|
+
tensor (bool): Flag indicating if the input source is a tensor.
|
36
|
+
|
37
|
+
Examples:
|
38
|
+
>>> source_types = SourceTypes(stream=True, screenshot=False, from_img=False)
|
39
|
+
>>> print(source_types.stream)
|
40
|
+
True
|
41
|
+
>>> print(source_types.from_img)
|
42
|
+
False
|
43
|
+
"""
|
44
|
+
|
45
|
+
stream: bool = False
|
46
|
+
screenshot: bool = False
|
47
|
+
from_img: bool = False
|
48
|
+
tensor: bool = False
|
49
|
+
|
50
|
+
|
51
|
+
class LoadStreams:
|
52
|
+
"""
|
53
|
+
Stream Loader for various types of video streams.
|
54
|
+
|
55
|
+
Supports RTSP, RTMP, HTTP, and TCP streams. This class handles the loading and processing of multiple video
|
56
|
+
streams simultaneously, making it suitable for real-time video analysis tasks.
|
57
|
+
|
58
|
+
Attributes:
|
59
|
+
sources (List[str]): The source input paths or URLs for the video streams.
|
60
|
+
vid_stride (int): Video frame-rate stride.
|
61
|
+
buffer (bool): Whether to buffer input streams.
|
62
|
+
running (bool): Flag to indicate if the streaming thread is running.
|
63
|
+
mode (str): Set to 'stream' indicating real-time capture.
|
64
|
+
imgs (List[List[np.ndarray]]): List of image frames for each stream.
|
65
|
+
fps (List[float]): List of FPS for each stream.
|
66
|
+
frames (List[int]): List of total frames for each stream.
|
67
|
+
threads (List[Thread]): List of threads for each stream.
|
68
|
+
shape (List[Tuple[int, int, int]]): List of shapes for each stream.
|
69
|
+
caps (List[cv2.VideoCapture]): List of cv2.VideoCapture objects for each stream.
|
70
|
+
bs (int): Batch size for processing.
|
71
|
+
cv2_flag (int): OpenCV flag for image reading (grayscale or RGB).
|
72
|
+
|
73
|
+
Methods:
|
74
|
+
update: Read stream frames in daemon thread.
|
75
|
+
close: Close stream loader and release resources.
|
76
|
+
__iter__: Returns an iterator object for the class.
|
77
|
+
__next__: Returns source paths, transformed, and original images for processing.
|
78
|
+
__len__: Return the length of the sources object.
|
79
|
+
|
80
|
+
Examples:
|
81
|
+
>>> stream_loader = LoadStreams("rtsp://example.com/stream1.mp4")
|
82
|
+
>>> for sources, imgs, _ in stream_loader:
|
83
|
+
... # Process the images
|
84
|
+
... pass
|
85
|
+
>>> stream_loader.close()
|
86
|
+
|
87
|
+
Notes:
|
88
|
+
- The class uses threading to efficiently load frames from multiple streams simultaneously.
|
89
|
+
- It automatically handles YouTube links, converting them to the best available stream URL.
|
90
|
+
- The class implements a buffer system to manage frame storage and retrieval.
|
91
|
+
"""
|
92
|
+
|
93
|
+
def __init__(self, sources="file.streams", vid_stride=1, buffer=False, channels=3):
|
94
|
+
"""Initialize stream loader for multiple video sources, supporting various stream types."""
|
95
|
+
torch.backends.cudnn.benchmark = True # faster for fixed-size inference
|
96
|
+
self.buffer = buffer # buffer input streams
|
97
|
+
self.running = True # running flag for Thread
|
98
|
+
self.mode = "stream"
|
99
|
+
self.vid_stride = vid_stride # video frame-rate stride
|
100
|
+
self.cv2_flag = cv2.IMREAD_GRAYSCALE if channels == 1 else cv2.IMREAD_COLOR # grayscale or RGB
|
101
|
+
|
102
|
+
sources = Path(sources).read_text().rsplit() if os.path.isfile(sources) else [sources]
|
103
|
+
n = len(sources)
|
104
|
+
self.bs = n
|
105
|
+
self.fps = [0] * n # frames per second
|
106
|
+
self.frames = [0] * n
|
107
|
+
self.threads = [None] * n
|
108
|
+
self.caps = [None] * n # video capture objects
|
109
|
+
self.imgs = [[] for _ in range(n)] # images
|
110
|
+
self.shape = [[] for _ in range(n)] # image shapes
|
111
|
+
self.sources = [ops.clean_str(x).replace(os.sep, "_") for x in sources] # clean source names for later
|
112
|
+
for i, s in enumerate(sources): # index, source
|
113
|
+
# Start thread to read frames from video stream
|
114
|
+
st = f"{i + 1}/{n}: {s}... "
|
115
|
+
if urllib.parse.urlparse(s).hostname in {"www.youtube.com", "youtube.com", "youtu.be"}: # YouTube video
|
116
|
+
# YouTube format i.e. 'https://www.youtube.com/watch?v=Jsn8D3aC840' or 'https://youtu.be/Jsn8D3aC840'
|
117
|
+
s = get_best_youtube_url(s)
|
118
|
+
s = eval(s) if s.isnumeric() else s # i.e. s = '0' local webcam
|
119
|
+
if s == 0 and (IS_COLAB or IS_KAGGLE):
|
120
|
+
raise NotImplementedError(
|
121
|
+
"'source=0' webcam not supported in Colab and Kaggle notebooks. "
|
122
|
+
"Try running 'source=0' in a local environment."
|
123
|
+
)
|
124
|
+
self.caps[i] = cv2.VideoCapture(s) # store video capture object
|
125
|
+
if not self.caps[i].isOpened():
|
126
|
+
raise ConnectionError(f"{st}Failed to open {s}")
|
127
|
+
w = int(self.caps[i].get(cv2.CAP_PROP_FRAME_WIDTH))
|
128
|
+
h = int(self.caps[i].get(cv2.CAP_PROP_FRAME_HEIGHT))
|
129
|
+
fps = self.caps[i].get(cv2.CAP_PROP_FPS) # warning: may return 0 or nan
|
130
|
+
self.frames[i] = max(int(self.caps[i].get(cv2.CAP_PROP_FRAME_COUNT)), 0) or float(
|
131
|
+
"inf"
|
132
|
+
) # infinite stream fallback
|
133
|
+
self.fps[i] = max((fps if math.isfinite(fps) else 0) % 100, 0) or 30 # 30 FPS fallback
|
134
|
+
|
135
|
+
success, im = self.caps[i].read() # guarantee first frame
|
136
|
+
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)[..., None] if self.cv2_flag == cv2.IMREAD_GRAYSCALE else im
|
137
|
+
if not success or im is None:
|
138
|
+
raise ConnectionError(f"{st}Failed to read images from {s}")
|
139
|
+
self.imgs[i].append(im)
|
140
|
+
self.shape[i] = im.shape
|
141
|
+
self.threads[i] = Thread(target=self.update, args=([i, self.caps[i], s]), daemon=True)
|
142
|
+
LOGGER.info(f"{st}Success ✅ ({self.frames[i]} frames of shape {w}x{h} at {self.fps[i]:.2f} FPS)")
|
143
|
+
self.threads[i].start()
|
144
|
+
LOGGER.info("") # newline
|
145
|
+
|
146
|
+
def update(self, i, cap, stream):
|
147
|
+
"""Read stream frames in daemon thread and update image buffer."""
|
148
|
+
n, f = 0, self.frames[i] # frame number, frame array
|
149
|
+
while self.running and cap.isOpened() and n < (f - 1):
|
150
|
+
if len(self.imgs[i]) < 30: # keep a <=30-image buffer
|
151
|
+
n += 1
|
152
|
+
cap.grab() # .read() = .grab() followed by .retrieve()
|
153
|
+
if n % self.vid_stride == 0:
|
154
|
+
success, im = cap.retrieve()
|
155
|
+
im = (
|
156
|
+
cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)[..., None] if self.cv2_flag == cv2.IMREAD_GRAYSCALE else im
|
157
|
+
)
|
158
|
+
if not success:
|
159
|
+
im = np.zeros(self.shape[i], dtype=np.uint8)
|
160
|
+
LOGGER.warning("Video stream unresponsive, please check your IP camera connection.")
|
161
|
+
cap.open(stream) # re-open stream if signal was lost
|
162
|
+
if self.buffer:
|
163
|
+
self.imgs[i].append(im)
|
164
|
+
else:
|
165
|
+
self.imgs[i] = [im]
|
166
|
+
else:
|
167
|
+
time.sleep(0.01) # wait until the buffer is empty
|
168
|
+
|
169
|
+
def close(self):
|
170
|
+
"""Terminates stream loader, stops threads, and releases video capture resources."""
|
171
|
+
self.running = False # stop flag for Thread
|
172
|
+
for thread in self.threads:
|
173
|
+
if thread.is_alive():
|
174
|
+
thread.join(timeout=5) # Add timeout
|
175
|
+
for cap in self.caps: # Iterate through the stored VideoCapture objects
|
176
|
+
try:
|
177
|
+
cap.release() # release video capture
|
178
|
+
except Exception as e:
|
179
|
+
LOGGER.warning(f"Could not release VideoCapture object: {e}")
|
180
|
+
cv2.destroyAllWindows()
|
181
|
+
|
182
|
+
def __iter__(self):
|
183
|
+
"""Iterates through YOLO image feed and re-opens unresponsive streams."""
|
184
|
+
self.count = -1
|
185
|
+
return self
|
186
|
+
|
187
|
+
def __next__(self):
|
188
|
+
"""Returns the next batch of frames from multiple video streams for processing."""
|
189
|
+
self.count += 1
|
190
|
+
|
191
|
+
images = []
|
192
|
+
for i, x in enumerate(self.imgs):
|
193
|
+
# Wait until a frame is available in each buffer
|
194
|
+
while not x:
|
195
|
+
if not self.threads[i].is_alive() or cv2.waitKey(1) == ord("q"): # q to quit
|
196
|
+
self.close()
|
197
|
+
raise StopIteration
|
198
|
+
time.sleep(1 / min(self.fps))
|
199
|
+
x = self.imgs[i]
|
200
|
+
if not x:
|
201
|
+
LOGGER.warning(f"Waiting for stream {i}")
|
202
|
+
|
203
|
+
# Get and remove the first frame from imgs buffer
|
204
|
+
if self.buffer:
|
205
|
+
images.append(x.pop(0))
|
206
|
+
|
207
|
+
# Get the last frame, and clear the rest from the imgs buffer
|
208
|
+
else:
|
209
|
+
images.append(x.pop(-1) if x else np.zeros(self.shape[i], dtype=np.uint8))
|
210
|
+
x.clear()
|
211
|
+
|
212
|
+
return self.sources, images, [""] * self.bs
|
213
|
+
|
214
|
+
def __len__(self):
|
215
|
+
"""Return the number of video streams in the LoadStreams object."""
|
216
|
+
return self.bs # 1E12 frames = 32 streams at 30 FPS for 30 years
|
217
|
+
|
218
|
+
|
219
|
+
class LoadScreenshots:
|
220
|
+
"""
|
221
|
+
Ultralytics screenshot dataloader for capturing and processing screen images.
|
222
|
+
|
223
|
+
This class manages the loading of screenshot images for processing with YOLO. It is suitable for use with
|
224
|
+
`yolo predict source=screen`.
|
225
|
+
|
226
|
+
Attributes:
|
227
|
+
source (str): The source input indicating which screen to capture.
|
228
|
+
screen (int): The screen number to capture.
|
229
|
+
left (int): The left coordinate for screen capture area.
|
230
|
+
top (int): The top coordinate for screen capture area.
|
231
|
+
width (int): The width of the screen capture area.
|
232
|
+
height (int): The height of the screen capture area.
|
233
|
+
mode (str): Set to 'stream' indicating real-time capture.
|
234
|
+
frame (int): Counter for captured frames.
|
235
|
+
sct (mss.mss): Screen capture object from `mss` library.
|
236
|
+
bs (int): Batch size, set to 1.
|
237
|
+
fps (int): Frames per second, set to 30.
|
238
|
+
monitor (Dict[str, int]): Monitor configuration details.
|
239
|
+
cv2_flag (int): OpenCV flag for image reading (grayscale or RGB).
|
240
|
+
|
241
|
+
Methods:
|
242
|
+
__iter__: Returns an iterator object.
|
243
|
+
__next__: Captures the next screenshot and returns it.
|
244
|
+
|
245
|
+
Examples:
|
246
|
+
>>> loader = LoadScreenshots("0 100 100 640 480") # screen 0, top-left (100,100), 640x480
|
247
|
+
>>> for source, im, im0s, vid_cap, s in loader:
|
248
|
+
... print(f"Captured frame: {im.shape}")
|
249
|
+
"""
|
250
|
+
|
251
|
+
def __init__(self, source, channels=3):
|
252
|
+
"""Initialize screenshot capture with specified screen and region parameters."""
|
253
|
+
check_requirements("mss")
|
254
|
+
import mss # noqa
|
255
|
+
|
256
|
+
source, *params = source.split()
|
257
|
+
self.screen, left, top, width, height = 0, None, None, None, None # default to full screen 0
|
258
|
+
if len(params) == 1:
|
259
|
+
self.screen = int(params[0])
|
260
|
+
elif len(params) == 4:
|
261
|
+
left, top, width, height = (int(x) for x in params)
|
262
|
+
elif len(params) == 5:
|
263
|
+
self.screen, left, top, width, height = (int(x) for x in params)
|
264
|
+
self.mode = "stream"
|
265
|
+
self.frame = 0
|
266
|
+
self.sct = mss.mss()
|
267
|
+
self.bs = 1
|
268
|
+
self.fps = 30
|
269
|
+
self.cv2_flag = cv2.IMREAD_GRAYSCALE if channels == 1 else cv2.IMREAD_COLOR # grayscale or RGB
|
270
|
+
|
271
|
+
# Parse monitor shape
|
272
|
+
monitor = self.sct.monitors[self.screen]
|
273
|
+
self.top = monitor["top"] if top is None else (monitor["top"] + top)
|
274
|
+
self.left = monitor["left"] if left is None else (monitor["left"] + left)
|
275
|
+
self.width = width or monitor["width"]
|
276
|
+
self.height = height or monitor["height"]
|
277
|
+
self.monitor = {"left": self.left, "top": self.top, "width": self.width, "height": self.height}
|
278
|
+
|
279
|
+
def __iter__(self):
|
280
|
+
"""Yields the next screenshot image from the specified screen or region for processing."""
|
281
|
+
return self
|
282
|
+
|
283
|
+
def __next__(self):
|
284
|
+
"""Captures and returns the next screenshot as a numpy array using the mss library."""
|
285
|
+
im0 = np.asarray(self.sct.grab(self.monitor))[:, :, :3] # BGRA to BGR
|
286
|
+
im0 = cv2.cvtColor(im0, cv2.COLOR_BGR2GRAY)[..., None] if self.cv2_flag == cv2.IMREAD_GRAYSCALE else im0
|
287
|
+
s = f"screen {self.screen} (LTWH): {self.left},{self.top},{self.width},{self.height}: "
|
288
|
+
|
289
|
+
self.frame += 1
|
290
|
+
return [str(self.screen)], [im0], [s] # screen, img, string
|
291
|
+
|
292
|
+
|
293
|
+
class LoadImagesAndVideos:
|
294
|
+
"""
|
295
|
+
A class for loading and processing images and videos for YOLO object detection.
|
296
|
+
|
297
|
+
This class manages the loading and pre-processing of image and video data from various sources, including
|
298
|
+
single image files, video files, and lists of image and video paths.
|
299
|
+
|
300
|
+
Attributes:
|
301
|
+
files (List[str]): List of image and video file paths.
|
302
|
+
nf (int): Total number of files (images and videos).
|
303
|
+
video_flag (List[bool]): Flags indicating whether a file is a video (True) or an image (False).
|
304
|
+
mode (str): Current mode, 'image' or 'video'.
|
305
|
+
vid_stride (int): Stride for video frame-rate.
|
306
|
+
bs (int): Batch size.
|
307
|
+
cap (cv2.VideoCapture): Video capture object for OpenCV.
|
308
|
+
frame (int): Frame counter for video.
|
309
|
+
frames (int): Total number of frames in the video.
|
310
|
+
count (int): Counter for iteration, initialized at 0 during __iter__().
|
311
|
+
ni (int): Number of images.
|
312
|
+
cv2_flag (int): OpenCV flag for image reading (grayscale or RGB).
|
313
|
+
|
314
|
+
Methods:
|
315
|
+
__init__: Initialize the LoadImagesAndVideos object.
|
316
|
+
__iter__: Returns an iterator object for VideoStream or ImageFolder.
|
317
|
+
__next__: Returns the next batch of images or video frames along with their paths and metadata.
|
318
|
+
_new_video: Creates a new video capture object for the given path.
|
319
|
+
__len__: Returns the number of batches in the object.
|
320
|
+
|
321
|
+
Examples:
|
322
|
+
>>> loader = LoadImagesAndVideos("path/to/data", batch=32, vid_stride=1)
|
323
|
+
>>> for paths, imgs, info in loader:
|
324
|
+
... # Process batch of images or video frames
|
325
|
+
... pass
|
326
|
+
|
327
|
+
Notes:
|
328
|
+
- Supports various image formats including HEIC.
|
329
|
+
- Handles both local files and directories.
|
330
|
+
- Can read from a text file containing paths to images and videos.
|
331
|
+
"""
|
332
|
+
|
333
|
+
def __init__(self, path, batch=1, vid_stride=1, channels=3):
|
334
|
+
"""Initialize dataloader for images and videos, supporting various input formats."""
|
335
|
+
parent = None
|
336
|
+
if isinstance(path, str) and Path(path).suffix == ".txt": # *.txt file with img/vid/dir on each line
|
337
|
+
parent = Path(path).parent
|
338
|
+
path = Path(path).read_text().splitlines() # list of sources
|
339
|
+
files = []
|
340
|
+
for p in sorted(path) if isinstance(path, (list, tuple)) else [path]:
|
341
|
+
a = str(Path(p).absolute()) # do not use .resolve() https://github.com/ultralytics/ultralytics/issues/2912
|
342
|
+
if "*" in a:
|
343
|
+
files.extend(sorted(glob.glob(a, recursive=True))) # glob
|
344
|
+
elif os.path.isdir(a):
|
345
|
+
files.extend(sorted(glob.glob(os.path.join(a, "*.*")))) # dir
|
346
|
+
elif os.path.isfile(a):
|
347
|
+
files.append(a) # files (absolute or relative to CWD)
|
348
|
+
elif parent and (parent / p).is_file():
|
349
|
+
files.append(str((parent / p).absolute())) # files (relative to *.txt file parent)
|
350
|
+
else:
|
351
|
+
raise FileNotFoundError(f"{p} does not exist")
|
352
|
+
|
353
|
+
# Define files as images or videos
|
354
|
+
images, videos = [], []
|
355
|
+
for f in files:
|
356
|
+
suffix = f.split(".")[-1].lower() # Get file extension without the dot and lowercase
|
357
|
+
if suffix in IMG_FORMATS:
|
358
|
+
images.append(f)
|
359
|
+
elif suffix in VID_FORMATS:
|
360
|
+
videos.append(f)
|
361
|
+
ni, nv = len(images), len(videos)
|
362
|
+
|
363
|
+
self.files = images + videos
|
364
|
+
self.nf = ni + nv # number of files
|
365
|
+
self.ni = ni # number of images
|
366
|
+
self.video_flag = [False] * ni + [True] * nv
|
367
|
+
self.mode = "video" if ni == 0 else "image" # default to video if no images
|
368
|
+
self.vid_stride = vid_stride # video frame-rate stride
|
369
|
+
self.bs = batch
|
370
|
+
self.cv2_flag = cv2.IMREAD_GRAYSCALE if channels == 1 else cv2.IMREAD_COLOR # grayscale or RGB
|
371
|
+
if any(videos):
|
372
|
+
self._new_video(videos[0]) # new video
|
373
|
+
else:
|
374
|
+
self.cap = None
|
375
|
+
if self.nf == 0:
|
376
|
+
raise FileNotFoundError(f"No images or videos found in {p}. {FORMATS_HELP_MSG}")
|
377
|
+
|
378
|
+
def __iter__(self):
|
379
|
+
"""Iterates through image/video files, yielding source paths, images, and metadata."""
|
380
|
+
self.count = 0
|
381
|
+
return self
|
382
|
+
|
383
|
+
def __next__(self):
|
384
|
+
"""Returns the next batch of images or video frames with their paths and metadata."""
|
385
|
+
paths, imgs, info = [], [], []
|
386
|
+
while len(imgs) < self.bs:
|
387
|
+
if self.count >= self.nf: # end of file list
|
388
|
+
if imgs:
|
389
|
+
return paths, imgs, info # return last partial batch
|
390
|
+
else:
|
391
|
+
raise StopIteration
|
392
|
+
|
393
|
+
path = self.files[self.count]
|
394
|
+
if self.video_flag[self.count]:
|
395
|
+
self.mode = "video"
|
396
|
+
if not self.cap or not self.cap.isOpened():
|
397
|
+
self._new_video(path)
|
398
|
+
|
399
|
+
success = False
|
400
|
+
for _ in range(self.vid_stride):
|
401
|
+
success = self.cap.grab()
|
402
|
+
if not success:
|
403
|
+
break # end of video or failure
|
404
|
+
|
405
|
+
if success:
|
406
|
+
success, im0 = self.cap.retrieve()
|
407
|
+
im0 = (
|
408
|
+
cv2.cvtColor(im0, cv2.COLOR_BGR2GRAY)[..., None]
|
409
|
+
if self.cv2_flag == cv2.IMREAD_GRAYSCALE
|
410
|
+
else im0
|
411
|
+
)
|
412
|
+
if success:
|
413
|
+
self.frame += 1
|
414
|
+
paths.append(path)
|
415
|
+
imgs.append(im0)
|
416
|
+
info.append(f"video {self.count + 1}/{self.nf} (frame {self.frame}/{self.frames}) {path}: ")
|
417
|
+
if self.frame == self.frames: # end of video
|
418
|
+
self.count += 1
|
419
|
+
self.cap.release()
|
420
|
+
else:
|
421
|
+
# Move to the next file if the current video ended or failed to open
|
422
|
+
self.count += 1
|
423
|
+
if self.cap:
|
424
|
+
self.cap.release()
|
425
|
+
if self.count < self.nf:
|
426
|
+
self._new_video(self.files[self.count])
|
427
|
+
else:
|
428
|
+
# Handle image files (including HEIC)
|
429
|
+
self.mode = "image"
|
430
|
+
if path.split(".")[-1].lower() == "heic":
|
431
|
+
# Load HEIC image using Pillow with pillow-heif
|
432
|
+
check_requirements("pillow-heif")
|
433
|
+
|
434
|
+
from pillow_heif import register_heif_opener
|
435
|
+
|
436
|
+
register_heif_opener() # Register HEIF opener with Pillow
|
437
|
+
with Image.open(path) as img:
|
438
|
+
im0 = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) # convert image to BGR nparray
|
439
|
+
else:
|
440
|
+
im0 = imread(path, flags=self.cv2_flag) # BGR
|
441
|
+
if im0 is None:
|
442
|
+
LOGGER.warning(f"Image Read Error {path}")
|
443
|
+
else:
|
444
|
+
paths.append(path)
|
445
|
+
imgs.append(im0)
|
446
|
+
info.append(f"image {self.count + 1}/{self.nf} {path}: ")
|
447
|
+
self.count += 1 # move to the next file
|
448
|
+
if self.count >= self.ni: # end of image list
|
449
|
+
break
|
450
|
+
|
451
|
+
return paths, imgs, info
|
452
|
+
|
453
|
+
def _new_video(self, path):
|
454
|
+
"""Creates a new video capture object for the given path and initializes video-related attributes."""
|
455
|
+
self.frame = 0
|
456
|
+
self.cap = cv2.VideoCapture(path)
|
457
|
+
self.fps = int(self.cap.get(cv2.CAP_PROP_FPS))
|
458
|
+
if not self.cap.isOpened():
|
459
|
+
raise FileNotFoundError(f"Failed to open video {path}")
|
460
|
+
self.frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT) / self.vid_stride)
|
461
|
+
|
462
|
+
def __len__(self):
|
463
|
+
"""Returns the number of files (images and videos) in the dataset."""
|
464
|
+
return math.ceil(self.nf / self.bs) # number of batches
|
465
|
+
|
466
|
+
|
467
|
+
class LoadPilAndNumpy:
|
468
|
+
"""
|
469
|
+
Load images from PIL and Numpy arrays for batch processing.
|
470
|
+
|
471
|
+
This class manages loading and pre-processing of image data from both PIL and Numpy formats. It performs basic
|
472
|
+
validation and format conversion to ensure that the images are in the required format for downstream processing.
|
473
|
+
|
474
|
+
Attributes:
|
475
|
+
paths (List[str]): List of image paths or autogenerated filenames.
|
476
|
+
im0 (List[np.ndarray]): List of images stored as Numpy arrays.
|
477
|
+
mode (str): Type of data being processed, set to 'image'.
|
478
|
+
bs (int): Batch size, equivalent to the length of `im0`.
|
479
|
+
|
480
|
+
Methods:
|
481
|
+
_single_check: Validate and format a single image to a Numpy array.
|
482
|
+
|
483
|
+
Examples:
|
484
|
+
>>> from PIL import Image
|
485
|
+
>>> import numpy as np
|
486
|
+
>>> pil_img = Image.new("RGB", (100, 100))
|
487
|
+
>>> np_img = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
|
488
|
+
>>> loader = LoadPilAndNumpy([pil_img, np_img])
|
489
|
+
>>> paths, images, _ = next(iter(loader))
|
490
|
+
>>> print(f"Loaded {len(images)} images")
|
491
|
+
Loaded 2 images
|
492
|
+
"""
|
493
|
+
|
494
|
+
def __init__(self, im0, channels=3):
|
495
|
+
"""Initializes a loader for PIL and Numpy images, converting inputs to a standardized format."""
|
496
|
+
if not isinstance(im0, list):
|
497
|
+
im0 = [im0]
|
498
|
+
# use `image{i}.jpg` when Image.filename returns an empty path.
|
499
|
+
self.paths = [getattr(im, "filename", "") or f"image{i}.jpg" for i, im in enumerate(im0)]
|
500
|
+
pil_flag = "L" if channels == 1 else "RGB" # grayscale or RGB
|
501
|
+
self.im0 = [self._single_check(im, pil_flag) for im in im0]
|
502
|
+
self.mode = "image"
|
503
|
+
self.bs = len(self.im0)
|
504
|
+
|
505
|
+
@staticmethod
|
506
|
+
def _single_check(im, flag="RGB"):
|
507
|
+
"""Validate and format an image to numpy array, ensuring RGB order and contiguous memory."""
|
508
|
+
assert isinstance(im, (Image.Image, np.ndarray)), f"Expected PIL/np.ndarray image type, but got {type(im)}"
|
509
|
+
if isinstance(im, Image.Image):
|
510
|
+
im = np.asarray(im.convert(flag))
|
511
|
+
# adding new axis if it's grayscale, and converting to BGR if it's RGB
|
512
|
+
im = im[..., None] if flag == "L" else im[..., ::-1]
|
513
|
+
im = np.ascontiguousarray(im) # contiguous
|
514
|
+
elif im.ndim == 2: # grayscale in numpy form
|
515
|
+
im = im[..., None]
|
516
|
+
return im
|
517
|
+
|
518
|
+
def __len__(self):
|
519
|
+
"""Returns the length of the 'im0' attribute, representing the number of loaded images."""
|
520
|
+
return len(self.im0)
|
521
|
+
|
522
|
+
def __next__(self):
|
523
|
+
"""Returns the next batch of images, paths, and metadata for processing."""
|
524
|
+
if self.count == 1: # loop only once as it's batch inference
|
525
|
+
raise StopIteration
|
526
|
+
self.count += 1
|
527
|
+
return self.paths, self.im0, [""] * self.bs
|
528
|
+
|
529
|
+
def __iter__(self):
|
530
|
+
"""Iterates through PIL/numpy images, yielding paths, raw images, and metadata for processing."""
|
531
|
+
self.count = 0
|
532
|
+
return self
|
533
|
+
|
534
|
+
|
535
|
+
class LoadTensor:
|
536
|
+
"""
|
537
|
+
A class for loading and processing tensor data for object detection tasks.
|
538
|
+
|
539
|
+
This class handles the loading and pre-processing of image data from PyTorch tensors, preparing them for
|
540
|
+
further processing in object detection pipelines.
|
541
|
+
|
542
|
+
Attributes:
|
543
|
+
im0 (torch.Tensor): The input tensor containing the image(s) with shape (B, C, H, W).
|
544
|
+
bs (int): Batch size, inferred from the shape of `im0`.
|
545
|
+
mode (str): Current processing mode, set to 'image'.
|
546
|
+
paths (List[str]): List of image paths or auto-generated filenames.
|
547
|
+
|
548
|
+
Methods:
|
549
|
+
_single_check: Validates and formats an input tensor.
|
550
|
+
|
551
|
+
Examples:
|
552
|
+
>>> import torch
|
553
|
+
>>> tensor = torch.rand(1, 3, 640, 640)
|
554
|
+
>>> loader = LoadTensor(tensor)
|
555
|
+
>>> paths, images, info = next(iter(loader))
|
556
|
+
>>> print(f"Processed {len(images)} images")
|
557
|
+
"""
|
558
|
+
|
559
|
+
def __init__(self, im0) -> None:
|
560
|
+
"""Initialize LoadTensor object for processing torch.Tensor image data."""
|
561
|
+
self.im0 = self._single_check(im0)
|
562
|
+
self.bs = self.im0.shape[0]
|
563
|
+
self.mode = "image"
|
564
|
+
self.paths = [getattr(im, "filename", f"image{i}.jpg") for i, im in enumerate(im0)]
|
565
|
+
|
566
|
+
@staticmethod
|
567
|
+
def _single_check(im, stride=32):
|
568
|
+
"""Validates and formats a single image tensor, ensuring correct shape and normalization."""
|
569
|
+
s = (
|
570
|
+
f"torch.Tensor inputs should be BCHW i.e. shape(1, 3, 640, 640) "
|
571
|
+
f"divisible by stride {stride}. Input shape{tuple(im.shape)} is incompatible."
|
572
|
+
)
|
573
|
+
if len(im.shape) != 4:
|
574
|
+
if len(im.shape) != 3:
|
575
|
+
raise ValueError(s)
|
576
|
+
LOGGER.warning(s)
|
577
|
+
im = im.unsqueeze(0)
|
578
|
+
if im.shape[2] % stride or im.shape[3] % stride:
|
579
|
+
raise ValueError(s)
|
580
|
+
if im.max() > 1.0 + torch.finfo(im.dtype).eps: # torch.float32 eps is 1.2e-07
|
581
|
+
LOGGER.warning(
|
582
|
+
f"torch.Tensor inputs should be normalized 0.0-1.0 but max value is {im.max()}. Dividing input by 255."
|
583
|
+
)
|
584
|
+
im = im.float() / 255.0
|
585
|
+
|
586
|
+
return im
|
587
|
+
|
588
|
+
def __iter__(self):
|
589
|
+
"""Yields an iterator object for iterating through tensor image data."""
|
590
|
+
self.count = 0
|
591
|
+
return self
|
592
|
+
|
593
|
+
def __next__(self):
|
594
|
+
"""Yields the next batch of tensor images and metadata for processing."""
|
595
|
+
if self.count == 1:
|
596
|
+
raise StopIteration
|
597
|
+
self.count += 1
|
598
|
+
return self.paths, self.im0, [""] * self.bs
|
599
|
+
|
600
|
+
def __len__(self):
|
601
|
+
"""Returns the batch size of the tensor input."""
|
602
|
+
return self.bs
|
603
|
+
|
604
|
+
|
605
|
+
def autocast_list(source):
|
606
|
+
"""Merges a list of sources into a list of numpy arrays or PIL images for Ultralytics prediction."""
|
607
|
+
files = []
|
608
|
+
for im in source:
|
609
|
+
if isinstance(im, (str, Path)): # filename or uri
|
610
|
+
files.append(Image.open(urllib.request.urlopen(im) if str(im).startswith("http") else im))
|
611
|
+
elif isinstance(im, (Image.Image, np.ndarray)): # PIL or np Image
|
612
|
+
files.append(im)
|
613
|
+
else:
|
614
|
+
raise TypeError(
|
615
|
+
f"type {type(im).__name__} is not a supported Ultralytics prediction source type. \n"
|
616
|
+
f"See https://docs.ultralytics.com/modes/predict for supported source types."
|
617
|
+
)
|
618
|
+
|
619
|
+
return files
|
620
|
+
|
621
|
+
|
622
|
+
def get_best_youtube_url(url, method="pytube"):
|
623
|
+
"""
|
624
|
+
Retrieves the URL of the best quality MP4 video stream from a given YouTube video.
|
625
|
+
|
626
|
+
Args:
|
627
|
+
url (str): The URL of the YouTube video.
|
628
|
+
method (str): The method to use for extracting video info. Options are "pytube", "pafy", and "yt-dlp".
|
629
|
+
Defaults to "pytube".
|
630
|
+
|
631
|
+
Returns:
|
632
|
+
(str | None): The URL of the best quality MP4 video stream, or None if no suitable stream is found.
|
633
|
+
|
634
|
+
Examples:
|
635
|
+
>>> url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
636
|
+
>>> best_url = get_best_youtube_url(url)
|
637
|
+
>>> print(best_url)
|
638
|
+
https://rr4---sn-q4flrnek.googlevideo.com/videoplayback?expire=...
|
639
|
+
|
640
|
+
Notes:
|
641
|
+
- Requires additional libraries based on the chosen method: pytubefix, pafy, or yt-dlp.
|
642
|
+
- The function prioritizes streams with at least 1080p resolution when available.
|
643
|
+
- For the "yt-dlp" method, it looks for formats with video codec, no audio, and *.mp4 extension.
|
644
|
+
"""
|
645
|
+
if method == "pytube":
|
646
|
+
# Switched from pytube to pytubefix to resolve https://github.com/pytube/pytube/issues/1954
|
647
|
+
check_requirements("pytubefix>=6.5.2")
|
648
|
+
from pytubefix import YouTube
|
649
|
+
|
650
|
+
streams = YouTube(url).streams.filter(file_extension="mp4", only_video=True)
|
651
|
+
streams = sorted(streams, key=lambda s: s.resolution, reverse=True) # sort streams by resolution
|
652
|
+
for stream in streams:
|
653
|
+
if stream.resolution and int(stream.resolution[:-1]) >= 1080: # check if resolution is at least 1080p
|
654
|
+
return stream.url
|
655
|
+
|
656
|
+
elif method == "pafy":
|
657
|
+
check_requirements(("pafy", "youtube_dl==2020.12.2"))
|
658
|
+
import pafy # noqa
|
659
|
+
|
660
|
+
return pafy.new(url).getbestvideo(preftype="mp4").url
|
661
|
+
|
662
|
+
elif method == "yt-dlp":
|
663
|
+
check_requirements("yt-dlp")
|
664
|
+
import yt_dlp
|
665
|
+
|
666
|
+
with yt_dlp.YoutubeDL({"quiet": True}) as ydl:
|
667
|
+
info_dict = ydl.extract_info(url, download=False) # extract info
|
668
|
+
for f in reversed(info_dict.get("formats", [])): # reversed because best is usually last
|
669
|
+
# Find a format with video codec, no audio, *.mp4 extension at least 1920x1080 size
|
670
|
+
good_size = (f.get("width") or 0) >= 1920 or (f.get("height") or 0) >= 1080
|
671
|
+
if good_size and f["vcodec"] != "none" and f["acodec"] == "none" and f["ext"] == "mp4":
|
672
|
+
return f.get("url")
|
673
|
+
|
674
|
+
|
675
|
+
# Define constants
|
676
|
+
LOADERS = (LoadStreams, LoadPilAndNumpy, LoadImagesAndVideos, LoadScreenshots)
|