【问题标题】:Microsoft.ProjectOxford.Face. AddFaceToLargeFaceListAsync from FTP with MemoryStreamMicrosoft.ProjectOxford.Face。来自 FTP 的 AddFaceToLargeFaceListAsync 与 MemoryStream
【发布时间】:2018-05-30 07:58:02
【问题描述】:

我正在使用 Microsoft FaceAPI 的 WS。 使用 FileStream 时一切正常,但我必须从 FTP 读取图像,所以我使用的是 MemoryStream。 我总是以这个例外结束:

{Microsoft.ProjectOxford.Face.FaceAPIException: Image size is too small.(InvalidImageSize)

这是我创建 MemoryStream 的方法。对吗??

 public MemoryStream GetFileStream(string filePath, string userName, string password)
    {

        Console.WriteLine($"getting file stream : {filePath}");
        MemoryStream ftpStream = null;
        try
        {
            var fileName = Guid.NewGuid().ToString();

            FtpWebRequest request = WebRequest.Create(filePath) as FtpWebRequest;
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential(userName, password);

            ftpStream = new MemoryStream();
            WebResponse response = request.GetResponse();
            Stream responseStream = response.GetResponseStream();

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                ftpStream.Write(buffer, 0, bytesRead);
            }
            responseStream.Close();

        }
        catch (Exception ex)
        {
            Console.WriteLine($"getting file stream : {filePath} " + ex.ToString());
        }
        return ftpStream;

    }

这是我调用 FaceAPI 的 WS 的任务

  using (MemoryStream fStream = ftpService.GetFileStream(imgPath, customerConfig.FTPUserName, customerConfig.FTPPassword))
                        {
                            try
                            {
                                if (fStream != null)
                                {
                                    Console.WriteLine("fStream Length : " + imgPath + " {" + fStream.Length + "}");
                                    var faces = await faceServiceClient.AddFaceToLargeFaceListAsync(listId, fStream); //==>> GET ME The EXCEPTION
                                    if (fStream != null)
                                    {
                                        fStream.Close();
                                    }
                                    if (urlFaceID.ContainsKey(imgPath) == false)
                                        urlFaceID.Add(imgPath, faces.PersistedFaceId.ToString());
                                }

                            }
                            catch (FaceAPIException ex)
                            {

                                if (ex.ErrorCode.Equals("ConcurrentOperationConflict"))
                                {
                                    imageList.Add(imgPath);

                                }

                                else if (ex.ErrorCode.Equals("RateLimitExceeded"))
                                {
                                    imageList.Add(imgPath);
                                }
                                else if (ex.ErrorMessage.Contains("more than 1 face in the image."))
                                {
                                    Console.WriteLine("more than 1 face in the image.");
                                }
                                if (InvalidUrlFaceID.ContainsKey(imgPath) == false)
                                {
                                    InvalidUrlFaceID.Add(imgPath, null);
                                }

                            }
                            finally
                            {
                                if (fStream != null)
                                {
                                    fStream.Close();
                                }
                            }
                        }

我不知道我是否正确阅读 MemoryStream。 找不到任何解决方案 我希望有人会回答。 谢谢大家

谢谢

【问题讨论】:

    标签: c# .net microsoft-cognitive face-api


    【解决方案1】:

    它看起来基本正确,但是您忘记了无论何时写入,MemoryStream 的读取和写入指针都是高级的。换句话说,GetFileStream 的返回值已经指向您编写的流的末尾。您需要更改的只是在返回之前“倒回”流。

    public MemoryStream GetFileStream(string filePath, string userName, string password)
    {
        Console.WriteLine($"getting file stream : {filePath}");
        MemoryStream ftpStream = null;
        try
        {
            var fileName = Guid.NewGuid().ToString();
    
            FtpWebRequest request = WebRequest.Create(filePath) as FtpWebRequest;
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential(userName, password);
    
            ftpStream = new MemoryStream();
            ...
            ftpStream.Position = 0; // <-- ADD THIS LINE
        }
        catch (Exception ex)
        {
            Console.WriteLine($"getting file stream : {filePath} " + ex.ToString());
        }
    
        return ftpStream;
    }
    

    【讨论】:

    • 感谢您的回复。但是当我调试 ftpStream 时,我发现 ftpStream.Position 已经等于 0。
    • ftpStream.Length的值是多少?
    • 是文件大小的值 例如435515
    猜你喜欢
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2016-09-19
    • 2020-06-30
    • 2014-08-25
    • 1970-01-01
    相关资源
    最近更新 更多