0

I tried to read live video stream with OpenCV as following,

cv::VideoCapture capture(video_url);

It can read from live video stream and works well. However, when a fake video_url was sent to it, such as an url of a txt file, for example,

video_url = "http://127.0.0.10:8090/result.txt"

it can also decode data from this fake url. But I want it to return error information when the video_url is fake.

How can I make it to be able to discriminate whether an url is truly of a live video stream, or a txt web file?

  • 1
    You can check if `cap.isOpened()` returns false, and, also check if the captured frame is empty using `frame.empty()` – S4rt-H4K Aug 31 '20 at 08:48
  • cap.isOpened() returns true, and frame also contains data. I can reproduce this with python-opencv as I described in this issue https://github.com/opencv/opencv/issues/18233 – Gaofeng Sun Aug 31 '20 at 09:49
  • I'm surprised that cv2 actually works with that. In that case, you have to check the link yourself. Hope this answer will help you https://stackoverflow.com/a/46101052 OR, you can create a datatype check for 1-2 frames after creating the VideoCapture object – S4rt-H4K Aug 31 '20 at 10:48

1 Answers1

1

python


You can use URLValidator() for discriminating fake urls.

from django.core.exceptions import ValidationError
from django.core.validators import URLValidator

validate = URLValidator()
is_valid = False

url = "https://www.youtube.com/watch?v=vNSxargsAWk"

try:
    validate(url)
    is_valid = True
except ValidationError as exception:
    print("url is not valid")

c++


  • Check the URL using regular expression.
#include <regex>
using namespace std;

int main()
{
    regex url_validator("/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/");
    
    if(regex_match(input, url_validator))
        cout<<"Input is an integer"<<endl;
    else
        cout<<"Invalid input : Not an integer"<<endl;
}

Ahmet
  • 7,527
  • 3
  • 23
  • 47