Beispiel #1
0
        public async Task PixelImagesToGoogle()
        {
            PixelImage pixelImage = null;
            int        skip       = 0;

            do
            {
                _logger.Info($"skiping: {skip}");
                using (var pixelContext = new PixelContext(_dbContextOptions))
                {
                    var q = pixelContext.PixelImage
                            .Where(x => string.IsNullOrWhiteSpace(x.GoogleId) &&
                                   x.ProcessState == null || x.ProcessState.Value == 0)
                            .Skip(skip);
                    var count = await q.CountAsync();

                    _logger.Info($"To proccess: {count}");
                    pixelImage = await q.FirstOrDefaultAsync();
                }
                if (pixelImage == null)
                {
                    break;
                }
                await PixelImageToGoogle(pixelImage);
            } while (true);
        }
Beispiel #2
0
    void OnPixel(Color32 color, Vector3 position, PixelContext context)
    {
        var entry = m_prefabs.FirstOrDefault(e => e.color.Equals(color));

        if (entry != null)
        {
            var go = (GameObject)Instantiate(entry.prefab, position, Quaternion.identity);
            go.transform.parent = transform;
            m_loadedObjects.Add(go);

            if (color.Equals(m_fenceColor) && m_useFence)
            {
                bool down  = context.Down.Equals(m_emptyColor);
                bool up    = context.Up.Equals(m_emptyColor);
                bool left  = context.Left.Equals(m_emptyColor);
                bool right = context.Right.Equals(m_emptyColor);

                if (down || up || left || right)
                {
                    Quaternion rotation = Quaternion.identity;

                    var fenceGO = (GameObject)Instantiate(m_fencePrefab, position, rotation);
                    fenceGO.transform.parent = transform;
                    m_loadedObjects.Add(fenceGO);
                }
            }
        }
    }
Beispiel #3
0
        public async Task UpdateImagesMeta(int index = 0)
        {
            PixelImage pixelImage = null;

            do
            {
                _logger.Info($"index: {index}");
                using (var pixelContext = new PixelContext(_dbContextOptions))
                {
                    pixelImage = await pixelContext.PixelImage.Skip(index).FirstOrDefaultAsync();
                }
                index++;

                await UpdateImageMeta(pixelImage);
            } while (pixelImage != null);
        }
Beispiel #4
0
        public async Task UpdateUsersImages(int index = 0)
        {
            PixelUser user = null;

            do
            {
                _logger.Info($"index: {index}");
                using (var pixelContext = new PixelContext(_dbContextOptions))
                {
                    user = await pixelContext.PixelUser.Skip(index).FirstOrDefaultAsync();
                }
                index++;
                _logger.Info($"user.NickName: {user.NickName}");

                await UpdateUserImages(user);
            } while (user != null);
        }
Beispiel #5
0
        public async Task UpdateUserImages(PixelUser pixelUser, int page = 1)
        {
            while (true)
            {
                _logger.Info($"Page: {page}");
                var list = await _imagesParser.LoadImagesByUser(pixelUser.NickName, page);

                if (list is null || list.Count == 0)
                {
                    _logger.Info("No images.");
                    return;
                }
                _logger.Info($"Images loaded: {list.Count}");
                using (var pixelContext = new PixelContext(_dbContextOptions))
                {
                    foreach (var item in list)
                    {
                        var entity = await pixelContext.PixelImage
                                     .Include(x => x.PixelUser)
                                     .SingleOrDefaultAsync(x => x.Url == item.imgUrl);

                        if (entity == null)
                        {
                            await pixelContext.PixelImage.AddAsync(new PixelImage {
                                PixelImageId = Guid.NewGuid(),
                                Url          = item.imgUrl,
                                MetaUrl      = item.metaUrl,
                                Title        = item.title,
                                PixelUserId  = pixelUser.PixelUserId
                            });
                        }
                        else
                        {
                            entity.Title   = item.title;
                            entity.MetaUrl = item.metaUrl;
                            pixelContext.PixelImage.Update(entity);
                        }
                    }

                    await pixelContext.SaveChangesAsync();
                }

                page++;
            }
        }
Beispiel #6
0
        public async Task <IHttpActionResult> POSTRegisteration(User Account)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                PixelContext               context = new PixelContext();
                UserStore <IdentityUser>   store   = new UserStore <IdentityUser>(context);
                UserManager <IdentityUser> manager = new UserManager <IdentityUser>(store);
                IdentityUser               user    = new IdentityUser();
                user.UserName     = Account.UserName;
                user.PasswordHash = Account.Password;
                IdentityResult result = await manager.CreateAsync(user, user.PasswordHash);

                if (result.Succeeded)
                {
                    if (user.UserName == "aly")
                    {
                        manager.AddToRole(user.Id, "Admin");

                        //MAnager SignIn
                        IAuthenticationManager authenticationManager =
                            HttpContext.Current.GetOwinContext().Authentication;
                        SignInManager <IdentityUser, string> signinmanager =
                            new SignInManager <IdentityUser, string>
                                (manager, authenticationManager);
                        signinmanager.SignIn(user, true, true);
                    }

                    return(Ok("Created successfully !" + Account.UserName));
                    //return Redirect("produts.html");
                }
                else
                {
                    return(BadRequest(result.Errors.ToList()[0]));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Beispiel #7
0
        public async Task <Boolean> IsAdmin(string userName, string password)
        {
            PixelContext               context = new PixelContext();
            UserStore <IdentityUser>   store   = new UserStore <IdentityUser>(context);
            UserManager <IdentityUser> manager = new UserManager <IdentityUser>(store);
            IdentityUser               user    = new IdentityUser();

            user.UserName     = userName;
            user.PasswordHash = password;
            var Appuser = manager.Find(user.UserName, user.PasswordHash);//(userLogin.Username, userLogin.Password);
            var roles   = await manager.GetRolesAsync(Appuser.Id);

            if (roles.Count == 0)
            {
                return(false);
            }

            return(true);
        }
Beispiel #8
0
        public async Task UpdateUsers(int page = 1)
        {
            while (true)
            {
                _logger.Info($"Page: {page}");
                var list = await _usersParser.LoadUsersByPage(page);

                if (list is null || list.Count == 0)
                {
                    return;
                }
                _logger.Info($"Users loaded: {list.Count}");
                using (var pixelContext = new PixelContext(_dbContextOptions))
                {
                    foreach (var item in list)
                    {
                        var entity = await pixelContext.PixelUser.SingleOrDefaultAsync(x => x.NickName == item.nickName);

                        if (entity == null)
                        {
                            await pixelContext.PixelUser.AddAsync(new PixelUser()
                            {
                                NickName    = item.nickName,
                                PixelUserId = Guid.NewGuid(),
                                UserName    = item.userName
                            });
                        }
                        else
                        {
                            entity.UserName = entity.UserName;
                            pixelContext.PixelUser.Update(entity);
                        }
                    }

                    await pixelContext.SaveChangesAsync();
                }

                page++;
            }
        }
Beispiel #9
0
        public async Task UpdateImageMeta(PixelImage pixelImage)
        {
            _logger.Info($"metaurl: {pixelImage.MetaUrl}");
            var list = await _imageMetaParser.LoadImageTags(pixelImage.MetaUrl);

            if (list != null && list.Count > 0)
            {
                _logger.Info($"Tags loaded: {list.Count}");
                var keywords = string.Join(',', list);
                using (var pixelContext = new PixelContext(_dbContextOptions))
                {
                    pixelImage.Keywords = keywords;
                    pixelContext.PixelImage.Update(pixelImage);

                    await pixelContext.SaveChangesAsync();
                }
            }
            else
            {
                _logger.Info("No tags.");
            }
        }
        //public async Task Process(int index = 0)
        //{

        //        using var pixelContext = new PixelContext(_pixel_dbContextOptions);
        //    var pixelImages = pixelContext.PixelImage
        //        .Where(x => x.GoogleId != null);
        //    int i = -1;

        //    using var dbContext = new ImageGalleryContext(_dbContextOptions);
        //    foreach (var pixelImage in pixelImages)
        //    {
        //        i++;
        //        _logger.Info($"Processing: {i}");
        //        var image = await dbContext.Image
        //            .SingleOrDefaultAsync(x => x.GoogleId == pixelImage.GoogleId);

        //        if (image == null)
        //        {
        //            var listKeywordIds = new List<Guid>();
        //            if (pixelImage.Keywords!=null)
        //            {
        //                var keywords = new Regex("[^a-z0-9 -,]").Replace(pixelImage.Keywords.ToLower(), "").Split(',')
        //                    .Distinct();
        //                foreach (var keyword in keywords)
        //                {
        //                    var kw = await dbContext.Keyword.SingleOrDefaultAsync(x => x.Value == keyword);

        //                    Guid keywordId;
        //                    if (kw == null)
        //                    {
        //                        keywordId = Guid.NewGuid();
        //                        await dbContext.Keyword.AddAsync(new Keyword
        //                        {
        //                            KeywordId = keywordId,
        //                            Value = keyword
        //                        });
        //                    }
        //                    else
        //                    {
        //                        keywordId = kw.KeywordId;
        //                    }

        //                    listKeywordIds.Add(keywordId);
        //                }

        //            }

        //            var im = new Image
        //            {
        //                ImageId = pixelImage.PixelImageId,
        //                GoogleId = pixelImage.GoogleId
        //            };
        //            if (!string.IsNullOrWhiteSpace(pixelImage.Title))
        //            {
        //                im.Title = new Regex("[^a-zA-Z0-9 -,]").Replace(pixelImage.Title, "");
        //            }
        //            await dbContext.Image.AddAsync(im);
        //            foreach (var item in listKeywordIds)
        //            {
        //                await dbContext.ImageKeyword.AddAsync(new ImageKeyword
        //                {
        //                    ImageKeywordId=Guid.NewGuid(),
        //                    ImageId = pixelImage.PixelImageId,
        //                    KeywordId = item,
        //                });
        //            }
        //            await dbContext.SaveChangesAsync();
        //        }
        //    }

        //}



        private async Task <List <ImageGalleryDb.Models.ImageGallery.Image> > GenData()
        {
            using var pixelContext = new PixelContext(_pixel_dbContextOptions);
            var q = await pixelContext.PixelImage
                    .Where(x => !string.IsNullOrWhiteSpace(x.GoogleId) &&
                           (x.ProcessState == null || x.ProcessState.Value == 0) &&
                           !string.IsNullOrWhiteSpace(x.MSVision))
                    //.Take(2000)
                    .ToListAsync();

            var r = q.AsParallel().GroupBy(x =>
            {
                var obj       = JsonConvert.DeserializeObject <ImageAnalysis>(x.MSVision);
                obj.RequestId = null;
                obj.Metadata  = null;
                obj.Objects   = null;
                var str       = JsonConvert.SerializeObject(obj, new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                });
                var hash = Encoding.ASCII.GetBytes(str).GetStringSha1Hash();
                return(hash);
            }).Select(x => x.FirstOrDefault())
                    .Select(x =>
            {
                var xobj = JsonConvert.DeserializeObject <ImageAnalysis>(x.MSVision);
                var obj  = new ImageGalleryDb.Models.ImageGallery.MSVision.MSVision();
                obj.Tags = xobj.Tags.Select(x => new ImageGalleryDb.Models.ImageGallery.MSVision.Tag
                {
                    Name       = x.Name.ToLower(),
                    Confidence = (float)x.Confidence
                }).GroupBy(x => x.Name).Select(x => x.OrderByDescending(y => y.Confidence).FirstOrDefault()).ToList();
                obj.Categories = xobj.Categories.Select(x => new ImageGalleryDb.Models.ImageGallery.MSVision.Category
                {
                    Name  = x.Name.ToLower(),
                    Score = (float)x.Score
                }).GroupBy(x => x.Name).Select(x => x.OrderByDescending(y => y.Score).FirstOrDefault()).ToList();

                if (xobj.Description != null)
                {
                    obj.Description = new ImageGalleryDb.Models.ImageGallery.MSVision.Description
                    {
                        Tags = xobj.Description.Tags.Select(x => x.ToLower()).Distinct().ToList(),
                    };
                    if (xobj.Description.Captions != null)
                    {
                        obj.Description.Captions = xobj.Description.Captions.Select(
                            x => new ImageGalleryDb.Models.ImageGallery.MSVision.Caption
                        {
                            Confidence = (float)x.Confidence,
                            Text       = x.Text?.ToLower().Replace("a close up of ", "")
                        }).GroupBy(x => x.Text).Select(x => x.OrderByDescending(y => y.Confidence).FirstOrDefault()).ToList();
                    }
                }
                if (xobj.Color != null)
                {
                    obj.Color             = new ImageGalleryDb.Models.ImageGallery.MSVision.Color();
                    obj.Color.AccentColor = xobj.Color.AccentColor;
                    obj.Color.IsBWImg     = xobj.Color.IsBWImg;
                    if (xobj.Color.DominantColors != null)
                    {
                        obj.Color.DominantColors = xobj.Color.DominantColors.Select(
                            x => new ImageGalleryDb.Models.ImageGallery.MSVision.DominantColor
                        {
                            Name = x.ToLower(),
                            DominantColorType = ImageGalleryDb.Models.ImageGallery.MSVision.DominantColorType.None
                        }).ToList();

                        var dcb = xobj.Color.DominantColorBackground.ToLower();
                        var dcf = xobj.Color.DominantColorForeground.ToLower();
                        obj.Color.DominantColors.ForEach(x =>
                        {
                            if (x.Name == dcb &&
                                x.Name == dcf)
                            {
                                x.DominantColorType = ImageGalleryDb.Models.ImageGallery.MSVision.DominantColorType.BackgroundAndForeground;
                            }
                            else if (x.Name == dcb)
                            {
                                x.DominantColorType = ImageGalleryDb.Models.ImageGallery.MSVision.DominantColorType.Background;
                            }
                            else if (x.Name == dcf)
                            {
                                x.DominantColorType = ImageGalleryDb.Models.ImageGallery.MSVision.DominantColorType.Foreground;
                            }
                            else
                            {
                            }
                        });
                    }
                }



                var title = x.Title;
                if (string.IsNullOrEmpty(title))
                {
                    title = obj.Description.Captions.FirstOrDefault()?.Text;
                    if (string.IsNullOrEmpty(title))
                    {
                        title = obj.Tags.FirstOrDefault()?.Name;
                        if (string.IsNullOrEmpty(title))
                        {
                            title = obj.Description.Tags.FirstOrDefault();
                        }
                    }
                }
                var r = new ImageGalleryDb.Models.ImageGallery.Image
                {
                    MSVision    = obj,
                    Title       = title.ToLower(),
                    SeoId       = title.Replace(' ', '-').ToLower(),
                    GoogleDrive = new ImageGalleryDb.Models.ImageGallery.GoogleDrive
                    {
                        Width    = x.Width.Value,
                        Height   = x.Height.Value,
                        GoogleId = x.GoogleId
                    }
                };

                return(r);
            }).ToList();


            r.AsParallel().GroupBy(x => x.Title)
            .Select(x => new { x.Key, Count = x.Count(), Values = x.ToList() })
            .Where(x => x.Count > 1)
            .ToList().ForEach(x =>
            {
                var urlTitle = x.Key;

                for (int i = 1; i < x.Count; i++)
                {
                    x.Values[i].SeoId = (urlTitle).Replace(' ', '-') + '-' + i;
                }
            });
            return(r);
        }
Beispiel #11
0
        public async Task PixelImageToGoogle(PixelImage pixelImage)
        {
            _logger.Info($"url: {pixelImage.Url}");
            var arr = await _fileLoader.GetByteArrayAsync(pixelImage.Url);

            if (arr == null || arr.Length == 0)
            {
                _logger.Error("arr is null");
                using (var pixelContext = new PixelContext(_dbContextOptions))
                {
                    pixelImage.ProcessState = PixelImageProcessState.ImageNotFound;
                    pixelContext.Update(pixelImage);
                    await pixelContext.SaveChangesAsync();
                }
                return;
            }
            int width  = 0;
            int height = 0;

            //string phashStr = null;
            using (var stream = new MemoryStream())
            {
                try
                {
                    using (var im = new MagickImage(arr))
                    {
                        im.Strip();
                        im.Format     = MagickFormat.Jpeg;
                        im.Quality    = 85;
                        im.Interlace  = Interlace.Jpeg;
                        im.ColorSpace = ColorSpace.sRGB;
                        im.Settings.SetDefines(new JpegWriteDefines()
                        {
                            SamplingFactors = new MagickGeometry[] {
                                new MagickGeometry(4, 2)
                            }
                        });
                        width  = im.Width;
                        height = im.Height;
                        im.Write(stream);
                        //im.Grayscale();
                        //var phash = im.PerceptualHash();
                        //phashStr = phash.ToString();
                    }
                }
                catch (ImageMagick.MagickErrorException ex) {
                    _logger.Error(ex);
                    using (var pixelContext = new PixelContext(_dbContextOptions))
                    {
                        pixelImage.ProcessState = PixelImageProcessState.ImgFormatNotValid;
                        pixelContext.Update(pixelImage);
                        await pixelContext.SaveChangesAsync();
                    }
                    return;
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                    throw ex;
                }

                stream.Position = 0;

                try
                {
                    var name = string.IsNullOrEmpty(pixelImage.Title)
                        ? "image"
                        : pixelImage.Title;
                    var id = await _googleService.Upload(stream, $"{name}.jpg");

                    using (var pixelContext = new PixelContext(_dbContextOptions))
                    {
                        pixelImage.GoogleId = id;
                        //pixelImage.GrayscalePerceptualHash = phashStr;
                        pixelImage.Width  = width;
                        pixelImage.Height = height;
                        pixelContext.Update(pixelImage);
                        await pixelContext.SaveChangesAsync();
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                    throw ex;
                }
            }
            return;
        }