Пример #1
0
        Image MakeImage(PictureTag t)
        {
            Image img = Image.FromFile(t.Path);

            int sourceWidth  = img.Width;
            int sourceHeight = img.Height;

            int width  = _width;
            int height = _height;

            // Fit height or fit width
            if (t.FitHeight)
            {
                width = img.Width * height / img.Height;    // Fit height
            }
            else
            {
                height = img.Height * width / img.Width;    // Fit width
            }

            Bitmap   bm = new Bitmap(_width, _height);
            Graphics g  = Graphics.FromImage(bm);

            g.FillRectangle(new SolidBrush(t.Background), new Rectangle(0, 0, _width, _height));
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Rectangle r = new Rectangle((_width - width) / 2, (_height - height) / 2, width, height);

            g.DrawImage(img, r);
            g.Dispose();
            return(bm);
        }
Пример #2
0
        private void OnFitHeight(object sender, EventArgs e)
        {
            PictureBox source = _menu.SourceControl as PictureBox;
            PictureTag t      = source.Tag as PictureTag;

            if (!t.FitHeight)
            {
                t.FitHeight  = true;
                source.Image = MakeImage(t);
            }
            _fit = true;
        }
Пример #3
0
        private void OnColor(object sender, EventArgs e)
        {
            PictureBox source = _menu.SourceControl as PictureBox;
            PictureTag t      = source.Tag as PictureTag;

            ColorDialog d = new ColorDialog();

            d.Color = t.Background;
            if (d.ShowDialog() == DialogResult.OK)
            {
                _bgcolor     = t.Background = d.Color;
                source.Image = MakeImage(t);
            }
        }
Пример #4
0
        public async Task Results_Results_WhenSearchIsTag()
        {
            using (var db = new ArtDbContext(TestingDbContextOptions.TestDbContextOptions()))
            {
                var controller = new SearchController(db);

                var picture = new Picture
                {
                    Id         = 1,
                    IsImage    = true,
                    Likes      = 10,
                    Title      = "Test",
                    UploadDate = DateTime.Now,
                    Url        = "//Test",
                    UserId     = "test",
                    UserName   = "******"
                };

                var tag = new Tag()
                {
                    Id      = 1,
                    TagName = "Tag",
                };

                var pictureTag = new PictureTag
                {
                    Picture   = picture,
                    PictureId = picture.Id,
                    Tag       = tag,
                    TagId     = tag.Id
                };

                tag.Pictures.Add(pictureTag);
                picture.Tags.Add(pictureTag);

                await db.Pictures.AddAsync(picture);

                await db.SaveChangesAsync();

                string searchTerm = "Tag";

                controller.Results(searchTerm);

                Assert.AreEqual(1, controller.PictureResults.Count);
            }
        }
Пример #5
0
        public IActionResult Confirm()
        {
            var pictureToEdit = this.Context.Pictures.Include(a => a.Tags).Where(b => b.Id == this.Id).FirstOrDefault();

            if (Authorized.IsAuthorizedUser(pictureToEdit.UserName, this.User.Identity.Name))
            {
                return(RedirectToPage("/Index"));
            }

            pictureToEdit.Title = this.Title;

            var keywords = this.Tags
                           .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                           .ToList();

            var listOfTagsAll = this.Context.Tags;

            var listOfPictureTags = new List <PictureTag>();

            foreach (var tag in keywords)
            {
                int?tagId = 0;

                try
                {
                    tagId = this.Context.Tags.Where(a => a.TagName == tag).FirstOrDefault().Id;
                }
                catch { }

                if (pictureToEdit.Tags.Any(a => a.Tag.Id == tagId))
                {
                    continue;
                }

                var existsTag = listOfTagsAll.Where(a => a.TagName == tag).FirstOrDefault();

                if (existsTag == null)
                {
                    var newTag = new Tag
                    {
                        TagName = tag
                    };

                    var pictureTag = new PictureTag
                    {
                        Picture   = pictureToEdit,
                        PictureId = pictureToEdit.Id,
                        Tag       = newTag,
                        TagId     = newTag.Id
                    };

                    listOfPictureTags.Add(pictureTag);

                    this.Context.PictureTag.Add(pictureTag);
                    this.Context.Tags.Add(newTag);
                }
                else
                {
                    var pictureTag = new PictureTag
                    {
                        Picture   = pictureToEdit,
                        PictureId = pictureToEdit.Id,
                        Tag       = existsTag,
                        TagId     = existsTag.Id
                    };

                    listOfPictureTags.Add(pictureTag);

                    this.Context.PictureTag.Add(pictureTag);
                }
            }

            pictureToEdit.Tags = listOfPictureTags;

            this.Context.Pictures.Update(pictureToEdit);
            this.Context.SaveChanges();

            return(Redirect($"/viewImage/image/{pictureToEdit.Id}"));
        }
Пример #6
0
        public async Task <IActionResult> UploadImage(IFormFile file, string title, string tags)
        {
            if (file == null || file.Length == 0)
            {
                ModelState.AddModelError("File", "File not selected!");
                return(View());
            }

            var fileExtension = file.FileName.Substring(file.FileName.Length - Math.Min(4, file.FileName.Length));

            if (fileExtension != ".jpg" && fileExtension != ".png")
            {
                ModelState.AddModelError("File", "File not in the right format!");
                return(View());
            }

            var username = this.ControllerContext.HttpContext.User.Identity.Name;

            var           path         = "_Pictures\\" + username;
            DirectoryInfo di           = Directory.CreateDirectory(path);
            var           pathToImages = path + "\\" + Guid.NewGuid() + fileExtension;

            using (var stream = new FileStream(pathToImages, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            var picture = new Picture()
            {
                Title      = title,
                UploadDate = DateTime.Now,
                Url        = pathToImages,
                UserName   = username,
                UserId     = userId,
            };

            var keywords = tags
                           .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                           .ToList();

            var listOfTagsAll = this.Context.Tags;

            var listOfPictureTags = new List <PictureTag>();

            foreach (var tag in keywords)
            {
                var existsTag = listOfTagsAll.Where(a => a.TagName == tag).FirstOrDefault();

                if (existsTag == null)
                {
                    var newTag = new Tag
                    {
                        TagName = tag
                    };

                    var pictureTag = new PictureTag
                    {
                        Picture   = picture,
                        PictureId = picture.Id,
                        Tag       = newTag,
                        TagId     = newTag.Id
                    };

                    listOfPictureTags.Add(pictureTag);

                    this.Context.PictureTag.Add(pictureTag);
                    this.Context.Tags.Add(newTag);
                }
                else
                {
                    var pictureTag = new PictureTag
                    {
                        Picture   = picture,
                        PictureId = picture.Id,
                        Tag       = existsTag,
                        TagId     = existsTag.Id
                    };

                    listOfPictureTags.Add(pictureTag);

                    this.Context.PictureTag.Add(pictureTag);
                }
            }

            picture.Tags = listOfPictureTags;

            this.Context.Pictures.Add(picture);
            await this.Context.SaveChangesAsync();

            return(Redirect($"/viewImage/image/{picture.Id}"));
        }
        public FaceRecognitionTrainingStatus AddImageToTrainingSet(Bitmap faceImage, SimplePerson personOnImage)
        {
            using (var originalImage = new Image<Bgr, byte>(faceImage))
            {
                try
                {
                    var faces = _faceDetectionAlgorithm.FindFaces(faceImage);

                    if (faces.Count == 1)
                    {
                        var face = faces.First();

                        var tag = new PictureTag
                        {
                            X = face.Bounds.X,
                            Y = face.Bounds.Y,
                            Height = face.Bounds.Height,
                            Width = face.Bounds.Width,
                        };
                        var existingPerson = _unitOfWork.Repository<Person>().Query(q => q.FirstName == personOnImage.FirstName && q.LastName == personOnImage.LastName).Select().FirstOrDefault();

                        if (existingPerson != null)
                        {
                            tag.PersonOnImageTag = existingPerson;
                        }
                        else
                        {
                            tag.PersonOnImageTag = new Person
                            {
                                FirstName = personOnImage.FirstName,
                                LastName = personOnImage.LastName
                            };
                        }

                        if (!Directory.Exists(_basePicturesPath))
                        {
                            Directory.CreateDirectory(_basePicturesPath);
                        }
                        
                        var thumbnailGrayscaleIplImage = CropAndNormalizeFace(originalImage, face);
                        string originalImageGuid = Guid.NewGuid().ToString();
                        string thumbnailGrayscaleGuid = Guid.NewGuid().ToString();
                        string imageExtension = FileHelper.ImageExtensionFromImageFormat(originalImage.Bitmap.RawFormat);
                        string localOrignalImagePath = _basePicturesPath +"\\Files\\Faces\\"+ originalImageGuid + imageExtension;
                        string localThumbnailGrayscaleImagePath = _basePicturesPath + "\\Files\\Faces\\" + thumbnailGrayscaleGuid + imageExtension;

                        originalImage.Save(localOrignalImagePath);
                        thumbnailGrayscaleIplImage.Save(localThumbnailGrayscaleImagePath);
                        //FileHelper.Save(originalImage, orignalImagePath, 90);
                        //FileHelper.Save(thumbnailGrayscaleIplImage, thumbnailGrayscaleImagePath, 90);

                        var image = new FaceDetection.Library.Models.Image
                        {
                            Pictures = new List<Picture>() 
                            {
                                new Picture
                                {
                                    PicturePath = "/Files/Faces/"+originalImageGuid+imageExtension,
                                    NumberOfChannels = originalImage.NumberOfChannels,
                                    Height = faceImage.Height,
                                    Width = faceImage.Width,
                                    Type = PictureType.Original,
                                    Tags = new List<PictureTag>
                                    {
                                        tag
                                    }
                                },
                                new Picture
                                {
                                    PicturePath = "/Files/Faces/"+thumbnailGrayscaleGuid+imageExtension,
                                    NumberOfChannels = thumbnailGrayscaleIplImage.NumberOfChannels,
                                    Height = _faceSize.Height,
                                    Width = _faceSize.Width,
                                    Type = PictureType.GrayscaleThumbnail,
                                    Tags = new List<PictureTag>
                                    {
                                        tag
                                    }
                                }
                            }
                        };

                        _unitOfWork.BeginTransaction();

                        try
                        {
                            _unitOfWork.Repository<FaceDetection.Library.Models.Image>().Insert(image);
                            _unitOfWork.Commit();

                            return FaceRecognitionTrainingStatus.TrainingSuccessful;
                        }
                        catch (Exception e)
                        {
                            _unitOfWork.Rollback();
                            throw e;
                        }
                    }
                    else if (faces.Count == 0)
                    {
                        return FaceRecognitionTrainingStatus.NoFacesFound;
                    }
                    else
                    {
                        return FaceRecognitionTrainingStatus.FoundMoreThenOneFace;
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            return FaceRecognitionTrainingStatus.TrainingFailure;
        }