private static bool FaceRecognizeAndSaveResultsCommon(string saveDir, string filePahtPrefix, List <Tuple <string, RectangleF> > facesInfo) { // python実行 bool result; string output = AnimeFaceRecognizer.RunCPython("../python", "detect.py input.png", out result); if (false == result) { return(false); } // 結果をパースして、得られた顔領域で画像をトリミングして保存 var faces = AnimeFaceRecognizer.ParseFaces(output); int faceCnt = 0; foreach (var face in faces) { string savePath = System.IO.Path.Combine(saveDir, filePahtPrefix + (faceCnt++) + ".png"); facesInfo.Add(new Tuple <string, RectangleF>(savePath, face)); } return(true); }
/// <summary> /// スクショ+顔認識のバックグラウンド処理 /// </summary> private void screenshotBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; var args = e.Argument as ScreenshotBackgroundWorkArguments; if (args == null || args.Fps < 0.01f) { return; } float millisecPerFrame = 1000.0f / args.Fps; string saveDir = "../output_screenshot/" + DateTime.Now.ToString("yyyyMMdd_hhmmss"); string saveScreenshotDir = saveDir + "/screenshot"; saveDir = System.IO.Path.GetFullPath(saveDir); saveScreenshotDir = System.IO.Path.GetFullPath(saveScreenshotDir); var userState = new ScreenshotBackgroundWorkUserState() { ScreenshotCount = 0, ScreenshotTime = 0, RealFps = args.Fps, SaveDirectoryPath = saveDir, }; worker.ReportProgress(0, userState.ScreenshotCount); if (args.OptionFlags.HasFlag(ScreenshotOptionFlag.SaveScreenshot)) { if (System.IO.Directory.Exists(saveScreenshotDir) == false) { System.IO.Directory.CreateDirectory(saveScreenshotDir); } System.IO.File.Copy("../script/screenshot2faces.bat", System.IO.Path.Combine(saveDir, "screenshot2faces.bat")); } else { if (System.IO.Directory.Exists(saveDir) == false) { System.IO.Directory.CreateDirectory(saveDir); } } System.Diagnostics.Process.Start(saveDir); List <float> elapsedTimeHistory = new List <float>(); System.Diagnostics.Stopwatch fpsTimer = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch elapsedTimer = System.Diagnostics.Stopwatch.StartNew(); while (true) { fpsTimer.Restart(); // キャンセルされてないか定期的にチェック if (worker.CancellationPending) { e.Cancel = true; return; } var bmp = takeScreenshot(Screen.PrimaryScreen.Bounds); userState.ScreenshotTime = elapsedTimer.ElapsedMilliseconds; if (bmp != null) { // スクショを保存 if (args.OptionFlags.HasFlag(ScreenshotOptionFlag.SaveScreenshot)) { string path = System.IO.Path.Combine(saveScreenshotDir, args.Prefix + userState.ScreenshotTime + ".png"); bmp.Save(path); } // 顔認識して、顔部分を切り出して保存 if (args.OptionFlags.HasFlag(ScreenshotOptionFlag.FaceRecognition)) { string prefix = userState.ScreenshotTime + "-"; AnimeFaceRecognizer.FaceRecognizeAndSaveResults(bmp, saveDir, prefix); } } // FPS設定に合わせて、一定時間寝る int waitTime = (int)(millisecPerFrame - fpsTimer.ElapsedMilliseconds); waitTime = Math.Max(0, waitTime); System.Threading.Thread.Sleep(waitTime); // 撮影したスクショの枚数 userState.ScreenshotCount++; // fpsの計算 elapsedTimeHistory.Add(fpsTimer.ElapsedMilliseconds); if (elapsedTimeHistory.Sum() >= 1.0f) { userState.RealFps = 1000.0f / elapsedTimeHistory.Average(); elapsedTimeHistory.Clear(); } // 進捗報告 worker.ReportProgress(0, userState); } }
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; var arg = e.Argument as bgWorkArg; string filepath = arg.filepath; string saveDir = getDirectoryName(filepath); worker.ReportProgress(0); using (var capture = new OpenCvSharp.CvCapture(filepath)) { if (capture == null) { return; } if (System.IO.Directory.Exists(saveDir) == false) { System.IO.Directory.CreateDirectory(saveDir); } System.Diagnostics.Process.Start(saveDir); capture.PosFrames = arg.startFrame; int prevProgress = -1; while (true) { // キャンセルされてないか定期的にチェック if (worker.CancellationPending) { e.Cancel = true; return; } // 画像取得→顔認識 using (var cap = capture.RetrieveFrame()) { if (cap == null) { e.Result = true; return; } string prefix = capture.PosFrames + "-"; AnimeFaceRecognizer.FaceRecognizeAndSaveResults(cap, saveDir, prefix); } // 終了判定 if (capture.FrameCount - 1 <= capture.PosFrames + arg.skipFrames) { worker.ReportProgress(100); e.Result = true; return; } capture.PosFrames = capture.PosFrames + arg.skipFrames; // プログレス更新 int progress = (int)(100.0f * capture.PosFrames / capture.FrameCount); if (prevProgress != progress) { worker.ReportProgress(progress); } prevProgress = progress; } } }