Specifying a timeout when reading a URL image using the MATLAB imread function

The imread MATLAB function provides the ability to read images from a URL. This usually works fine, but sometimes I come across an image that takes a lot of time.

For example, at the time of publication, the following URL just got stuck in the "connect ..." state for more than 5 minutes before finally succeeding.

http://www.hollywoodheadache.com/wp-content/uploads/2007/12/tom-and-julia.jpg

Is there a way to set the timeout in MATLAB?

thanks

+4
source share
1 answer

I do not know how to abort imread using a timer object. In addition, I suspect its capabilities. But I can recommend you check if you can access the file first and then get the file. I wrote the following function to check the status of a file and the Internet:

 function flag = does_url_exist(urlName) url =java.net.URL(urlName); try link = openStream(url); parse = java.io.InputStreamReader(link); snip = java.io.BufferedReader(parse); if ~isempty(snip) flag = 1; else flag = 0; end catch exception flag = 0; end end 

Then it looks like this:

 fname = 'http://www.hollywoodheadache.com/wp-content/uploads/2007/12/tom-and-julia.jpg'; if(does_url_exist(fname)) img = imread(fname); end 

Please note that to check the Internet connection, I took the source code for this post . Also note that if you are sure that the file exists, it is impossible to verify it again, since it increases the operating time.

+1
source

Source: https://habr.com/ru/post/1386570/


All Articles