首页 > 解决方案 > Bot 中的 MS Bot c# Custom Vision 预测端点错误“不支持给定路径的格式。”

问题描述

创建了一个简单的项目来演示自定义视觉对象检测。一切运作良好。但是当我通过 Bot 聊天附加图像时,问题就出现了,它抛出了 Exemption 说

The given path’s format is not supported...
at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare

这是完整的代码...

 public class Vision:IDialog<Object>
 {
     Guid ProjectId = Guid.Empty;

     const string PredictionKey = "<MY KEY>";

     public Task StartAsync(IDialogContext context)
     {
         context.Wait(MessageReceivedAsync);

         return Task.CompletedTask;
     }

     private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
     {
         await context.PostAsync("Hello there. Nice to meet you!");
         context.Wait(ResumeAfterOperationSelecting);

     }

     private async Task ResumeAfterOperationSelecting(IDialogContext context, IAwaitable<object> result)
     {
         PromptDialog.Attachment(
             context: context,
             prompt: "Upload Image to perform operation",
             resume: ResumeAfterRecievingAttachment
         );
     }

     private async Task ResumeAfterRecievingAttachment(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
     {
         var images = await result;

         foreach (var image in images)
         {
             ProjectId = new Guid("<MY PROJECT ID>");

             PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = PredictionKey };

             var results = endpoint.PredictImage(ProjectId, File.OpenRead(image.ContentUrl));

             foreach (var c in results.Predictions)
             {
                 await context.PostAsync($"{c.TagName}, {c.Probability}");
             }

         }
         await context.PostAsync("Hello there. Have done!");

         context.Wait(MessageReceivedAsync);
     }

}

所以错误发生在这一行

var results = endpoint.PredictImage(ProjectId, File.OpenRead(image.ContentUrl));

这是完整的豁免信息

 The given path’s format is not supported.     
 at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String  fullPath)
 at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
 at System.IO.FileStream…ctor(String path, FileMode mode, FileAccess access, FileShare share)
 at SimpleEchoBot.Dialogs.Vision.<ResumeAfterRecievingAttachment>d__5.MoveNext()
 in C:\Users\AbdiHakim\Music\reres-src\Dialogs\Vision.cs:line 55
 — End of stack trace from previous location where exception was thrown —
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.ThunkResume1.<Rest>d__5.MoveNext()
 in D:\a\1\s\CSharp\Library\Microsoft.Bot.Builder\Dialogs\DialogTask.cs:line 164 --- End of stack trace from previous location where exception was thrown --- 
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
 at Microsoft.Bot.Builder.Internals.Fibers.Wait2.<Microsoft-Bot-Builder-Internals-Fibers-IWait<C>-PollAsync>d__19.MoveNext()
 in D:\a\1\s\CSharp\Library\Microsoft.Bot.Builder\Fibers\…

我该怎么处理这条线。因为根据我的观点,Microsoft Bot Framework 可能不允许 System.IO 重新生成文件图像附件。如果是这样,我该如何实现它,以便预测点 URL 可以读取机器人附件的图像 URL。

NOTE: Prediction endpoint requires the method to have format like this..

端点.预测(,)

Json Bot Framework Emulator 上的输出是这样的

 "type": "message",
  "attachments": [
    {
      "contentType": "image/jpeg",
      "contentUrl": "blob:file:///a3f2d3e1-e295-46fe-b434-b05f3905301c",
      "name": "11.jpg"
    }
  ],

标签: c#botframework

解决方案


我不知道您的 URL 也指向什么类型的文件(Azure blob 存储?)。消息很清楚- File.OpenRead 不支持您尝试访问的文件类型blob:file://

https://msdn.microsoft.com/en-us/library/system.io.file.openread(v=vs.110).aspx

以下是来自 MSDN 的可接受路径列表:

在接受路径的成员中,路径可以引用文件或仅引用目录。指定的路径还可以指服务器和共享名称的相对路径或通用命名约定 (UNC) 路径。例如,以下所有路径都是可接受的路径:

C# 中的“c:\MyDir\MyFile.txt”,或 Visual Basic 中的“c:\MyDir\MyFile.txt”。

C# 中的“c:\MyDir”或 Visual Basic 中的“c:\MyDir”。

C# 中的“MyDir\MySubdir”或 Visual Basic 中的“MyDir\MySubDir”。

C# 中的“\\MyServer\MyShare”或 Visual Basic 中的“\MyServer\MyShare”。


推荐阅读