コード例 #1
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context)
        {
            #region Preparation
            var imageUrl = req.Query["imageUrl"];

            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            var predictionSubscriptionKey = config["predictionSubscriptionKey"];
            var predictionProjectId       = config["predictionProjectId"];

            #endregion

            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionSubscriptionKey
            };

            var result = endpoint.PredictImageUrl(Guid.Parse(predictionProjectId), new ImageUrl(imageUrl));

            #region Output
            return(new OkObjectResult(result));

            #endregion
        }
コード例 #2
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);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: fcatae/haranohara
        static void Main(string[] args)
        {
            var predictionKey = "insert your prediction key here";


            PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey);

            PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials);



            // Make a prediction against the new project

            Console.WriteLine("Making a prediction:");

            var testImage = new MemoryStream(File.ReadAllBytes(@"insert the picture you want to compare here"));


            var projectId = new Guid("insert the project guid here");

            var result = endpoint.PredictImage(projectId, testImage, null, null);



            // Loop over each prediction and write out the results

            foreach (var c in result.Predictions)

            {
                Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}");
            }

            Console.ReadKey();
        }
コード例 #4
0
        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}");
            }
        }
コード例 #5
0
        static void Main(string[] args)
        {
            var options = Options.InitializeOptions <PredictionOptions>(args);

            if (options == null)
            {
                return;
            }

            var images             = ImagesLoaderGenerator.GenerateImagesLoader(options, options.AllowedTagNames).LoadImages();
            var predictionEndpoint = new PredictionEndpoint(new PredictionEndpointCredentials(options.PredictionKey));

            if (options.BaseUri != null)
            {
                Console.WriteLine($"The default base uri is {predictionEndpoint.BaseUri}. Changed it to {options.BaseUri}.");
                predictionEndpoint.BaseUri = options.BaseUri;
            }

            PrecisionAndRecall precisionAndRecall;

            using (var predictor = ConstructPredictor(predictionEndpoint, options.WorkDir + options.PredictionFileName, options.MilliSecondsBetweenPredictions))
            {
                var batchPredictor = new BatchPredictorWithRateLimit(predictor, options.MilliSecondsBetweenPredictions);
                precisionAndRecall = PrecisionAndRecall.ComputeAsync(
                    images,
                    options.Threshold,
                    async imgs => await batchPredictor.PredictWithImagesAsync(imgs, options.ProjectId, options.IterationId),
                    options.AllowedTagNames).Result;
            }

            Console.WriteLine($"Precision: {precisionAndRecall.Precision}");
            Console.WriteLine($"Recall: {precisionAndRecall.Recall}");
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
コード例 #6
0
        private static ImagePredictionResultModel ProcessImagePredictions(string imageUrl, Guid projectId)
        {
            string predictionKey = "6c9f96aa9a574ab8b3aa18f5e0ec4c1c";
            var    shouldAlert   = false;

            // Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key
            PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey);
            PredictionEndpoint            endpoint = new PredictionEndpoint(predictionEndpointCredentials);

            // Make a prediction against the new project
            var result = endpoint.PredictImageUrl(projectId, new Microsoft.Cognitive.CustomVision.Models.ImageUrl(imageUrl));

            // Need to throttle the requests as to not upset it into throwing the dreaded 429 too many requests in a given period of time
            Thread.Sleep(200);

            //check for any probabilities greater than 90%
            shouldAlert = result.Predictions.Any(x => x.Probability > .9);

            if (shouldAlert)
            {
                return(result);
            }
            else
            {
                return(null);
            }
        }
コード例 #7
0
        private void MediaService_Saving(IMediaService sender, SaveEventArgs <IMedia> e)
        {
            var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);

            _customVisionPredictionKey = umbracoHelper.TypedContent(1127).GetPropertyValue <string>("customVisionPredictionKey");
            _customVisionApiProjectId  = umbracoHelper.TypedContent(1127).GetPropertyValue <string>("customVisionProjectId");


            if (!string.IsNullOrWhiteSpace(_customVisionPredictionKey) && !string.IsNullOrWhiteSpace(_customVisionApiProjectId))
            {
                PredictionEndpoint endpoint = new PredictionEndpoint()
                {
                    ApiKey = _customVisionPredictionKey
                };

                foreach (IMedia media in e.SavedEntities.Where(a => a.ContentType.Name.Equals(Constants.Conventions.MediaTypes.Image)))
                {
                    string relativeImagePath = ImagePathHelper.GetImageFilePath(media);

                    using (Stream imageFileStream = _fs.OpenFile(relativeImagePath))
                    {
                        var result = endpoint.PredictImage(Guid.Parse(_customVisionApiProjectId), imageFileStream);
                        IEnumerable <string> tags = result.Predictions.Where(a => a.Probability > 0.75).Select(a => a.Tag);
                        media.SetTags("customVisionTags", tags, true);
                    }
                }
            }
        }
コード例 #8
0
        public async Task <IEnumerable <LabelConfidence> > Tags(byte[] image, CustomVisionSettings settings)
        {
            // Create a prediction endpoint, passing in obtained prediction key
            var endpoint = new PredictionEndpoint()
            {
                ApiKey = settings.CustomVisionPredictionKey
            };

            Cognitive.CustomVision.Prediction.Models.ImagePredictionResultModel result;

            using (var testImage = new MemoryStream(image))
            {
                // Make a prediction against the prediction project
                // IMPORTANT: the prediction project should have an iteration marked as default.
                // Otherwise, you need to specify an iteration ID
                result = await endpoint.PredictImageWithNoStoreAsync(settings.CustomVisionProjectId, testImage);
            }

            return(result.Predictions.Select(t => new LabelConfidence()
            {
                Label = t.Tag, Probability = (float)t.Probability
            })
                   .Where(c => c.Probability >= settings.Threshold)
                   .OrderByDescending(c => c.Probability));
        }
コード例 #9
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);
            }
        }
コード例 #10
0
        static void EfetuarTesteImagem(string caminho, bool eURL)
        {
            string chaveAPI     = "0c91a890892d40a0ba9f9e22a5e358de";
            string chaveProjeto = "4a1a0670-7689-4d01-ac59-fc56227cc3ed";

            // Crio o client do Custom View Service.
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = chaveAPI
            };
            ImagePrediction resultado = null;

            // Verifico se o projeto é uma URL ou um arquivo local para usar o método correto.
            if (eURL)
            {
                resultado = endpoint.PredictImageUrl(new Guid(chaveProjeto), new ImageUrl(caminho));
            }
            else
            {
                resultado = endpoint.PredictImage(new Guid(chaveProjeto), File.OpenRead(caminho));
            }

            // Percorro as analises.
            foreach (var possibilidade in resultado.Predictions)
            {
                Console.WriteLine($"Tag: {possibilidade.TagName}");
                Console.WriteLine($"Possibilidade: {possibilidade.Probability:P1}");
            }
        }
コード例 #11
0
 public CustomVisionService(string predictionKey, string projectId)
 {
     _endpoint = new PredictionEndpoint {
         ApiKey = predictionKey
     };
     _projectId = projectId;
 }
コード例 #12
0
        public Form1()
        {
            InitializeComponent();

            trainingApi = new TrainingApi()
            {
                ApiKey = trainingKey
            };
            currentPredictionMode = PredictionMode.Detection;
            project = trainingApi.GetProject(new Guid("e1ec7b7e-ac74-4959-9c8b-4d48d25a7668"));

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

            labelInfo.Text         = textBoxUrl.Text = "";
            percentLevel           = trackBar1.Value;
            selectedItemIndex      = -1;
            isPredicting           = true;
            textBoxTag.Enabled     = buttonAddTag.Enabled = buttonSend.Enabled = buttonClear.Enabled = false;
            labelStatus.Text       = "PREDICTING";
            treeListToUpload       = new List <TreeData>();
            treeListDetection      = new List <TreeData>();
            treeListClassification = new List <TreeData>();
        }
コード例 #13
0
        /// <summary>
        /// Uses Cognitive Services Custom Vision API to determine whether an object was found in an image
        /// with some degree of certainty
        /// </summary>
        /// <param name="imageUrl"></param>
        /// <returns>Boolean</returns>
        private static async Task <bool> ObjectFound(string imageUrl, double minimumScore = 0.5)
        {
            bool objectFound = false;

            try
            {
                // Initialize prediction endpoint
                PredictionEndpoint predictionEndpoint = new PredictionEndpoint()
                {
                    ApiKey = ConfigurationManager.AppSettings["CustomVisionApiKey"]
                };

                // Call custom vision prediction API to predict image
                ImagePredictionResultModel predictionResult = await predictionEndpoint.PredictImageUrlAsync(
                    new Guid(ConfigurationManager.AppSettings["CustomVisionProjectId"]),
                    new ImageUrl(imageUrl));

                // Query for the object tag
                var objectTag = predictionResult.Predictions.Where(x => x.Tag == "Object").FirstOrDefault();

                // Check if the object tag probability surpassed the minimum score
                if (objectTag != null && objectTag.Probability >= minimumScore)
                {
                    objectFound = true;
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            // Return result
            return(objectFound);
        }
コード例 #14
0
 public SubmitPhotoController(IOptions <AppSettings> options)
 {
     _appSettings = options.Value;
     _endpoint    = new PredictionEndpoint {
         ApiKey = _appSettings.PredictionEndpointApiKey
     };
 }
コード例 #15
0
        private static PredictionEndpoint GetPredictionEndpoint()
        {
            var predictionEndpoint = new PredictionEndpoint();

            predictionEndpoint.ApiKey = Properties.Settings.Default.Classifier_PredictionApiKey;
            return(predictionEndpoint);
        }
コード例 #16
0
        private string connectToAnalyze()
        {
            // custom vision
            TrainingApi trainingAPI = new TrainingApi()
            {
                ApiKey = "1360135bcfaa45d5b6d69dc06dcb04e4"
            };
            Guid _projectkey = new Guid("ea8591bc-bff1-47d6-b46d-64938d2d8b58");

            project = trainingAPI.GetProject(_projectkey);

            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = "65125a9d5a774acaaeaa0f9705404307"
            };
            // 28bf5d1c-1025-4d3f-a7d7-4dfd4b65f4a1   iteration key


            ArrayList lists = new ArrayList();

            byte[] imgArr = GetFileAsByteArray(App._file.AbsolutePath);
            System.IO.MemoryStream stream = new System.IO.MemoryStream(imgArr);
            var result = endpoint.PredictImage(project.Id, stream, new Guid("28bf5d1c-1025-4d3f-a7d7-4dfd4b65f4a1"));

            foreach (var prediction in result.Predictions)
            {
                lists.Add($"{prediction.Tag}");
            }
            return(lists.Get(0).ToString());
        }
コード例 #17
0
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            try
            {
                string requestBody = new StreamReader(req.Body).ReadToEnd();
                var    prediction  = JsonConvert.DeserializeObject <Prediction>(requestBody);

                var api           = new TrainingApi(new TrainingApiCredentials(prediction.TrainingId));
                var account       = api.GetAccountInfo();
                var predictionKey = account.Keys.PredictionKeys.PrimaryKey;

                var creds    = new PredictionEndpointCredentials(predictionKey);
                var endpoint = new PredictionEndpoint(creds);

                //This is where we run our prediction against the default iteration
                var result = endpoint.PredictImageUrl(new Guid(prediction.ProjectId), new ImageUrl(prediction.ImageUrl));
                prediction.Results = new Dictionary <string, decimal>();
                // Loop over each prediction and write out the results
                foreach (var outcome in result.Predictions)
                {
                    if (outcome.Probability > .70)
                    {
                        prediction.Results.Add(outcome.Tag, (decimal)outcome.Probability);
                    }
                }

                return((ActionResult) new OkObjectResult(prediction));
            }
            catch (Exception e)
            {
                return(new BadRequestObjectResult(e.GetBaseException().Message));
            }
        }
コード例 #18
0
        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);
        }
コード例 #19
0
ファイル: MainViewModel.cs プロジェクト: hernandoz/VisionAI
        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);
        }
コード例 #20
0
        /// <summary>
        /// ボタンをクリックした時の処理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void _Click(object sender, RoutedEventArgs e)
        {
            var button = (Button)sender;

            if (button.Name == "btnShot")
            {
                txtDisplay.Text = "";
                var loader = new ResourceLoader();
                try
                {
                    // カメラで撮影する
                    var file = await _cameraHelper.CapturePhoto();

                    using (var stream = File.OpenRead(file.Path))
                    {
                        // クライアント生成
                        var client = new PredictionEndpoint
                        {
                            BaseUri = new Uri(Constants.ENDPOINT),
                            ApiKey  = Constants.VISION_KEY,
                        };
                        // 画像を送ってタグを取得する
                        var result = await client.PredictImageWithHttpMessagesAsync(
                            new Guid(Constants.PROJECT_ID),
                            stream,
                            new Guid(Constants.ITERATION_ID));

                        var predictions = result.Body.Predictions;
                        // 合致率が高い順にソートして先頭を取得
                        var    prediction = predictions.ToArray().OrderByDescending(p => p.Probability).ElementAt(0);
                        string text;
                        if (prediction.Tag == "ペットボトル")
                        {
                            text = prediction.Tag + " " + loader.GetString("MSG_TRUSH");
                        }
                        else if (prediction.Tag == "缶")
                        {
                            text = prediction.Tag + " " + loader.GetString("MSG_STAMP");
                        }
                        else if (prediction.Tag == "瓶")
                        {
                            text = prediction.Tag + " " + loader.GetString("MSG_SMASH");
                        }
                        else
                        {
                            text = loader.GetString("LABEL_UNKNOWN");
                        }
                        // 表示
                        txtDisplay.Text = text;
                    }
                }
                catch (Exception exp)
                {
                    var dialog = new MessageDialog(exp.Message);
                    await dialog.ShowAsync();
                }
            }
        }
コード例 #21
0
        private static PredictionEndpoint GetPredictionEndpoint()
        {
            var predictionEndpoint = new PredictionEndpoint
            {
                ApiKey = ConfigurationManager.AppSettings["predictionKey"]
            };

            return(predictionEndpoint);
        }
コード例 #22
0
        public HotDogOrNotPage()
        {
            InitializeComponent();

            endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };
        }
コード例 #23
0
 private void InvokeCognitive(string photoUrl)
 {
     PredictionEndpoint endpoint = new PredictionEndpoint()
     {
         ApiKey = PREDICTION_KEY
     };
     ImageUrl url    = new ImageUrl(photoUrl);
     var      result = endpoint.PredictImageUrl(new Guid("152a2f03-5840-46b4-acfe-987a8342b47e"), url);
 }
コード例 #24
0
        public MainViewModel()
        {
            TakePhotoCommand = new Command(async() => await TakePhoto());
            PickPhotoCommand = new Command(async() => await PickPhoto());

            endpoint = new PredictionEndpoint
            {
                ApiKey = ApiKeys.PredictionKey
            };
        }
コード例 #25
0
        private async Task <ImagePredictionResultModel> PredictImageAsync(Stream stream)
        {
            var ep = new PredictionEndpoint(new PredictionEndpointCredentials(AuthKeys.VisionPredictionKey))
            {
                BaseUri = new Uri(AuthKeys.VisionPredictionUrl)
            };
            var response = await ep.PredictImageWithHttpMessagesAsync(AuthKeys.GardenCenterProjectId, stream).ConfigureAwait(false);

            return(response.Body);
        }
コード例 #26
0
        private void button1_Click(object sender, EventArgs e)
        {
            // 変数定義
            bool   food = false;                        // "food" タグの有無
            string tag  = "";                           // カテゴリタグ

            ImageUrl url = new ImageUrl(textBox1.Text); //TextBoxからURLを取得

            var cvEp = new PredictionEndpoint {
                ApiKey = "PREDICTION_KEY"
            };
            var cvGuid      = new Guid("PROJECT_ID");
            var iterationId = new Guid("iterationId");

            try
            {
                // 画像を判定
                var cvResult = cvEp.PredictImageUrl(cvGuid, url, iterationId);
                foreach (var item in cvResult.Predictions)
                {
                    if (item.Probability > 0.8)
                    {
                        if (item.Tag == "食べ物")
                        {
                            food = true;
                        }
                        else
                        {
                            tag = item.Tag;
                            break;
                        }
                    }
                }

                if (tag != "")
                {
                    // タグに応じてメッセージをセット
                    label1.Text = "この写真は " + tag + " です。";
                }
                else if (food == true)
                {
                    //msg = "I'm not sure what it is ...";
                    label1.Text = "これは食べ物ってことしか分からないです。";
                }
                else
                {
                    //msg = "Send me food photo you are eating!";
                    label1.Text = "食べ物の写真を送ってください。";
                }
            }
            catch (Exception)
            {
                label1.Text = "エラー";
            }
        }