public void Run() { Console.WriteLine(@"Creating face image..."); SettingData.TotalFrameNum = 0; SettingData.CurrentFrameNum = 0; foreach (var videoName in SettingData.VideoFileNames) { using (var video = new VideoCapture(videoName)) { SettingData.TotalFrameNum += video.FrameCount; } } Cv2.NamedWindow("image", WindowMode.FreeRatio); foreach (var videoName in SettingData.VideoFileNames) { using (var video = VideoCapture.FromFile(videoName)) { var frameNum = 0; while (true) { using (var frame = video.RetrieveMat()) { if (frame.Empty() || IsExitStatus) { if (IsExitStatus) { Console.WriteLine(@"Cancel"); IsExitStatus = false; goto CANCEL; } break; } frameNum++; SettingData.CurrentFrameNum++; //Detecting every 10 frames because the number of images //increases too much when cutting out all frames if (frameNum % SettingData.FrameRateNum == 0) { DetectAndSaveImg(frame); } } } Console.WriteLine(@"End of video"); } } CANCEL: Cv2.DestroyAllWindows(); Cascade.Dispose(); }