YouTube Livestream Detection (#2752)
* Youtube livestream detection * dependancy update to auto install pafy * Remove print * include youtube_dl in deps * PEP8 reformat * youtube url check fix * reduce lines * add comment * update check_requirements * stream framerate fix * Update README.md * cleanup * PEP8 * remove cap.retrieve() failure code Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
1 parent
9029759cb3
commit
e2b7bc0b32
4 files changed
+30
-20
No files matched your search
+15
-8
@@ -20,8 +20,8 @@ from PIL import Image, ExifTags
|
||||
from torch.utils.data import Dataset
|
||||
from tqdm import tqdm
|
||||
|
||||
from utils.general import xyxy2xywh, xywh2xyxy, xywhn2xyxy, xyn2xy, segment2box, segments2boxes, resample_segments, \
|
||||
clean_str
|
||||
from utils.general import check_requirements, xyxy2xywh, xywh2xyxy, xywhn2xyxy, xyn2xy, segment2box, segments2boxes, \
|
||||
resample_segments, clean_str
|
||||
from utils.torch_utils import torch_distributed_zero_first
|
||||
|
||||
# Parameters
|
||||
@@ -275,14 +275,20 @@ class LoadStreams: # multiple IP or RTSP cameras
|
||||
for i, s in enumerate(sources):
|
||||
# Start the thread to read frames from the video stream
|
||||
print(f'{i + 1}/{n}: {s}... ', end='')
|
||||
cap = cv2.VideoCapture(eval(s) if s.isnumeric() else s)
|
||||
url = eval(s) if s.isnumeric() else s
|
||||
if 'youtube.com/' in url or 'youtu.be/' in url: # if source is YouTube video
|
||||
check_requirements(('pafy', 'youtube_dl'))
|
||||
import pafy
|
||||
url = pafy.new(url).getbest(preftype="mp4").url
|
||||
cap = cv2.VideoCapture(url)
|
||||
assert cap.isOpened(), f'Failed to open {s}'
|
||||
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
||||
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
||||
fps = cap.get(cv2.CAP_PROP_FPS) % 100
|
||||
self.fps = cap.get(cv2.CAP_PROP_FPS) % 100
|
||||
|
||||
_, self.imgs[i] = cap.read() # guarantee first frame
|
||||
thread = Thread(target=self.update, args=([i, cap]), daemon=True)
|
||||
print(f' success ({w}x{h} at {fps:.2f} FPS).')
|
||||
print(f' success ({w}x{h} at {self.fps:.2f} FPS).')
|
||||
thread.start()
|
||||
print('') # newline
|
||||
|
||||
@@ -303,7 +309,7 @@ class LoadStreams: # multiple IP or RTSP cameras
|
||||
success, im = cap.retrieve()
|
||||
self.imgs[index] = im if success else self.imgs[index] * 0
|
||||
n = 0
|
||||
time.sleep(0.01) # wait time
|
||||
time.sleep(1 / self.fps) # wait time
|
||||
|
||||
def __iter__(self):
|
||||
self.count = -1
|
||||
@@ -444,7 +450,7 @@ class LoadImagesAndLabels(Dataset): # for training/testing
|
||||
gb += self.imgs[i].nbytes
|
||||
pbar.desc = f'{prefix}Caching images ({gb / 1E9:.1f}GB)'
|
||||
pbar.close()
|
||||
|
||||
|
||||
def cache_labels(self, path=Path('./labels.cache'), prefix=''):
|
||||
# Cache dataset labels, check images and read shapes
|
||||
x = {} # dict
|
||||
@@ -489,7 +495,7 @@ class LoadImagesAndLabels(Dataset): # for training/testing
|
||||
pbar.desc = f"{prefix}Scanning '{path.parent / path.stem}' images and labels... " \
|
||||
f"{nf} found, {nm} missing, {ne} empty, {nc} corrupted"
|
||||
pbar.close()
|
||||
|
||||
|
||||
if nf == 0:
|
||||
print(f'{prefix}WARNING: No labels found in {path}. See {help_url}')
|
||||
|
||||
@@ -1034,6 +1040,7 @@ def extract_boxes(path='../coco128/'): # from utils.datasets import *; extract_
|
||||
b[[1, 3]] = np.clip(b[[1, 3]], 0, h)
|
||||
assert cv2.imwrite(str(f), im[b[1]:b[3], b[0]:b[2]]), f'box failure in {f}'
|
||||
|
||||
|
||||
def autosplit(path='../coco128', weights=(0.9, 0.1, 0.0), annotated_only=False):
|
||||
""" Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files
|
||||
Usage: from utils.datasets import *; autosplit('../coco128')
|
||||
|
||||
Reference in new issue
Block a user