144 lines
6.8 KiB
C#
144 lines
6.8 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Data;
|
||
using System.Windows.Documents;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
using System.Windows.Media.Imaging;
|
||
using System.Windows.Navigation;
|
||
using System.Windows.Shapes;
|
||
using WPFMediaKit.DirectShow.Controls;
|
||
|
||
namespace BaiDuDishDemo
|
||
{
|
||
/// <summary>
|
||
/// MainWindow.xaml 的交互逻辑
|
||
/// </summary>
|
||
public partial class MainWindow : Window
|
||
{
|
||
public string APP_ID { get; set; }
|
||
public string API_KEY { get; set; }
|
||
public string SECRET_KEY { get; set; }
|
||
public Baidu.Aip.ImageClassify.ImageClassify BaiduAipClient { get; set; }
|
||
public Baidu.Aip.Ocr.Ocr BaiduAipOcrClient { get; set; }
|
||
public Baidu.Aip.BodyAnalysis.Body BaiduAipBodyClient { get; set; }
|
||
public MainWindow()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
private void Window_Loaded(object sender, RoutedEventArgs e)
|
||
{
|
||
APP_ID = "24045911";
|
||
API_KEY = "zcHzMCd0lUGK9WXRlWl79iGL";
|
||
SECRET_KEY = "VG45PUqGIuZhB0keehEPGIxSzaQrqPyH";
|
||
|
||
BaiduAipClient = new Baidu.Aip.ImageClassify.ImageClassify("Kx0CSX3f1l1LqNr7sXfxFRcc", "rVCmfYOsZWQfQRYu1LqzBad7IeCgeEhc");
|
||
BaiduAipOcrClient = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);
|
||
BaiduAipBodyClient = new Baidu.Aip.BodyAnalysis.Body(API_KEY, SECRET_KEY);
|
||
BaiduAipClient.Timeout = 60000; // 修改超时时间
|
||
//if (MultimediaUtil.VideoInputDevices.Any())
|
||
//{
|
||
// mediaCapVideo.VideoCaptureDevice = MultimediaUtil.VideoInputDevices[0];
|
||
// mediaCapVideo.Play();
|
||
//}
|
||
}
|
||
|
||
private void Button_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
|
||
openFileDialog.Filter = "png文件(.png)|*.png|所有文件|*.*";
|
||
if (openFileDialog.ShowDialog() == true)
|
||
{
|
||
var image = File.ReadAllBytes(openFileDialog.FileName);
|
||
switch (((Button)sender).Name)
|
||
{
|
||
case "btn_rk":
|
||
// 自定义菜品识别-入库,可能会抛出网络等异常,请使用try/catch捕获
|
||
var vrief = $"{{\"Name\":\"{txt_Name.Text}\"}}";
|
||
var result = BaiduAipClient.Dishadd(image, vrief);
|
||
txt_Result.Text = result.ToString();
|
||
break;
|
||
case "btn_cx":
|
||
// 自定义菜品识别-检索,可能会抛出网络等异常,请使用try/catch捕获
|
||
DishSearch.DishSearchResult _DishSearchResult = Newtonsoft.Json.JsonConvert.DeserializeObject<DishSearch.DishSearchResult>(BaiduAipClient.DishSearch(image).ToString());
|
||
txt_Result.Inlines.Clear();
|
||
for (int i = 0; i < _DishSearchResult.result.Length; i++)
|
||
{
|
||
Run run = new Run($"菜品名称:{_DishSearchResult.result[i].dishes[0].brief} 匹配度:{_DishSearchResult.result[i].dishes[0].score.ToString("P")}\r\n");
|
||
txt_Result.Inlines.Add(run);
|
||
}
|
||
break;
|
||
case "btn_Car":
|
||
// 如果有可选参数
|
||
var options = new Dictionary<string, object>
|
||
{
|
||
{"top_num", 2},
|
||
{"baike_num", 0}
|
||
};
|
||
// 带参数调用车辆识别
|
||
result = BaiduAipClient.CarDetect(image, options);
|
||
txt_Result.Text = result.ToString();
|
||
// 如果有可选参数
|
||
options = new Dictionary<string, object>
|
||
{
|
||
{"multi_detect", "true"}
|
||
};
|
||
// 带参数调用车牌识别
|
||
result = BaiduAipOcrClient.LicensePlate(image, options);
|
||
txt_Result.Text = txt_Result.Text + "\r\n" + result.ToString();
|
||
break;
|
||
case "btn_Face":
|
||
//var dynamic = "true";
|
||
|
||
//// 调用人流量统计-动态版,可能会抛出网络等异常,请使用try/catch捕获
|
||
// result = BaiduAipClient.BodyTracking(image, dynamic);
|
||
//Console.WriteLine(result);
|
||
//// 如果有可选参数
|
||
// options = new Dictionary<string, object>
|
||
//{
|
||
// {"case_id", 123},
|
||
// {"case_init", "123"},
|
||
// {"show", "true"},
|
||
// {"area", "100"}
|
||
//};
|
||
//// 带参数调用人流量统计-动态版
|
||
//result = BaiduAipClient.BodyTracking(image, dynamic, options);
|
||
//Console.WriteLine(result);
|
||
break;
|
||
default:
|
||
// 如果有可选参数
|
||
options = new Dictionary<string, object>
|
||
{
|
||
{"top_num", 2},
|
||
{"filter_threshold", "0.7"},
|
||
{"baike_num", 0}
|
||
};
|
||
|
||
// 带参数调用菜品识别
|
||
DishResult Dishresult = Newtonsoft.Json.JsonConvert.DeserializeObject<DishResult>(BaiduAipClient.DishDetect(image, options).ToString());
|
||
txt_Result.Inlines.Clear();
|
||
for (int i = 0; i < Dishresult.result_num; i++)
|
||
{
|
||
Run run = new Run($"菜品名称:{Dishresult.result[i].name}\r\n可信度:{Dishresult.result[i].probability}\r\n");
|
||
txt_Result.Inlines.Add(run);
|
||
}
|
||
break;
|
||
}
|
||
BitmapImage bitmapImage = new BitmapImage();
|
||
bitmapImage.BeginInit(); //给BitmapImage对象赋予数据的时候,需要用BeginInit()开始,用EndInit()结束
|
||
bitmapImage.UriSource = new Uri(openFileDialog.FileName);
|
||
bitmapImage.DecodePixelWidth = 320; //对大图片,可以节省内存。尽可能不要同时设置DecodePixelWidth和DecodePixelHeight,否则宽高比可能改变
|
||
bitmapImage.EndInit();
|
||
|
||
img_Dish.Source = bitmapImage;
|
||
}
|
||
}
|
||
}
|
||
}
|