Search code examples
matlaburlimread

How to deal with expired URL links when using imread URL?


I am working on the face scrub dataset, which has a long list of urls of pictures.

I used a for loop to fetch these photos. However, some of the urls had expired so my matlab code return an error saying 'Unable to determine the file format.' However I think the actual reason is that the url link does not have the image anymore. For example, one of the bad urls is: http://www.gossip.is/administrator/components/com_n-myndir/uploads/16de47418d69b1c2991731dffaca8a78.jpg

How do I identify and ignore this error so my code can keep working on the rest of the list? I could use R instead if that make solving this problem easier.


Solution

  • You can implement a try/catch block to catch (original isnt'it) the error message and skip the image if the link is indeed broken.

    When we use the following syntax:

    try
       A = imread('http://www.gossip.is/cgi-sys/suspendedpage.cgi');
    
    catch ME
    
     %// Just so we know what the identifier is.  
          ME
    
    
    end
    

    Matlab first tries to read the image given by the url. If it can't, we ask it to catch the error message (MException actually) and perform some other appropriate action.

    The thing is, we need to know what is the exact error message in order to recognize it in the try/catch block.

    When I entered the above code, I got the following structure for ME:

     ME = 
    
      MException with properties:
    
        identifier: 'MATLAB:imagesci:imread:fileFormat'
           message: 'Unable to determine the file format.'
             cause: {0x1 cell}
             stack: [2x1 struct]
    

    Therefore, once we know the exact identifier generating the error we can use strcmp to look for it in the try/catch block. For instance with the following code:

    clear
    clc
    
    
    try
       A = imread('http://www.gossip.is/cgi-sys/suspendedpage.cgi');
    catch ME
       if strcmp(ME.identifier,'MATLAB:imagesci:imread:fileFormat')
    
           disp('Image link broken')
    
       end
    
       A = imread('peppers.png');
    end 
    
    imshow(A);
    

    Matlab displays 'Image link broken' and reads peppers.png, as expected.

    Hope that helps!