我正在使用 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。找不到任何解决方案,我希望有人能回答。谢谢你们
谢谢