【发布时间】:2017-03-27 16:54:31
【问题描述】:
我有一个 UWP 应用程序,用于捕获和处理来自相机的图像。该项目利用 Microsoft Cognitive Services Face Recognition API,我正在探索该应用程序的现有功能一段时间。我的目标是当一个人的图像被相机识别(通过人脸识别 API 服务)时,我想显示那个人的相关图像。
这样,图像就会被捕获并存储在我机器的本地目录中。一旦识别出此人,我想检索图像文件并将其呈现在屏幕上。
下面的代码展示了asyncTask方法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 类,但没有在 asyncTask 方法中使用。使用async会不会导致使用I/O操作时文件不返回?
对不起这个愚蠢的问题,但我真的不明白。
非常感谢任何帮助。
【问题讨论】:
-
您的应用程序是否有权读取该文件?它在异步方法中不起作用吗?
-
@VMAtm 在
async方法中不起作用。我想是的,我在我的 Visual Studio 中调试,我发现这个方法Directory.GetDirectories(directoryPath)也返回了一个空字符串Directory.GetFiles()。我创建了一个单独的控制台应用程序,只是为了检查我是否在使用 Directory 类时做错了,但在控制台应用程序中GetDirectories和GetFiles返回了文件夹的数组字符串。我认为这与asyncTask 操作有关。你知道如何解决这个问题吗? -
在 UWP 中你不能像在 var directoryPath = @"D:\PersonImages"; 中那样随机访问文件夹你需要文件夹选择器或使用 PictyreLibrary
标签: c# uwp async-await