0

I would like to be able to capture YouTube videos (both live and recorded) using open cv.

I have found the question below, but is seems based on the comments and my own trials that the code/solutions provided below do not work on recent open cv versions.

Is it possible to stream video from https:// (e.g. YouTube) into python with OpenCV?

Is there any way to stream YouTube video through the most recent open cv version: opencv-python 4.1.0.25?

My goal is to use this to test a facial recognition algorithm on several random video streams which have human faces (for example news shows) to test for false positives.

Mustard Tiger
  • 3,520
  • 8
  • 43
  • 68
  • I think there is no problem with opencv but pafy. You can check by using pafy to capture the streaming url of your desired youtube video (in the refer link it is *play.url* ). Then open that pafy.url in web browser to check if it is working. – Peter Lee May 31 '19 at 01:04

1 Answers1

2

Below is the method that i used for stream the data to opencv. But I was using a older version of opencv and link to caffe myself.

Install Pafy and youtubedl

pip install pafy
pip install youtube_dl

After install, copy the url from the video you want. Below is the sample code

url = 'https://youtu.be/1AbfRENy3OQ'
urlPafy = pafy.new(url)
videoplay = urlPafy.getbest(preftype="webm")


cap = cv2.VideoCapture(videoplay.url)
while (True):
    ret,src = cap.read()
    cv2.imshow('src',src)
    #do your stuff here. 

cap.release()
cv2.destroyAllWindows()

But if you want the auto select random video with face in it that will be a bit more complicated

You need to use YouTube-API to get random VideoId's from a set of search word( e.g pretty face, deep fake faces)

Then from the queried database, auto loop through for your learning algorithm. Below is a short sample from other post

import json
import urllib.request
import string
import random

count = 50
API_KEY = 'your_key'
random = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(3))

urlData = "https://www.googleapis.com/youtube/v3/search?key={}&maxResults={}&part=snippet&type=video&q={}".format(API_KEY,count,random)
webURL = urllib.request.urlopen(urlData)
data = webURL.read()
encoding = webURL.info().get_content_charset('utf-8')
results = json.loads(data.decode(encoding))

for data in results['items']:
    videoId = (data['id']['videoId'])
    print(videoId)
    #store your ids

But without ground truth label, it is difficult to get a quantitative measure for your algo performance. Thus, I would suggest getting from one of the face video datasets for effective computing for the score. You need that properly generated score for publication.

https://www.cs.tau.ac.il/~wolf/ytfaces/

Dr Yuan Shenghai
  • 1,849
  • 1
  • 6
  • 19