Vision API C# - 读取存储在 Azure 中的图像 URL
Vision API C# - reading stored image's URL in Azure
//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-bot.
照原样,代码在本地执行 运行 时可以执行的操作,但是在我将机器人发布到 Azure 并且 运行 从那里开始。
我试图通过将 Visual Studio 直接附加到我在 Azure 中发布的 webapp 机器人来进行调试。看起来 webapp 无法从 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"
然后它就停止了。
有人 运行 遇到过这样的问题吗?我该如何解决这个问题?
谢谢!
如果您有一个图像 URL,您应该简单地用那个 URL 调用 AnalyzeImageAsync
,而不是像您那样创建一个流。 File
class 应该用于本地 drive/network 上的文件。通过创建流,您可以将图像下载到您的 VM 一次,然后将其上传到视觉服务,工作量加倍。
//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-bot.
照原样,代码在本地执行 运行 时可以执行的操作,但是在我将机器人发布到 Azure 并且 运行 从那里开始。
我试图通过将 Visual Studio 直接附加到我在 Azure 中发布的 webapp 机器人来进行调试。看起来 webapp 无法从 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"
然后它就停止了。
有人 运行 遇到过这样的问题吗?我该如何解决这个问题?
谢谢!
如果您有一个图像 URL,您应该简单地用那个 URL 调用 AnalyzeImageAsync
,而不是像您那样创建一个流。 File
class 应该用于本地 drive/network 上的文件。通过创建流,您可以将图像下载到您的 VM 一次,然后将其上传到视觉服务,工作量加倍。