YouTube Bug Fix (#2818)

Fix for #2810
```shell
python detect.py --source 0
```
introduced by YouTube Livestream Detection PR #2752
This commit is contained in:
Glenn Jocher authored and GitHub committed 2021-04-16 17:58:28 +02:00
1 parent 1f3e482bce
commit aff03be35a
1 file changed
+6 -6
+6 -6
View File
@@ -272,15 +272,15 @@ class LoadStreams: # multiple IP or RTSP cameras
n = len(sources) n = len(sources)
self.imgs = [None] * n self.imgs = [None] * n
self.sources = [clean_str(x) for x in sources] # clean source names for later self.sources = [clean_str(x) for x in sources] # clean source names for later
for i, s in enumerate(sources): for i, s in enumerate(sources): # index, source
# Start the thread to read frames from the video stream # Start thread to read frames from video stream
print(f'{i + 1}/{n}: {s}... ', end='') print(f'{i + 1}/{n}: {s}... ', end='')
url = eval(s) if s.isnumeric() else s if 'youtube.com/' in s or 'youtu.be/' in s: # if source is YouTube video
if 'youtube.com/' in url or 'youtu.be/' in url: # if source is YouTube video
check_requirements(('pafy', 'youtube_dl')) check_requirements(('pafy', 'youtube_dl'))
import pafy import pafy
url = pafy.new(url).getbest(preftype="mp4").url s = pafy.new(s).getbest(preftype="mp4").url # YouTube URL
cap = cv2.VideoCapture(url) s = eval(s) if s.isnumeric() else s # i.e. s = '0' local webcam
cap = cv2.VideoCapture(s)
assert cap.isOpened(), f'Failed to open {s}' assert cap.isOpened(), f'Failed to open {s}'
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))