C# UWP MediaFrameReader-System.ArgumentException:&x27;值不在预期范围内;
我正在尝试创建一个UWP应用程序,该应用程序使用笔记本电脑的网络摄像头来处理媒体帧,我希望在其上使用Microsoft Azure Face API搜索人脸 我创建了一个运行良好的MediaCapture和一个MediaFrameReader,它抛出了问题标题中提到的异常 您可以在此处看到我的代码:C# UWP MediaFrameReader-System.ArgumentException:&x27;值不在预期范围内;,c#,uwp,argumentexception,mediacapture,C#,Uwp,Argumentexception,Mediacapture,我正在尝试创建一个UWP应用程序,该应用程序使用笔记本电脑的网络摄像头来处理媒体帧,我希望在其上使用Microsoft Azure Face API搜索人脸 我创建了一个运行良好的MediaCapture和一个MediaFrameReader,它抛出了问题标题中提到的异常 您可以在此处看到我的代码: private async void StartDetection() { mediaStream = new InMemoryRandomAccessStr
private async void StartDetection()
{
mediaStream = new InMemoryRandomAccessStream();
MediaEncodingProfile encodingProfile = MediaEncodingProfile.CreateMp4(encodingQuality);
await mediaCapture.StartRecordToStreamAsync(encodingProfile, mediaStream);
var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();
MediaFrameSourceGroup selectedGroup = null;
MediaFrameSourceInfo colorSourceInfo = null;
foreach (var sourceGroup in frameSourceGroups)
{
foreach (var sourceInfo in sourceGroup.SourceInfos)
{
if (sourceInfo.MediaStreamType == MediaStreamType.VideoRecord
&& sourceInfo.SourceKind == MediaFrameSourceKind.Color)
{
colorSourceInfo = sourceInfo;
break;
}
}
if (colorSourceInfo != null)
{
selectedGroup = sourceGroup;
break;
}
}
var selectedGroupObjects = frameSourceGroups.Select(group =>
new
{
sourceGroup = group,
colorSourceInfo = group.SourceInfos.FirstOrDefault((sourceInfo) =>
{
// On Xbox/Kinect, omit the MediaStreamType and EnclosureLocation tests
return sourceInfo.MediaStreamType == MediaStreamType.VideoRecord
&& sourceInfo.SourceKind == MediaFrameSourceKind.Color
&& sourceInfo.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front;
})
}).Where(t => t.colorSourceInfo != null).FirstOrDefault();
selectedGroup = selectedGroupObjects?.sourceGroup;
colorSourceInfo = selectedGroupObjects?.colorSourceInfo;
if (selectedGroup == null)
{
return;
}
var allGroups = await MediaFrameSourceGroup.FindAllAsync();
var eligibleGroups = allGroups.Select(g => new
{
Group = g,
SourceInfos = new MediaFrameSourceInfo[]
{
g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Color),
g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Depth),
g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Infrared),
}
}).Where(g => g.SourceInfos.Any(info => info != null)).ToList();
if (eligibleGroups.Count == 0)
{
System.Diagnostics.Debug.WriteLine("No source group with color, depth or infrared found.");
return;
}
var selectedGroupIndex = 0;
selectedGroup = eligibleGroups[selectedGroupIndex].Group;
colorSourceInfo = eligibleGroups[selectedGroupIndex].SourceInfos[0];
MediaFrameSourceInfo infraredSourceInfo = eligibleGroups[selectedGroupIndex].SourceInfos[1];
MediaFrameSourceInfo depthSourceInfo = eligibleGroups[selectedGroupIndex].SourceInfos[2];
var colorFrameSource = mediaCapture.FrameSources[colorSourceInfo.Id];
var preferredFormat = colorFrameSource.SupportedFormats.Where(format =>
{
return format.VideoFormat.Width >= 1080
&& format.Subtype == "RGB24";
}).FirstOrDefault();
if (preferredFormat == null)
{
return;
}
await colorFrameSource.SetFormatAsync(preferredFormat);
MediaFrameReader mediaFrameReader;
mediaFrameReader = await mediaCapture.CreateFrameReaderAsync(colorFrameSource, preferredFormat.Subtype);
mediaFrameReader.FrameArrived += CheckFrameForFaces;
await mediaFrameReader.StartAsync();
}
private async void CheckFrameForFaces(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
{
var mediaFrame = sender.TryAcquireLatestFrame(); // -> this is the line that throws the exception
var videoFrame = mediaFrame?.VideoMediaFrame;
var bitmap = videoFrame?.SoftwareBitmap;
WriteableBitmap wb = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
bitmap.CopyToBuffer(wb.PixelBuffer);
var img = Dlib.LoadImageData<RgbPixel>(wb.PixelBuffer.ToArray(), (uint)wb.PixelHeight, (uint)wb.PixelWidth, 3);
ImageWindow im = new ImageWindow(img);
im.WaitUntilClosed();
}
private async void StartDetection()
{
mediaStream=新的InMemoryRandomAccessStream();
MediaEncodingProfile encodingProfile=MediaEncodingProfile.CreateMp4(编码质量);
等待mediaCapture.StartRecordToStreamAsync(编码配置文件,mediaStream);
var frameSourceGroups=wait MediaFrameSourceGroup.findalsync();
MediaFrameSourceGroup selectedGroup=null;
MediaFrameSourceInfo colorSourceInfo=null;
foreach(frameSourceGroups中的var sourceGroup)
{
foreach(sourceGroup.SourceInfos中的var sourceInfo)
{
如果(sourceInfo.MediaStreamType==MediaStreamType.VideoRecord
&&sourceInfo.SourceKind==MediaFrameSourceKind.Color)
{
colorSourceInfo=sourceInfo;
打破
}
}
if(colorSourceInfo!=null)
{
selectedGroup=sourceGroup;
打破
}
}
var selectedGroupObjects=frameSourceGroups.Select(group=>
新的
{
sourceGroup=group,
colorSourceInfo=group.SourceInfos.FirstOrDefault((sourceInfo)=>
{
//在Xbox/Kinect上,省略MediaStreamType和EnclosureLocation测试
return sourceInfo.MediaStreamType==MediaStreamType.VideoRecord
&&sourceInfo.SourceKind==MediaFrameSourceKind.Color
&&sourceInfo.DeviceInformation?.EnclosureLocation.Panel==Windows.Devices.Enumeration.Panel.Front;
})
}).Where(t=>t.colorSourceInfo!=null).FirstOrDefault();
selectedGroup=selectedGroupObjects?.sourceGroup;
colorSourceInfo=selectedGroupObjects?.colorSourceInfo;
如果(selectedGroup==null)
{
返回;
}
var allGroups=wait MediaFrameSourceGroup.findalsync();
var eligibleGroups=allGroups.Select(g=>new
{
组=g,
SourceInfos=新的MediaFrameSourceInfo[]
{
g、 SourceInfos.FirstOrDefault(info=>info.SourceKind==MediaFrameSourceKind.Color),
g、 SourceInfos.FirstOrDefault(info=>info.SourceKind==MediaFrameSourceKind.Depth),
g、 SourceInfos.FirstOrDefault(info=>info.SourceKind==MediaFrameSourceKind.Infrared),
}
}).Where(g=>g.SourceInfos.Any(info=>info!=null)).ToList();
如果(eligibleGroups.Count==0)
{
System.Diagnostics.Debug.WriteLine(“未找到颜色、深度或红外线的源组”);
返回;
}
var selectedGroupIndex=0;
selectedGroup=eligibleGroups[selectedGroupIndex]。组;
colorSourceInfo=eligibleGroups[selectedGroupIndex].SourceInfos[0];
MediaFrameSourceInfo infraredSourceInfo=eligibleGroups[selectedGroupIndex].SourceInfos[1];
MediaFrameSourceInfo depthSourceInfo=eligibleGroups[selectedGroupIndex].SourceInfos[2];
var colorFrameSource=mediaCapture.FrameSources[colorSourceInfo.Id];
var preferredFormat=colorFrameSource.SupportedFormats.Where(格式=>
{
返回格式.VideoFormat.Width>=1080
&&format.Subtype==“RGB24”;
}).FirstOrDefault();
if(preferredFormat==null)
{
返回;
}
等待colorFrameSource.SetFormatAsync(preferredFormat);
MediaFrameReader;
mediaFrameReader=等待mediaCapture.CreateFrameReaderAsync(colorFrameSource,preferredFormat.Subtype);
mediaFrameReader.FrameArrized+=CheckFrameForFaces;
等待mediaFrameReader.StartAsync();
}
专用异步void CheckFrameForFaces(MediaFrameReader发送方、MediaFrameArrivedEventArgs参数)
{
var mediaFrame=sender.TryAcquireLatestFrame();//->这是引发异常的行
var videoFrame=mediaFrame?.VideoMediaFrame;
var位图=视频帧?软件位图;
WriteableBitmap wb=新的WriteableBitmap(bitmap.PixelWidth,bitmap.PixelHeight);
CopyToBuffer(wb.PixelBuffer);
var img=Dlib.LoadImageData(wb.PixelBuffer.ToArray(),(uint)wb.PixelHeight,(uint)wb.PixelWidth,3);
ImageWindow im=新的ImageWindow(img);
im.waituntlclosed();
}
顺便说一下,此代码是根据Microsoft文档编写的,可以在以下位置找到:
我唯一更改的是MediaEncodingSubtype,因为它不适用于Argb32,所以我使用Rgb24(我检查了计算机中所有支持的格式)。我认为这个问题来自于这个性质。当我使用子类型Argb32初始化MediaFrameReader对象时(如您在文档示例中所看到的),我没有得到任何异常,尽管SoftwareBitmap将为null。另一方面,如果我使用Rgb24(因为preferredFormat有这个子类型),我会得到异常
我可以显示异常的一些详细信息:
System.ArgumentException
HResult=0x80070057
Message=Value does not fall within the expected range.
Source=Windows
StackTrace:
at Windows.Media.Capture.Frames.MediaFrameReader.TryAcquireLatestFrame()
at FaceAPI.MainPage.<CheckFrameForFaces>d__17.MoveNext() in P:\Dokumentumok\Suli\BME-VIK_6\Klitech\Házi feladat\KlitechHF\FaceAPI\FaceAPI\MainPage.xaml.cs:line 217
System.ArgumentException
HResult=0x80070057
消息=值不在wi范围内