public void LoadPhoto() { using (var ofd = new OpenFileDialogEx("Select template file", "Image Files|*.jpg;*.png;*.jpeg;*.bmp")) { ofd.Multiselect = false; if (ofd.ShowDialog() != DialogResult.OK) { return; } templateImage = ofd.FileName; Recognizer = new LuxandFaceRecognition(); if (!Recognizer.Recognize(ref templateImage, true)) { templateImage = string.Empty; pictureTemplate.Image = null; return; // это ОЧЕНЬ! важно. потому что мы во время распознавания можем создать обрезанную фотку и использовать ее как основную в проекте. } using (var ms = new MemoryStream(File.ReadAllBytes(templateImage))) // Don't use using!! { var img = (Bitmap)Image.FromStream(ms); pictureTemplate.Image = (Bitmap)img.Clone(); img.Dispose(); } RecalcRealTemplateImagePosition(); var eWidth = pictureTemplate.Width - 100; var TopEdgeTransformed = new RectangleF(pictureTemplate.Width / 2f - eWidth / 2f, 30, eWidth, eWidth); // затычка. нужен будет подгон верхней части бошки - сделаю var minX = Recognizer.GetMinX(); var topPoint = (TopEdgeTransformed.Y - ImageTemplateOffsetY) / ImageTemplateHeight; Recognizer.FaceRectRelative = new RectangleF(minX, topPoint, Recognizer.GetMaxX() - minX, Recognizer.BottomFace.Y - topPoint); var noseTip = Recognizer.FacialFeatures[2].Xy; var noseTop = Recognizer.FacialFeatures[22].Xy; var noseBottom = Recognizer.FacialFeatures[49].Xy; ProgramCore.MainForm.RenderControl.PhotoLoaded(Recognizer, TemplateImage); RenderTimer.Start(); if (Math.Abs(Recognizer.RotatedAngle) > 20) { MessageBox.Show("The head rotated more than 20 degrees. Please select an other photo..."); } } }
public void CreateProject() { #region Корректируем размер фотки var realTemplateImage = templateImage; using (var ms = new MemoryStream(File.ReadAllBytes(templateImage))) // Don't use using!! { var img = (Bitmap)Image.FromStream(ms); var max = (float)Math.Max(img.Width, img.Height); if (max != SelectedSize) { var k = SelectedSize / max; var newImg = ImageEx.ResizeImage(img, new Size((int)Math.Round(img.Width * k), (int)Math.Round((img.Height * k)))); templateImage = UserConfig.AppDataDir; FolderEx.CreateDirectory(templateImage); templateImage = Path.Combine(templateImage, "tempProjectImage.jpg"); newImg.Save(templateImage, ImageFormat.Jpeg); } } #endregion var path = string.Empty; if (string.IsNullOrEmpty(textNewProjectFolder.Text)) { path = UserConfig.AppDataDir; path = Path.Combine(path, "TempProject"); FolderEx.CreateDirectory(path, true); } else { path = textNewProjectFolder.Text; } var projectName = string.IsNullOrEmpty(textNewProjectName.Text) ? "HeadShop project" : textNewProjectName.Text; ProgramCore.Project = new Project(projectName, path, templateImage, GenType, ManType, CustomModelPath, true, SelectedSize, fcr.IsOpenSmile); ProgramCore.Project.RealTemplateImage = realTemplateImage; ProgramCore.Project.FacialFeatures = fcr.FacialFeatures; ProgramCore.Project.ImageRealPoints = fcr.RealPoints; ProgramCore.Project.LoadMeshes(); var minX = fcr.GetMinX(); var topPoint = (TopEdgeTransformed.Y - ImageTemplateOffsetY) / ImageTemplateHeight; ProgramCore.Project.TopPoint.X = 0.0f; ProgramCore.Project.TopPoint.Y = topPoint; ProgramCore.Project.FaceRectRelative = new RectangleF(minX, topPoint, fcr.GetMaxX() - minX, fcr.BottomFace.Y - topPoint); ProgramCore.Project.MouthCenter = new Vector2(fcr.LeftMouth.X + (fcr.RightMouth.X - fcr.LeftMouth.X) * 0.5f, fcr.LeftMouth.Y + (fcr.RightMouth.Y - fcr.LeftMouth.Y) * 0.5f); ProgramCore.Project.LeftEyeCenter = fcr.LeftEyeCenter; ProgramCore.Project.RightEyeCenter = fcr.RightEyeCenter; ProgramCore.Project.FaceColor = fcr.FaceColor; ProgramCore.Project.DetectedLipsPoints.Clear(); ProgramCore.Project.DetectedNosePoints.Clear(); ProgramCore.Project.DetectedBottomPoints.Clear(); ProgramCore.Project.DetectedTopPoints.Clear(); ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[3]); // точки рта ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[58]); ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[55]); ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[59]); ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[4]); ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[57]); ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[56]); ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[61]); //Центр рта верх ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[64]); //Центр рта низ ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[60]); //9 ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[62]); //10 ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[63]); //11 ProgramCore.Project.DetectedLipsPoints.Add(fcr.FacialFeatures[65]); //12 ProgramCore.Project.DetectedNosePoints.Add(fcr.FacialFeatures[45]); // точки носа ProgramCore.Project.DetectedNosePoints.Add(fcr.FacialFeatures[46]); ProgramCore.Project.DetectedNosePoints.Add(fcr.FacialFeatures[2]); ProgramCore.Project.DetectedNosePoints.Add(fcr.FacialFeatures[22]); ProgramCore.Project.DetectedNosePoints.Add(fcr.FacialFeatures[49]); ProgramCore.Project.DetectedLeftEyePoints.Add(fcr.FacialFeatures[23]); //Точки левого глаза ProgramCore.Project.DetectedLeftEyePoints.Add(fcr.FacialFeatures[28]); ProgramCore.Project.DetectedLeftEyePoints.Add(fcr.FacialFeatures[24]); ProgramCore.Project.DetectedLeftEyePoints.Add(fcr.FacialFeatures[27]); ProgramCore.Project.DetectedRightEyePoints.Add(fcr.FacialFeatures[25]); //Точки правого глаза ProgramCore.Project.DetectedRightEyePoints.Add(fcr.FacialFeatures[32]); ProgramCore.Project.DetectedRightEyePoints.Add(fcr.FacialFeatures[26]); ProgramCore.Project.DetectedRightEyePoints.Add(fcr.FacialFeatures[31]); ProgramCore.Project.DetectedBottomPoints.Add(fcr.FacialFeatures[5]); //точки нижней части лица ProgramCore.Project.DetectedBottomPoints.Add(fcr.FacialFeatures[7]); // * 0.75f + fcr.FacialFeatures[9] * 0.25f); var p11 = fcr.FacialFeatures[11]; ProgramCore.Project.DetectedBottomPoints.Add(new Vector3((p11.X + fcr.FacialFeatures[9].X) * 0.5f, p11.Y, fcr.FacialFeatures[9].Z)); ProgramCore.Project.DetectedBottomPoints.Add(new Vector3((p11.X + fcr.FacialFeatures[10].X) * 0.5f, p11.Y, fcr.FacialFeatures[10].Z)); ProgramCore.Project.DetectedBottomPoints.Add(fcr.FacialFeatures[8]);// * 0.75f + fcr.FacialFeatures[10] * 0.25f); ProgramCore.Project.DetectedBottomPoints.Add(fcr.FacialFeatures[6]); ProgramCore.Project.DetectedBottomPoints.Add(fcr.FacialFeatures[66]); ProgramCore.Project.DetectedBottomPoints.Add(fcr.FacialFeatures[68]); ProgramCore.Project.DetectedBottomPoints.Add(fcr.FacialFeatures[69]); ProgramCore.Project.DetectedBottomPoints.Add(fcr.FacialFeatures[67]); ProgramCore.Project.DetectedTopPoints.Add(fcr.FacialFeatures[66]); ProgramCore.Project.DetectedTopPoints.Add(fcr.FacialFeatures[67]); ProgramCore.Project.RotatedAngle = fcr.RotatedAngle; var aabb = ProgramCore.MainForm.ctrlRenderControl.InitializeShapedotsHelper(true); // инициализация точек головы. эта инфа тоже сохранится в проект // ProgramCore.MainForm.ctrlRenderControl.ImportPoints(); ProgramCore.MainForm.UpdateProjectControls(true, aabb); // ProgramCore.Project.ToStream(); // ProgramCore.MainForm.ctrlRenderControl.UpdateMeshProportions(); if (ProgramCore.Project.ManType == ManType.Custom) { ProgramCore.MainForm.ctrlRenderControl.Mode = Mode.SetCustomControlPoints; ProgramCore.MainForm.ctrlRenderControl.InitializeCustomControlSpritesPosition(); var exampleImgPath = Path.Combine(Application.StartupPath, "Plugin", "ControlBaseDotsExample.jpg"); using (var ms = new MemoryStream(File.ReadAllBytes(exampleImgPath))) // Don't use using!! ProgramCore.MainForm.ctrlTemplateImage.SetTemplateImage((Bitmap)Image.FromStream(ms), false); // устанавливаем картинку помощь для юзера } }