2016-12-23 28 views
0
//If the user uploaded an image, read it, and send it to the Vision API 
if (activity.Attachments.Any() && activity.Attachments.First().ContentType.Contains("image")) 
{ 
    //stores image url (parsed from attachment or mess`enter code here`age) 
    string uploadedImageUrl = activity.Attachments.First().ContentUrl; ; 
    uploadedImageUrl = HttpUtility.UrlDecode(uploadedImageUrl.Substring(uploadedImageUrl.IndexOf("file=") + 5)); 

    using (Stream imageFileStream = File.OpenRead(uploadedImageUrl)) 
    { 
     try 
     { 
      analysisResult = await visionClient.AnalyzeImageAsync(imageFileStream, visualFeatures); 
     } 
     catch (Exception e) 
     { 
      analysisResult = null; //on error, reset analysis result to null 
     } 
    } 
} 
//Else, if the user did not upload an image, determine if the message contains a url, and send it to the Vision API 
else 
{ 
    try 
    { 
     analysisResult = await visionClient.AnalyzeImageAsync(activity.Text, visualFeatures); 
    } 
    catch (Exception e) 
    { 
     analysisResult = null; //on error, reset analysis result to null 
    } 
} 

我正在嘗試上面的代碼。我從這裏得到了代碼:https://docs.botframework.com/en-us/bot-intelligence/vision/#example-vision-botVision API C# - 讀Azure中存儲的圖像的URL

就像這樣,代碼執行本地運行時的功能,但是在將我的機器人發佈到Azure並從此處運行後,從上載的文件中讀取圖像的URL不起作用。

我試圖通過將Visual Studio直接附加到我在Azure中發佈的webapp機器人進行調試。它看起來像web應用程序無法從Azure服務器臨時存儲位置讀取存儲的圖像的URL,或無法訪問臨時存儲位置。

這條線:

uploadedImageUrl = HttpUtility.UrlDecode(uploadedImageUrl.Substring(uploadedImageUrl.IndexOf("file=") + 5)); 

顯示該值: 「https://bcattachmentsprod.blob.core.windows.net/at4984/Gv4cOx6OdSl-原」

然後,這條線:

using (Stream imageFileStream = File.OpenRead(uploadedImageUrl)) 

值更改爲: 「S:// bcattachmentsprod.blob.core.windows.net/at4984/Gv4cOx6OdSl-original「

然後它就停下來。

有沒有人遇到過這樣的問題?我該如何着手解決這個問題?

謝謝!

回答

0

如果您有圖片網址,您應該簡單地使用該網址調用AnalyzeImageAsync,而不是像現在這樣創建一個流。 File類應該用於本地驅動器/網絡上的文件。通過創建一個流,您可以將圖像下載一次到您的虛擬機,然後將其上傳到視覺服務,使工作翻一番。

+0

在本地和Azure中發送圖像的URL可用於此片段。我想知道這個問題是否有解決方法。嗯... – kcd

+0

剩下的問題是什麼? – cthrash

相關問題