Exemplo n.º 1
0
        async Task PredictPhoto(MediaFile photo)
        {
            var endpoint = new PredictionEndpoint
            {
                ApiKey = await KeyService.GetPredictionKey()
            };

            var results = await endpoint.PredictImageAsync(Guid.Parse(await KeyService.GetProjectId()),
                                                           photo.GetStream());

            AllPredictions = results.Predictions
                             .Where(p => p.Probability > Probability)
                             .ToList();


            // Create the Api, passing in the training key
            CustomVisionTrainingClient trainingApi = new CustomVisionTrainingClient()
            {
                ApiKey   = KeyService.TK,
                Endpoint = KeyService.SouthCentralUsEndpoint
            };

            // Find the object detection domain

            //var domains = trainingApi.GetDomains();
            //var objDetectionDomain = domains.FirstOrDefault(d => d.Type == "ObjectDetection");

            //upload to service
            trainingApi.CreateImagesFromData(Guid.Parse(await KeyService.GetProjectId()), photo.GetStream(), null);
        }
Exemplo n.º 2
0
        async Task Predict()
        {
            try
            {
                IsProcessing.IsVisible = true;
                var stream = file.GetStream();

                var result = await endpoint.PredictImageAsync(projectId, stream);

                var observations = result.Predictions
                                   .OrderByDescending(x => x.Probability)
                                   .ToList();


                var observation = result.Predictions.FirstOrDefault();
                if (observation != null)
                {
                    var good    = observation.Probability > 0.8;
                    var name    = observation.Tag.Replace('-', ' ').ToUpperInvariant();
                    var title   = good ? $"{name}" : $"maybe {name}";
                    var message = good ? $"I am {Math.Round(observation.Probability * 100)}% sure." : "";
                    await Application.Current.MainPage.DisplayAlert(title, message, "OK");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                IsProcessing.IsVisible = false;
            }
        }
        private async void EvaluateImages()
        {
            // Add your prediction key from the settings page of the portal
            // The prediction key is used in place of the training key when making predictions
            string predictionKey = "ab7cbbf3606a41308e3c66d73c4af3fa";

            // Create a prediction endpoint, passing in obtained prediction key
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };
            // Get image to test
            var imagePath = System.IO.Path.Combine(root, "../../Evaluated/evaluated.jpg");

            testImage = new MemoryStream(File.ReadAllBytes(imagePath));

            // Make a prediction against the new project
            Log("Making a prediction:");
            //Hardcoding projectID.
            //Guid projectID = new Guid("d8622071-5654-435d-a776-83944fc43129");
            var result = await endpoint.PredictImageAsync(project.Id, testImage);

            // Loop over each prediction and write out the results
            foreach (var c in result.Predictions)
            {
                Log($"\t{c.Tag}: {c.Probability:P1}");
            }
        }
Exemplo n.º 4
0
        public async Task <string> ClassificacaoCustomizadaImagemAsync(Stream image)
        {
            // Now there is a trained endpoint, it can be used to make a prediction

            // Add your prediction key from the settings page of the portal
            // The prediction key is used in place of the training key when making predictions
            string predictionKey = _customApiKey;

            // Create a prediction endpoint, passing in the obtained prediction key
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };

            // Make a prediction against the new project
            Console.WriteLine("Making a prediction:");
            var result = await endpoint.PredictImageAsync(new Guid("fd6f899b-1987-41b7-8067-efe0d262d319"), image);

            // Loop over each prediction and write out the results
            foreach (var c in result.Predictions)
            {
                return($"Eu identifiquei um objeto do tipo **{c?.Tag}** na imagem, com " +
                       $"**{c?.Probability}**% de acertividade.");
            }

            return("Nenhum resultado encontrado.");
        }
        public static async Task <string> GetPredictionsAsync2(byte[] imageBytes, string intent)
        {
            Debug.WriteLine("inside GetPredictionsAsync2 ");
            string projectid, apikey;

            GetProjectBasedOnIntent(intent, out projectid, out apikey);

            // Create a prediction endpoint, passing in the obtained prediction key
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = apikey
            };

            // Make a prediction against the new project
            Debug.WriteLine("Making a prediction:");
            var result = await endpoint.PredictImageAsync(new Guid(projectid), new MemoryStream(imageBytes));

            var pn = result.Predictions.Where(e => e.Probability > 0.5).FirstOrDefault();

            if (pn != null)
            {
                Debug.Write($"responese Prediction tag {pn.Tag}");
                return(pn.Tag);
            }
            else
            {
                Debug.Write("We didnt get any prediction");
            }


            return(null);
        }
Exemplo n.º 6
0
        public async Task <ImageInsights> PredictImage(string imageLocalPath)
        {
            var fileName = Path.GetFileName(imageLocalPath);
            var customVisionProjectId     = ConfigurationManager.AppSettings["CustomVisionProjectId"];
            var customVisionPredictionKey = ConfigurationManager.AppSettings["CustomVisionPredictionKey"];
            PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(customVisionPredictionKey);
            PredictionEndpoint            endpoint = new PredictionEndpoint(predictionEndpointCredentials);

            try
            {
                // Make a prediction against the project
                var testImage = new MemoryStream(System.IO.File.ReadAllBytes(imageLocalPath));
                var result    = await endpoint.PredictImageAsync(Guid.Parse(customVisionProjectId), testImage);

                ImageInsights insights = new ImageInsights
                {
                    ImageId = fileName,
                    Tags    = result.Predictions.Where(s => s.Probability >= 0.60).Select(s => s.Tag).ToArray()
                };
                return(insights);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Exemplo n.º 7
0
        public async Task <IList <PredictionViewModel> > Predict(byte[] image)
        {
            var trainingCredentials = new TrainingApiCredentials(APIKeys.TrainingAPIKey);
            var trainingApi         = new TrainingApi(trainingCredentials);

            var predictionEndpointCredentials = new PredictionEndpointCredentials(APIKeys.PredictionAPIKey);
            var predictionEndpoint            = new PredictionEndpoint(predictionEndpointCredentials);

            var projects = await trainingApi.GetProjectsAsync();

            var stream  = new System.IO.MemoryStream(image);
            var results = await predictionEndpoint.PredictImageAsync(projects[0].Id, stream);

            var predictions = new List <PredictionViewModel>();

            foreach (var result in results.Predictions)
            {
                predictions.Add(new PredictionViewModel()
                {
                    Tag = result.Tag, Prediction = result.Probability
                });
            }

            return(predictions);
        }
Exemplo n.º 8
0
        public async Task <PredictionModel> AnalyzeAsync(string imagePath)
        {
            var image = await ImageUtils.GetImageStreamAsync(imagePath);

            var prediction = await _endpoint.PredictImageAsync(Guid.Parse(_projectId), image);

            return(prediction.Predictions.Where(x => x.Probability > 0.60).Count() > 0 ? prediction.Predictions.OrderByDescending(x => x.Probability).First() : null);
        }
Exemplo n.º 9
0
        async Task PredictPhoto(MediaFile photo)
        {
            var results = await endpoint.PredictImageAsync(Guid.Parse(ApiKeys.ProjectId),
                                                           photo.GetStream());

            Predictions = results.Predictions
                          .Where(p => p.Probability > Probability)
                          .ToList();
        }
Exemplo n.º 10
0
        private static async Task <ImagePredictionResultModel> GetPredictionResponse(Stream blob)
        {
            var endpoint = new PredictionEndpoint
            {
                ApiKey = Environment.GetEnvironmentVariable("PredictionKey")
            };

            var projectId = Guid.Parse(Environment.GetEnvironmentVariable("ProjectId"));

            return(await endpoint.PredictImageAsync(projectId, blob));
        }
Exemplo n.º 11
0
 public async Task <Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImagePrediction> GetDetectedObjects(byte[] image)
 {
     using (var endpoint = new PredictionEndpoint()
     {
         ApiKey = this._predictionApiKey
     })
     {
         using (var ms = new MemoryStream(image))
         {
             return(await endpoint.PredictImageAsync(this._project.Id, ms));
         }
     }
 }
Exemplo n.º 12
0
        async Task PredictPhoto(MediaFile photo)
        {
            var endpoint = new PredictionEndpoint
            {
                ApiKey = await KeyService.GetPredictionKey()
            };

            var results = await endpoint.PredictImageAsync(Guid.Parse(await KeyService.GetProjectId()),
                                                           photo.GetStream());

            AllPredictions = results.Predictions
                             .Where(p => p.Probability > Probability)
                             .ToList();
        }
Exemplo n.º 13
0
        public async Task <IImageRecognizedResult> DetectImage(Stream imageStream, float threshold)
        {
            using (var predictEndpoint = new PredictionEndpoint()
            {
                ApiKey = key
            })
            {
                try
                {
                    var result = new CustomVisionResult
                    {
                        PredictionResultModel = await predictEndpoint.PredictImageAsync(new Guid(projectId), imageStream)
                    };

                    var predictedObject = result.PredictionResultModel.Predictions.FirstOrDefault(obj => obj.Probability > threshold);
                    if (predictedObject != null)
                    {
                        result.IsSure = true;
                    }
                    return(result);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    throw new Exception();
                }
            }

            //using (var httpClient = new HttpClient())
            //{
            //    httpClient.DefaultRequestHeaders.Add("Prediction-Key", key);
            //    var content = new StreamContent(imageStream);
            //    content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            //    var response = await httpClient.PostAsync(uriBase, content);
            //    if (response.IsSuccessStatusCode)
            //    {
            //        string jsonResult = await response.Content.ReadAsStringAsync();
            //        var result = JsonConvert.DeserializeObject<CustomVisionResult>(jsonResult);
            //        if (result.Predictions.Any() && result.Predictions[0].Probability > threshold) result.IsSure = true;
            //        return result;
            //    }
            //    else
            //    {
            //        throw new Exception();
            //    }
            //}
        }
        public async Task <IEnumerable <Recognition> > RecognizeAsync(Stream image)
        {
            // Use the online model.
            var predictionEndpoint = new PredictionEndpoint {
                ApiKey = predictionKey
            };
            var predictions = await predictionEndpoint.PredictImageAsync(projectId, image);

            var results = predictions.Predictions.Select(p => new Recognition
            {
                Tag         = p.Tag,
                Probability = p.Probability
            }).ToList();

            return(results);
        }
        public async Task <bool> IsDuckFace(MediaFile photo)
        {
            InitIfRequired();
            if (photo == null)
            {
                return(false);
            }

            using (var stream = photo.GetStreamWithImageRotatedForExternalStorage())
            {
                var predictionModels = await _endpoint.PredictImageAsync(ApiKeys.ProjectId, stream);

                return(predictionModels.Predictions
                       .FirstOrDefault(p => p.Tag == "Duck Face")
                       .Probability > ProbabilityThreshold);
            }
        }
Exemplo n.º 16
0
        public async Task <IEnumerable <Recognition> > RecognizeAsync(Stream image)
        {
            var settingsService = SimpleIoc.Default.GetInstance <ISettingsService>();

            // Use the online model.
            var predictionEndpoint = new PredictionEndpoint {
                ApiKey = settingsService.PredictionKey
            };
            var predictions = await predictionEndpoint.PredictImageAsync(settingsService.ProjectId, image);

            var results = predictions.Predictions.Select(p => new Recognition
            {
                Tag         = p.Tag,
                Probability = p.Probability
            }).ToList();

            return(results);
        }
Exemplo n.º 17
0
        public async Task <PredictImageResult> PredictImage(Stream testImage)
        {
            string trainingKey   = "6308b3b62b344e3f8e4170c4728deed2";
            string predictionKey = "afdffbaa498445c1830aa18ee9216e0b";

            // Create a prediction endpoint, passing in obtained prediction key
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };

            TrainingApi trainingApi = new TrainingApi()
            {
                ApiKey = trainingKey
            };
            var projects = await trainingApi.GetProjectsAsync();

            var project = projects.First(f => f.Name == "WA-SE-AI");

            try
            {
                var result = await endpoint.PredictImageAsync(project.Id, testImage);

                var tags = await trainingApi.GetTagsAsync(project.Id);

                // Loop over each prediction and write out the results
                foreach (var c in result.Predictions)
                {
                    Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}");
                }
                var topPrediction = result.Predictions.OrderByDescending(m => m.Probability).First();
                PredictImageResult predictImageResult = new PredictImageResult
                {
                    PredictionModel = topPrediction,
                    Tag             = tags.FirstOrDefault(f => f.Id == topPrediction.TagId)
                };
                return(predictImageResult);
            }
            catch (Exception e)
            {
                throw new Exception("PredictImage failed");
            }
        }
Exemplo n.º 18
0
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result)
        {
            var activity = await result as Activity;

            // Variables declaration | 変数定義
            bool   food = false; // "food" tag                | "food" タグの有無
            string tag  = "";    // food category tag         | 食べ物カテゴリータグ
            string msg  = "";    // response message from bot | 返答メッセージ

            // Prep for Custom Vision API | Custom Vision API を使う準備
            var cvCred = new PredictionEndpointCredentials("YOUR_PREDICTION_KEY");
            var cvEp   = new PredictionEndpoint(cvCred);
            var cvGuid = new Guid("YOUR_PROJECT_ID");

            if (activity.Attachments?.Count != 0)
            {
                // Get attachment (photo) and get as Stream | 送られてきた画像を Stream として取得
                var photoUrl    = activity.Attachments[0].ContentUrl;
                var client      = new HttpClient();
                var photoStream = await client.GetStreamAsync(photoUrl);

                try
                {
                    // Predict Image using Custom Vision API | 画像を判定
                    var cvResult = await cvEp.PredictImageAsync(cvGuid, photoStream);


                    // Get food and category tag | food タグ および カテゴリーを取得
                    foreach (var item in cvResult.Predictions)
                    {
                        if (item.Probability > 0.8)
                        {
                            if (item.Tag == "food")
                            {
                                food = true;
                            }
                            else
                            {
                                tag = item.Tag;
                                break;
                            }
                        }
                    }
                }
                catch
                {
                    // Error Handling
                }
            }


            if (tag != "")
            {
                // Set message | タグに応じてメッセージをセット
                //msg = "This is " + tag + "! Looks good ;)";
                //msg = "この写真は " + tag + " だね♪";

                switch (tag)
                {
                case "curry":
                    msg = "カレーおいしそう!甘いチャイでホッとしよう☕";
                    //msg = "Have sweet chai after spicy curry!";
                    break;

                case "gyoza":
                    msg = "やっぱ餃子にはビールだね🍺";
                    //msg = "Beer should be best much to Gyoza!";
                    break;

                case "pizza":
                    msg = "ピザには刺激的な炭酸飲料★はどうかな?";
                    //msg = "What about sparkling soda with pizza?";
                    break;

                case "meat":
                    msg = "肉、にく、ニク♪ 赤ワインを合わせてどうぞ🍷";
                    //msg = "Red wine makes you eat more meat!";
                    break;

                case "ramen":
                    msg = "やめられないよねー。ラーメンには緑茶でスッキリ☆";
                    //msg = "Have green tea after Ramen!";
                    break;

                case "sushi":
                    msg = "今日はちょっとリッチにお寿司?合わせるなら日本酒かな🍶";
                    //msg = "Sushi! Have you ever tried Japanese Sake?";
                    break;
                }
            }
            else if (food == true)
            {
                //msg = "I'm not sure what it is ...";
                msg = "この食べ物は分からないです...日本の夏は麦茶だね!";
            }
            else
            {
                //msg = "Send me food photo you are eating!";
                msg = "食べ物の写真を送ってね♪";
            }

            await context.PostAsync(msg);

            context.Wait(MessageReceivedAsync);
        }