2012-02-29 48 views
2

我想使用face.com人臉檢測API(將圖像發送到服務器並獲取xml字符串結果)。我使用urlread(),它不能上傳圖像文件。如何使用MATLAB中的urlread()將圖像發佈到服務器

代碼:

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}) 

但因爲MATLAB試圖將圖像編碼爲URL返回一個錯誤。

注意:當我在網絡上使用圖像時,它確實有效(因爲沒有文件上傳)。

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'}) 
+0

我編輯了問題的第一個單詞,從'熱'到'如何'。如果事實上這不是一個如何做某事的問題,而是一個你熱衷或有壓力去做某事的陳述,我會道歉。 – 2012-02-29 09:38:18

回答

2

不幸的是,您不能使用內建函數urlread。它僅對POST請求使用application/x-www-form-urlencoded,而face API需要multipart/form-data才能上傳jpeg文件。你將不得不看第三方工具

或者,你可以嘗試編寫自己修改後的urlread函數。然而,Matlab沒有比urlread更細的訪問。要解決這個問題,你可以使用Java within Matlab。該文檔甚至包含一個URL example。基本上你可以創建Java對象並在Matlab解釋器中調用它們的方法。下面是Matlab內部Java的一個例子:

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' 

祝你好運。

0

Mathworks創建了webreadwebwrite來解決這個問題在更新版本的Matlab中。

相關問題