コード例 #1
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();
                }
            }
        }
コード例 #2
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);
        }