private void ComputeObjectsFromFolder()
        {
            if (!VerifyWebConfigVariables())
            {
                return;
            }
            var emotionMapper = new MLMapper();

            var sourceFolder = ConfigurationManager.AppSettings["SourceFolder"].ToString();
            var saveFolder   = ConfigurationManager.AppSettings["SaveFolder"].ToString();

            var files = Directory.GetFiles(sourceFolder);

            foreach (var file in files)
            {
                string  lastFolderName = Path.GetFileName(Path.GetDirectoryName(file));
                InfoDTO dto            = new InfoDTO()
                {
                    Category = lastFolderName,
                    Image    = file
                };
                Console.WriteLine(string.Format("Processing {0}", file));

                Thread.Sleep(_pauseParameter);
                byte[]        imgdata     = System.IO.File.ReadAllBytes(file);
                ObjectInfoDTO objectsDTOs = _mlService.GetObjectsFromImage(imgdata);
            }
        }
        private void ComputeEmotionsFromFolderInDocumentDb()
        {
            CreateDatabaseIfNotExists(_db).Wait();
            CreateDocumentCollectionIfNotExists(_db, _collection).Wait();
            var            collectionLink = UriFactory.CreateDocumentCollectionUri(_db, _collection);
            DocumentClient client;

            var emotionMapper = new MLMapper();
            var sourceFolder  = ConfigurationManager.AppSettings["SourceFolder"].ToString();
            var saveFolder    = ConfigurationManager.AppSettings["SaveFolder"].ToString();

            string[] dirs     = Directory.GetDirectories(saveFolder);
            string   category = Path.GetFileName(Path.GetDirectoryName(saveFolder));

            using (client = new DocumentClient(new Uri(_endpointUri), _primaryKey, ConnectionPolicy))
            {
                foreach (var dir in dirs)
                {
                    string[] files = Directory.GetFiles(dir);
                    foreach (var file in files)
                    {
                        var          imageStr   = File.ReadAllText(file);
                        ImageInfoDTO objectInfo = JsonConvert.DeserializeObject <ImageInfoDTO>(imageStr);
                        objectInfo.Id = Path.GetFileName(objectInfo.ImageUrl);

                        Console.WriteLine(objectInfo.Id);
                        AddDocument(client, _db, _collection, objectInfo).Wait();
                    }
                }
            }
        }
        private void ComputeAllCharacteristicsFromFolder()
        {
            if (!VerifyWebConfigVariables())
            {
                return;
            }

            var emotionMapper = new MLMapper();
            var sourceFolder  = ConfigurationManager.AppSettings["SourceFolder"].ToString();
            var saveFolder    = ConfigurationManager.AppSettings["SaveFolder"].ToString();

            string[] dirs = Directory.GetDirectories(sourceFolder);
            foreach (var dir in dirs)
            {
                string[] files = Directory.GetFiles(dir);
                Parallel.ForEach(files, new ParallelOptions {
                    MaxDegreeOfParallelism = 4
                }, (file) =>
                {
                    var mlService         = new MLService(new ServiceProxyCognitiveAzure());
                    string lastFolderName = Path.GetFileName(Path.GetDirectoryName(file));
                    string fileName       = Path.GetFileName(file);
                    Console.WriteLine(string.Format("Processing {0}", file));
                    byte[] imgdata = System.IO.File.ReadAllBytes(file);
                    FaceEmotionDTO[] emotionDTOs = mlService.GetEmotionsFromImage(imgdata);

                    if (emotionDTOs != null && emotionDTOs.Count() > 0)
                    {
                        FaceInfoDTO[] faceInfo    = mlService.GetFacesFromImage(imgdata);
                        ObjectInfoDTO objectsDTOs = mlService.GetObjectsFromImage(imgdata);

                        var imageinfo = new ImageInfoDTO()
                        {
                            Emotions  = emotionDTOs,
                            Faces     = faceInfo,
                            Objects   = objectsDTOs,
                            Category  = lastFolderName,
                            ImageUrl  = fileName,
                            Anger     = emotionDTOs.Average(x => x.Scores != null ? x.Scores.Anger : 0),
                            Contempt  = emotionDTOs.Average(x => x.Scores != null ? x.Scores.Contempt : 0),
                            Disgust   = emotionDTOs.Average(x => x.Scores != null ? x.Scores.Disgust : 0),
                            Fear      = emotionDTOs.Average(x => x.Scores != null ? x.Scores.Fear : 0),
                            Happiness = emotionDTOs.Average(x => x.Scores != null ? x.Scores.Happiness : 0),
                            Neutral   = emotionDTOs.Average(x => x.Scores != null ? x.Scores.Neutral : 0),
                            Sadness   = emotionDTOs.Average(x => x.Scores != null ? x.Scores.Sadness : 0),
                            Surprise  = emotionDTOs.Average(x => x.Scores != null ? x.Scores.Surprise : 0)
                        };

                        if (imageinfo != null)
                        {
                            _emotionService.AddEmotion(imageinfo);
                        }
                        Thread.Sleep(_pauseParameter);
                    }
                }
                                 );
            }
        }
Beispiel #4
0
 public MLService(IServiceProxy serviceProxy)
 {
     _serviceProxy            = serviceProxy;
     _apiCognitiveEmotionLink = ConfigurationManager.AppSettings["ApiCognitiveEmotionLink"].ToString();
     _apiCognitiveFaceLink    = ConfigurationManager.AppSettings["ApiCognitiveFaceLink"].ToString();
     _apiCognitiveCVLink      = ConfigurationManager.AppSettings["ApiCognitiveCVLink"].ToString();
     _apiContentType          = ConfigurationManager.AppSettings["ApiContentType"].ToString();
     _apiKeyName     = ConfigurationManager.AppSettings["ApiKeyName"].ToString();
     _apiKeyEmotions = ConfigurationManager.AppSettings["ApiKeyEmotions"].ToString();
     _apiKeyFace     = ConfigurationManager.AppSettings["ApiKeyFace"].ToString();
     _apiKeyCV       = ConfigurationManager.AppSettings["ApiKeyCV"].ToString();
     _emotionMapper  = new MLMapper();
 }
        private void ComputeEmotionsFromFolder()
        {
            if (!VerifyWebConfigVariables())
            {
                return;
            }
            var emotionMapper = new MLMapper();

            var sourceFolder = ConfigurationManager.AppSettings["SourceFolder"].ToString();
            var saveFolder   = ConfigurationManager.AppSettings["SaveFolder"].ToString();

            var files = Directory.GetFiles(sourceFolder);

            foreach (var file in files)
            {
                string  lastFolderName = Path.GetFileName(Path.GetDirectoryName(file));
                InfoDTO dto            = new InfoDTO()
                {
                    Category = lastFolderName,
                    Image    = file
                };
                Console.WriteLine(string.Format("Processing {0}", file));

                if (!_emotionService.EmotionAllreadyComputed(dto))
                {
                    Thread.Sleep(_pauseParameter);
                    byte[]           imgdata     = System.IO.File.ReadAllBytes(file);
                    FaceEmotionDTO[] emotionDTOs = _mlService.GetEmotionsFromImage(imgdata);

                    if (emotionDTOs != null)
                    {
                        dto.Emotions = emotionDTOs.ToArray();
                        _emotionService.AddEmotion(dto);
                    }
                }
            }
        }