Stream.readTimeout 抛出了一个类型为"System.InvalidOperationException"的异常



我正在使用OpenCV来检测人脸并将人脸从图像框中裁剪出来。需要将裁剪后的图像发送到 Azure,以便通过人脸 API 进行面部识别。 人脸 API 库 (Microsoft.CognitiveServices.Face( 有一个用于流的检测函数和一个用于 URL 的检测函数。我选择了 Stream,因为我不想先上传图像(性能(。

我的代码的以下部分在检测到一张(或多张(人脸时运行。检测到的人脸图像是一个垫子。我正在尝试将此垫转换为流,以便我可以将其发送到 Azure。

我根本没有收到任何错误,但是一旦我进入byte[] imageByte,应用程序就会停止工作。添加断点后,我注意到对于 imageStream 流,它显示:

stream.readtimeout 抛出类型异常 '系统无效操作异常'"。

所以我假设它以某种方式无法读取流,但我不明白为什么。我检查了 byte[] imageByte,它确实有一个值(不为 null(,因此该部分似乎可以工作。

{
try
{
Console.WriteLine(faceRect.ToString());
var detectedFaceImage = new Mat(frame, faceRect);
byte[] imageByte = detectedFaceImage.ToBytes();
Stream imageStream = new MemoryStream(imageByte);
await GetFaceId(imageStream);
}
catch(Exception e)
{
Console.WriteLine(e);
throw e;
}
}

System.Threading.Thread.Sleep(5000);
Console.WriteLine(faces);
}
}
}

它在捕获时不做任何事情。

我想如果您在IDE 中签出 CLR 异常,您可以看到更详细的异常消息。我知道这很明显,但是您的传入字节数组:)是错误的。也许imageStream.Position = 0;可以解决它,但我不确定。我需要更详细的异常消息。

相关内容

最新更新