private bool Validatation() { // validate empty path if (string.IsNullOrEmpty(targetVideoPathTextBox.Text)) { MessageSender.ErrorMessage("Provide target video path"); return(false); } if (string.IsNullOrEmpty(reactionVideoPathTextBox.Text)) { MessageSender.ErrorMessage("Provide reaction video path"); return(false); } // validate expected result input var lastRow = videoIntervalGrid.Rows[videoIntervalGrid.Rows.Count - 1]; var inputFile = new MediaFile { Filename = targetVideoPathTextBox.Text }; using (var engine = new Engine()) { engine.GetMetadata(inputFile); } var totalSec = inputFile.Metadata.Duration.TotalSeconds; if (string.IsNullOrEmpty(lastRow.Cells[1].Value.ToString())) { lastRow.Cells[1].Value = totalSec; } return(true); }
public async Task <string> Process(string imgPath) { IList <DetectedFace> facesResult; // ImageAnalysis visionResult = null; try { Image image; using (var stream = File.Open(imgPath, FileMode.Open)) { image = Image.FromStream(stream); } // проверка (Emgu, Виола-Джонс) есть ли лицо var haveFaces = FaceDetector.ContainsFaces(new Bitmap(image)); if (!haveFaces) { return("No attention"); } // анализ лица var face = new FaceService.Create() .WithSubscriptionKey(AzureConstants.FaceSubscriptionKey) .WithEndpoint(AzureConstants.FaceEndpoint) .Get().Init(); facesResult = await face.AnalyzeFaceLocalAsync(imgPath); } catch (Exception ex) { MessageSender.ErrorMessage(ex.Message); return(null); } /* * // анализа изображения * var computeVision = new VisionService.Create() * .WithSubscriptionKey(AzureConstants.VisionSubscriptionKey) * .WithEndpoint(AzureConstants.VisionUriBase) * .Get().Init(); * * visionResult = await computeVision.AnalyzeLocalAsync(imgPath); */ var dictionary = new Dictionary <string, double> { { "Anger", facesResult?.First().FaceAttributes.Emotion.Anger ?? 0 }, { "Contempt", facesResult?.First().FaceAttributes.Emotion.Contempt ?? 0 }, { "Disgust", facesResult?.First().FaceAttributes.Emotion.Disgust ?? 0 }, { "Fear", facesResult?.First().FaceAttributes.Emotion.Fear ?? 0 }, { "Happiness", facesResult?.First().FaceAttributes.Emotion.Happiness ?? 0 }, { "Neutral", facesResult?.First().FaceAttributes.Emotion.Neutral ?? 0 }, { "Sadness", facesResult?.First().FaceAttributes.Emotion.Sadness ?? 0 }, { "Surprise", facesResult?.First().FaceAttributes.Emotion.Surprise ?? 0 } }; // todo handle gender // var gender = facesResult?.First().FaceAttributes.Gender.ToString(); // todo revise whether we need it // var smile = facesResult?.First().FaceAttributes.Smile.ToString(); return(dictionary.Aggregate((x, y) => x.Value > y.Value ? x : y).Key); }
private async Task Extracting() { var inputFile = new MediaFile { Filename = targetVideoPathTextBox.Text }; using (var engine = new Engine()) { engine.GetMetadata(inputFile); } var totalSec = inputFile.Metadata.Duration.TotalSeconds; // validate grid input foreach (DataGridViewRow row in videoIntervalGrid.Rows) { if ( string.IsNullOrEmpty(row.Cells[0].Value.ToString()) || string.IsNullOrEmpty(row.Cells[1].Value.ToString()) || string.IsNullOrEmpty(row.Cells[2].Value.ToString()) ) { MessageSender.ErrorMessage("You must fill expected grid"); return; } } // save expected result foreach (DataGridViewRow row in videoIntervalGrid.Rows) { var from = int.Parse(row.Cells[0].Value.ToString()); var to = int.Parse(row.Cells[1].Value.ToString()); var emotion = row.Cells[2].Value.ToString(); var expectedResultWorker = new ExpectedResultWorker(); expectedResultWorker.Create(this.projectId, from, to, emotion); } // store actual result for (var i = 0; i < totalSec; i++) { var path = Path.Combine(Directory.GetCurrentDirectory(), "frames"); var fullPath = $"{path}/{fileName}{i}.jpeg"; var emotionExtracter = new ExtractEmotionFromPicture(); var emotion = await emotionExtracter.Process(fullPath); // встановлюємо час очікування в секунду Thread.Sleep(1000); // error handling if (emotion == null) { continue; } var actualResultWorker = new ActualResultWorker(); actualResultWorker.Create(emotion, this.projectId, i); logTextBox.AppendText($"{DateTime.Now} image processing, obtained emotion {emotion}{Environment.NewLine}"); } }