20
20
from torch .utils .data import Dataset
21
21
from tqdm import tqdm
22
22
23
- from utils .general import xyxy2xywh , xywh2xyxy , xywhn2xyxy , xyn2xy , segment2box , segments2boxes , resample_segments , \
24
- clean_str
23
+ from utils .general import check_requirements , xyxy2xywh , xywh2xyxy , xywhn2xyxy , xyn2xy , segment2box , segments2boxes , \
24
+ resample_segments , clean_str
25
25
from utils .torch_utils import torch_distributed_zero_first
26
26
27
27
# Parameters
@@ -275,14 +275,20 @@ def __init__(self, sources='streams.txt', img_size=640, stride=32):
275
275
for i , s in enumerate (sources ):
276
276
# Start the thread to read frames from the video stream
277
277
print (f'{ i + 1 } /{ n } : { s } ... ' , end = '' )
278
- cap = cv2 .VideoCapture (eval (s ) if s .isnumeric () else s )
278
+ url = eval (s ) if s .isnumeric () else s
279
+ if 'youtube.com/' in url or 'youtu.be/' in url : # if source is YouTube video
280
+ check_requirements (('pafy' , 'youtube_dl' ))
281
+ import pafy
282
+ url = pafy .new (url ).getbest (preftype = "mp4" ).url
283
+ cap = cv2 .VideoCapture (url )
279
284
assert cap .isOpened (), f'Failed to open { s } '
280
285
w = int (cap .get (cv2 .CAP_PROP_FRAME_WIDTH ))
281
286
h = int (cap .get (cv2 .CAP_PROP_FRAME_HEIGHT ))
282
- fps = cap .get (cv2 .CAP_PROP_FPS ) % 100
287
+ self .fps = cap .get (cv2 .CAP_PROP_FPS ) % 100
288
+
283
289
_ , self .imgs [i ] = cap .read () # guarantee first frame
284
290
thread = Thread (target = self .update , args = ([i , cap ]), daemon = True )
285
- print (f' success ({ w } x{ h } at { fps :.2f} FPS).' )
291
+ print (f' success ({ w } x{ h } at { self . fps :.2f} FPS).' )
286
292
thread .start ()
287
293
print ('' ) # newline
288
294
@@ -303,7 +309,7 @@ def update(self, index, cap):
303
309
success , im = cap .retrieve ()
304
310
self .imgs [index ] = im if success else self .imgs [index ] * 0
305
311
n = 0
306
- time .sleep (0.01 ) # wait time
312
+ time .sleep (1 / self . fps ) # wait time
307
313
308
314
def __iter__ (self ):
309
315
self .count = - 1
@@ -444,7 +450,7 @@ def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, r
444
450
gb += self .imgs [i ].nbytes
445
451
pbar .desc = f'{ prefix } Caching images ({ gb / 1E9 :.1f} GB)'
446
452
pbar .close ()
447
-
453
+
448
454
def cache_labels (self , path = Path ('./labels.cache' ), prefix = '' ):
449
455
# Cache dataset labels, check images and read shapes
450
456
x = {} # dict
@@ -489,7 +495,7 @@ def cache_labels(self, path=Path('./labels.cache'), prefix=''):
489
495
pbar .desc = f"{ prefix } Scanning '{ path .parent / path .stem } ' images and labels... " \
490
496
f"{ nf } found, { nm } missing, { ne } empty, { nc } corrupted"
491
497
pbar .close ()
492
-
498
+
493
499
if nf == 0 :
494
500
print (f'{ prefix } WARNING: No labels found in { path } . See { help_url } ' )
495
501
@@ -1034,6 +1040,7 @@ def extract_boxes(path='../coco128/'): # from utils.datasets import *; extract_
1034
1040
b [[1 , 3 ]] = np .clip (b [[1 , 3 ]], 0 , h )
1035
1041
assert cv2 .imwrite (str (f ), im [b [1 ]:b [3 ], b [0 ]:b [2 ]]), f'box failure in { f } '
1036
1042
1043
+
1037
1044
def autosplit (path = '../coco128' , weights = (0.9 , 0.1 , 0.0 ), annotated_only = False ):
1038
1045
""" Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files
1039
1046
Usage: from utils.datasets import *; autosplit('../coco128')
0 commit comments