While 循环让 Kairos 每 1 分钟捕获一次
While loop for Kairos to only capture every 1 minute
我目前正在使用名为 Kairos 的面部识别软件来分析视频中人群的情绪。
我的问题是,我不是在 while 中使用 "true"(它会每秒分析一次人群情绪),而是如何将其配置为每 1 分钟只分析一次人群?提前致谢。
HumanAnalysisService has = null;
try{
has = new HumanAnalysisService("license.xml", "", 20, 4);
} catch (ApplicationException lie) {
Console.WriteLine(lie.Message);
return;
}
// has = new HumanAnalysisService("license.xml", "", 20, 4);
/* attach to camera device */
// has.initUsingCameraSource(0);
has.initUsingImageSource(file1);
/* *loop thru the capture feed */
while (true) {
/* pull pull out the next frame */
has.pullFrame();
/* does the device have more frames */
if (has.isFrameEmpty())
break;
/* process the pulled frame */
has.processFrame();
/* get the people that are in the current frame*/
People people = has.getPeople();
System.Console.Write("Media Height: " + has.getMediaSourceHeight());
System.Console.Write("Media Width: " + has.getMediaSourceWidth());
System.Console.Write("Media Type: " + has.getMediaType());
System.Console.Write("Mime Type: " + has.getMediaContentType() + "\n\n");
/* print out the info from every person in te frame*/
// foreach ( Person person in people )
for (int i = 0; i < people.size(); i++) {
System.Console.Write("Person id" + people.get(i).id + " , face x coordinate: " + people.get(i).face.x + "\n");
System.Console.Write("Person id" + people.get(i).id + " , face y coordinate: " + people.get(i).face.x + "\n");
System.Console.Write("Person id" + people.get(i).id + " , face width coordinate: " + people.get(i).face.width + "\n");
System.Console.Write("Person id" + people.get(i).id + " , face height coordinate: " + people.get(i).face.height + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Joy: " + people.get(i).impression.emotion_response.joy_score + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Surprise: " + people.get(i).impression.emotion_response.surprise_score + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Anger: " + people.get(i).impression.emotion_response.anger_score + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Fear: " + people.get(i).impression.emotion_response.fear_score + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Sadness: " + people.get(i).impression.emotion_response.sadness_score + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Disgust: " + people.get(i).impression.emotion_response.disgust_score + "\n");
}
}
}
我建议使用 Timer
:
var timer = new System.Timers.Timer()
timer.Interval = 60000;
timer.Elapsed += (_s, _e) =>
{
/* pull pull out the next frame */
has.pullFrame();
/* does the device have more frames */
if (has.isFrameEmpty())
timer.Enabled = false;;
// REST OF YOUR LOOP CODE HERE
};
timer.Enabled = true;
或使用 Microsoft Reactive Framework:
IDisposable subscription =
Observable
.Interval(TimeSpan.FromMinutes(1.0))
.Do(x => has.pullFrame())
.TakeWhile(n => !has.isFrameEmpty())
.Do(x =>
{
/* process the pulled frame */
has.processFrame();
/* get the people that are in the current frame*/
People people = has.getPeople();
// REST OF YOUR LOOP CODE HERE
})
.Wait();
对于后一个选项,只需 NuGet "System.Reactive" 并将 using System.Reactive.Linq;
添加到您的代码中。
对于您的应用来说,这可能有点过头了,但请看一下 Quartz.Net
您可以简单地创建一个无限期重复的任务。
可以轻松测试的干净代码
[DisallowConcurrentExecution]
public class CaptureFeedFeedbackLoop : IJob
{
public static HumanAnalysisService Has;
public Task Execute(IJobExecutionContext context)
{
//Do stuff
return Task.CompletedTask;
}
}
然后
HumanAnalysisService has = null;
try
{
has = new HumanAnalysisService("license.xml", "", 20, 4);
}
catch (ApplicationException lie)
{
Console.WriteLine(lie.Message);
return;
}
// has = new HumanAnalysisService("license.xml", "", 20, 4);
/* attach to camera device */
// has.initUsingCameraSource(0);
has.initUsingImageSource(file1);
IJobDetail job = JobBuilder.Create<CaptureFeedFeedbackLoopJob>().WithIdentity("job1", "group1").Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(60)
.RepeatForever())
.Build();
await scheduler.ScheduleJob(job, trigger);
如果您需要同步版本的方法,您可以使用 Quartz.net 2.x
我目前正在使用名为 Kairos 的面部识别软件来分析视频中人群的情绪。
我的问题是,我不是在 while 中使用 "true"(它会每秒分析一次人群情绪),而是如何将其配置为每 1 分钟只分析一次人群?提前致谢。
HumanAnalysisService has = null;
try{
has = new HumanAnalysisService("license.xml", "", 20, 4);
} catch (ApplicationException lie) {
Console.WriteLine(lie.Message);
return;
}
// has = new HumanAnalysisService("license.xml", "", 20, 4);
/* attach to camera device */
// has.initUsingCameraSource(0);
has.initUsingImageSource(file1);
/* *loop thru the capture feed */
while (true) {
/* pull pull out the next frame */
has.pullFrame();
/* does the device have more frames */
if (has.isFrameEmpty())
break;
/* process the pulled frame */
has.processFrame();
/* get the people that are in the current frame*/
People people = has.getPeople();
System.Console.Write("Media Height: " + has.getMediaSourceHeight());
System.Console.Write("Media Width: " + has.getMediaSourceWidth());
System.Console.Write("Media Type: " + has.getMediaType());
System.Console.Write("Mime Type: " + has.getMediaContentType() + "\n\n");
/* print out the info from every person in te frame*/
// foreach ( Person person in people )
for (int i = 0; i < people.size(); i++) {
System.Console.Write("Person id" + people.get(i).id + " , face x coordinate: " + people.get(i).face.x + "\n");
System.Console.Write("Person id" + people.get(i).id + " , face y coordinate: " + people.get(i).face.x + "\n");
System.Console.Write("Person id" + people.get(i).id + " , face width coordinate: " + people.get(i).face.width + "\n");
System.Console.Write("Person id" + people.get(i).id + " , face height coordinate: " + people.get(i).face.height + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Joy: " + people.get(i).impression.emotion_response.joy_score + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Surprise: " + people.get(i).impression.emotion_response.surprise_score + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Anger: " + people.get(i).impression.emotion_response.anger_score + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Fear: " + people.get(i).impression.emotion_response.fear_score + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Sadness: " + people.get(i).impression.emotion_response.sadness_score + "\n");
System.Console.Write("Person id" + people.get(i).id + " , Emotion - Disgust: " + people.get(i).impression.emotion_response.disgust_score + "\n");
}
}
}
我建议使用 Timer
:
var timer = new System.Timers.Timer()
timer.Interval = 60000;
timer.Elapsed += (_s, _e) =>
{
/* pull pull out the next frame */
has.pullFrame();
/* does the device have more frames */
if (has.isFrameEmpty())
timer.Enabled = false;;
// REST OF YOUR LOOP CODE HERE
};
timer.Enabled = true;
或使用 Microsoft Reactive Framework:
IDisposable subscription =
Observable
.Interval(TimeSpan.FromMinutes(1.0))
.Do(x => has.pullFrame())
.TakeWhile(n => !has.isFrameEmpty())
.Do(x =>
{
/* process the pulled frame */
has.processFrame();
/* get the people that are in the current frame*/
People people = has.getPeople();
// REST OF YOUR LOOP CODE HERE
})
.Wait();
对于后一个选项,只需 NuGet "System.Reactive" 并将 using System.Reactive.Linq;
添加到您的代码中。
对于您的应用来说,这可能有点过头了,但请看一下 Quartz.Net 您可以简单地创建一个无限期重复的任务。 可以轻松测试的干净代码
[DisallowConcurrentExecution]
public class CaptureFeedFeedbackLoop : IJob
{
public static HumanAnalysisService Has;
public Task Execute(IJobExecutionContext context)
{
//Do stuff
return Task.CompletedTask;
}
}
然后
HumanAnalysisService has = null;
try
{
has = new HumanAnalysisService("license.xml", "", 20, 4);
}
catch (ApplicationException lie)
{
Console.WriteLine(lie.Message);
return;
}
// has = new HumanAnalysisService("license.xml", "", 20, 4);
/* attach to camera device */
// has.initUsingCameraSource(0);
has.initUsingImageSource(file1);
IJobDetail job = JobBuilder.Create<CaptureFeedFeedbackLoopJob>().WithIdentity("job1", "group1").Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(60)
.RepeatForever())
.Build();
await scheduler.ScheduleJob(job, trigger);
如果您需要同步版本的方法,您可以使用 Quartz.net 2.x