我有一个UWP应用程序,用于捕获和处理来自摄像头的图像 . 该项目利用Microsoft认知服务人脸识别API,我现在正在探索应用程序的现有功能 . 我的目标是当相机识别人物的图像时(通过脸部识别API服务),我想显示该人的相关图像 .
这样,图像被捕获并存储在我的机器的本地目录中 . 我想检索图像文件并在识别出人物后在屏幕上呈现它 .
下面的代码显示 async
任务方法 ProcessCameraCapture
private async Task ProcessCameraCapture(ImageAnalyzer e)
{
if (e == null)
{
this.UpdateUIForNoFacesDetected();
this.isProcessingPhoto = false;
return;
}
DateTime start = DateTime.Now;
await e.DetectFacesAsync();
if (e.DetectedFaces.Any())
{
string names;
await e.IdentifyFacesAsync();
this.greetingTextBlock.Text = this.GetGreettingFromFaces(e, out names);
if (e.IdentifiedPersons.Any())
{
this.greetingTextBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.GreenYellow);
this.greetingSymbol.Foreground = new SolidColorBrush(Windows.UI.Colors.GreenYellow);
this.greetingSymbol.Symbol = Symbol.Comment;
GetSavedFilePhoto(names);
}
else
{
this.greetingTextBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.Yellow);
this.greetingSymbol.Foreground = new SolidColorBrush(Windows.UI.Colors.Yellow);
this.greetingSymbol.Symbol = Symbol.View;
}
}
else
{
this.UpdateUIForNoFacesDetected();
}
TimeSpan latency = DateTime.Now - start;
this.faceLantencyDebugText.Text = string.Format("Face API latency: {0}ms", (int)latency.TotalMilliseconds);
this.isProcessingPhoto = false;
}
在 GetSavedFilePhoto
中,一旦识别出人员,我就传递了字符串名称参数 .
以下代码为 GetSavedFilePhoto
方法
private void GetSavedFilePhoto(string personName)
{
if (string.IsNullOrWhiteSpace(personName)) return;
var directoryPath = @"D:\PersonImages";
var directories = Directory.GetDirectories(directoryPath);
var filePaths = Directory.GetFiles(directoryPath, "*.jpg", SearchOption.AllDirectories);
}
但是,在 GetSavedFilePhoto
方法中,变量 directories
在使用 directoryPath
字符串变量时返回了一个空的数组字符串 . 目录"D:\PersonImages"是我的计算机中的有效和现有文件夹,它包含内部带有图像的子文件夹 . 我也试过 Directory.GetFiles
来检索jpg图像,但仍然返回一个空字符串 .
我认为它应该工作,因为我多次使用 Directory
类但不在 async
任务方法中 . 使用 async
时是否导致在使用I / O操作时未返回文件?
抱歉这个愚蠢的问题,但我真的不明白 .
任何帮助是极大的赞赏 .
1 回答
使用Directory.GetFiles或Directory.GetDirectories方法可以通过以下代码获取Application的本地文件夹中的文件夹/文件 . 但它无法打开
D:\
.在UWP应用程序中,您只能访问默认的两个位置(本地文件夹和安装文件夹),其他需要功能设置或file open picker . 详细信息请参考file access permission .
如果您需要访问
D:\
中的所有文件,用户必须使用FolderPicker
手动选择D:\
驱动器,然后您有权访问此驱动器中的文件 .