示例#1
0
        /// <summary>
        /// 检测图片中熊的位子并标识
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void BtDetection_Click(object sender, RoutedEventArgs e)
        {
            tbResult.Text         = string.Empty;
            imgPreview.Source     = null;
            btDetection.IsEnabled = false;

            try
            {
                // 下载图片
                Uri uri      = new Uri(tbUrl.Text);
                var response = await new HttpClient().GetAsync(uri);
                var stream   = (await response.Content.ReadAsStreamAsync()).AsRandomAccessStream();

                var decoder = await BitmapDecoder.CreateAsync(stream);

                var softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                // 构造模型需要的输入格式
                VideoFrame videoFrame = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);


                var stringResult = new StringBuilder();
                var output       = await detectionModel.PredictImageAsync(videoFrame);

                var resultDescend = output.OrderByDescending(m => m.Probability);
                foreach (var r in resultDescend)
                {
                    stringResult.AppendLine(string.Format("{0}\t: {1}%", r.TagName, r.Probability * 100));
                }

                tbResult.Text = stringResult.ToString();

                if (output.Count > 0)
                {
                    softwareBitmap = await Process(softwareBitmap, output.First().BoundingBox);
                }

                imgPreview.Source = await LoadTempOutImage();
            }
            catch (Exception ex)
            {
                MessageDialog a = new MessageDialog(String.Format("读取图片错误:\n{0}", ex.ToString()));
                await a.ShowAsync();
            }
            btDetection.IsEnabled = true;
        }
示例#2
0
        private async void BtRecognize_Click(object sender, RoutedEventArgs e)
        {
            tbResult.Text     = string.Empty;
            imgPreview.Source = null;


            List <UIElement> removedItems = new List <UIElement>();

            foreach (var item in gridMain.Children)
            {
                if (item is Border || (item is TextBlock && item != tbResult))
                {
                    removedItems.Add(item);
                }
            }
            foreach (var item in removedItems)
            {
                gridMain.Children.Remove(item);
            }

            try
            {
                // 下载图片
                Uri uri      = new Uri(tbUrl.Text);
                var response = await new HttpClient().GetAsync(uri);
                var stream   = (await response.Content.ReadAsStreamAsync()).AsRandomAccessStream();

                var decoder = await BitmapDecoder.CreateAsync(stream);

                var softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                // 构造模型需要的输入格式
                VideoFrame videoFrame = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
                //ImageFeatureValue imageFeatureValue = ImageFeatureValue.CreateFromVideoFrame(videoFrame);

                // 推理
                //var output = await model.EvaluateAsync(new bearInput() { data = imageFeatureValue });
                var output = await model.PredictImageAsync(videoFrame);

                // 解析结果、更新控件
                var    stringResult = new StringBuilder();
                var    pbWidth      = imgPreview.ActualWidth;
                var    pbHeight     = imgPreview.ActualHeight;
                Random rand         = new Random();

                foreach (var item in output)
                {
                    if (item.Probability > 0.5)
                    {
                        int x = (int)Math.Round(item.BoundingBox.Left * pbWidth);
                        int y = (int)Math.Round(item.BoundingBox.Top * pbHeight);
                        int w = (int)Math.Round(item.BoundingBox.Width * pbWidth);
                        int h = (int)Math.Round(item.BoundingBox.Height * pbHeight);
                        stringResult.AppendLine(string.Format("{0}: {1}, [{2},{3},{4},{5}]",
                                                              item.TagName, item.Probability, x, y, w, h));

                        Rectangle        rect    = new Rectangle(x, y, w, h);
                        Color            color   = Color.FromKnownColor((KnownColor)rand.Next(1, 175));
                        Windows.UI.Color uiColor = Windows.UI.Color.FromArgb(color.A, color.R, color.G, color.B);
                        Border           b       = new Border();
                        b.BorderBrush         = new SolidColorBrush(uiColor);
                        b.BorderThickness     = new Thickness(2);
                        b.Width               = w;
                        b.Height              = h;
                        b.Margin              = new Thickness(x + imgPreview.Margin.Left, y + imgPreview.Margin.Top, 0, 0);
                        b.HorizontalAlignment = HorizontalAlignment.Left;
                        b.VerticalAlignment   = VerticalAlignment.Top;
                        gridMain.Children.Add(b);

                        TextBlock tb = new TextBlock();
                        tb.Margin = new Thickness(b.Margin.Left + 2, b.Margin.Top + 2, 0, 0);
                        tb.HorizontalAlignment = HorizontalAlignment.Left;
                        tb.VerticalAlignment   = VerticalAlignment.Top;
                        tb.Text = item.TagName;
                        tb.IsColorFontEnabled = true;
                        tb.Foreground         = b.BorderBrush;
                        gridMain.Children.Add(tb);
                    }
                }
                tbResult.Text = stringResult.ToString();



                //以下老师原代码

                /*var resultDescend = output.loss[0].OrderByDescending(p => p.Value);
                 * var name = output.classLabel.GetAsVectorView()[0];
                 *
                 * var stringResult = new StringBuilder();
                 * stringResult.AppendLine(name);
                 * stringResult.AppendLine();
                 * foreach (var kvp in resultDescend)
                 * {
                 *  stringResult.AppendLine(string.Format("{0}\t: {1}%", kvp.Key, kvp.Value));
                 * }
                 */
                tbResult.Text = stringResult.ToString();
                var sbs          = new SoftwareBitmapSource();
                var imagePreview = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                await sbs.SetBitmapAsync(imagePreview);

                imgPreview.Source = sbs;
            }
            catch (Exception ex)
            {
                MessageDialog a = new MessageDialog(String.Format("读取图片错误:\n{0}", ex.ToString()));
                await a.ShowAsync();
            }
        }