DanielHsH
DanielHsH

Reputation: 4453

How to post image to server using urlread() in MATLAB

I want to use face.com face detection API (send an image to a server and get back an xml string result).

I use urlread() and it cannot upload an image file.

the code:

fid = fopen('T000.jpg');
im  = fread(fid,Inf,'*uint8');
fclose(fid);

urlread('http://api.face.com/faces/detect.xml','post',...
        {'api_key'      , MY_CODE,...
         'api_secret'   , MY_SECRET,...
         'detector'     , 'Normal',...
         'attributes'   , 'all',...
         'file'         , im}) 

But it returns an error because MATLAB tries to encode the image as url.

Note: when I use an image on the web it does work (since no file is uploaded).

urlread('http://api.face.com/faces/detect.xml','post',...
        {'api_key'      , MY_CODE,...
         'api_secret'   , MY_SECRET,...
         'detector'     , 'Normal',...
         'attributes'   , 'all',...
         'urls'         , 'http://0.tqn.com/d/beauty/1/0/x/3/1/halle_berry_pixie.jpg'}) 

Upvotes: 2

Views: 2975

Answers (3)

user46688
user46688

Reputation: 753

Mathworks created webread and webwrite to address this in newer versions of Matlab.

Upvotes: 0

DanielHsH
DanielHsH

Reputation: 4453

I found an answer. An alternative implementation to urlread() on the File Exchange.

Upvotes: 1

AE426082
AE426082

Reputation: 633

Unfortunately you cannot use the builtin function urlread. It only uses application/x-www-form-urlencoded for POST requests and the face API needs multipart/form-data in order to upload jpeg files. You would have to look at third party tools

Alternatively you could try writing your own modified urlread function. However Matlab does not have more fine grained access than urlread. To solve this you can use Java within Matlab. The docs even contain a URL example. Basically you can create Java objects and call their methods in the Matlab interpreter. Here is an example of Java inside Matlab:

string_builder = java.lang.StringBuilder('Bar'); %new is not used
string_builder.setCharAt(2, 'z');
java_string = string_builder.toString.toLowerCase; %brackets are optional
matlab_char = char(java_string); %matlab_char == 'baz'

Best of luck.

Upvotes: 2

Related Questions