public async Task <string> ImageClassifyDetect(string filePath) { return(await Task.Run(() => { try { var image = File.ReadAllBytes(filePath); var result = _client.AdvancedGeneral(image); Console.WriteLine(result); // 如果有可选参数 var options = new Dictionary <string, object> { { "baike_num", 5 } }; // 带参数调用通用物体识别 result = _client.AdvancedGeneral(image, options); return result.ToString(); } catch (Exception e) { _logger.LogError(null, e); throw; } })); }
/// <summary> /// 图像识别 /// </summary> /// <param name="Path">图像地址</param> /// <returns></returns> public static List <string> ImageRecognition(string Path) { List <string> types = null; // 设置APPID/AK/SK var API_KEY = "bCKBIvGV7CGRCTEmRErE3h8l"; var SECRET_KEY = "fsdd0brGLR04g47TykqIgZsRB4a7Kba1"; var client = new Baidu.Aip.ImageClassify.ImageClassify(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 // 调用通用物体识别,可能会抛出网络等异常,请使用try/catch捕获 try { var image = File.ReadAllBytes(Path); var result = client.AdvancedGeneral(image); // 如果有可选参数 var options = new Dictionary <string, object> { { "baike_num", 5 } }; result = client.AdvancedGeneral(image, options); //解析json数据 RootObject rb = JsonConvert.DeserializeObject <RootObject>(((object)result).ToString()); List <Result> list = rb.result; types = new List <string>(); foreach (Result itme in list) { types.Add(itme.keyword); } return(types); } catch (Exception ex) { return(types); } }
public static List <string> ImgSearchImg(string ImgPath) { var APP_ID = "20408952"; var API_KEY = "oGP41zYCvdWBzFkyz1xBfUXT"; var SECRET_KEY = "CwM4KCyzngkTCQAd2aIy08LYtPgPaDL8"; var client = new Baidu.Aip.ImageClassify.ImageClassify(API_KEY, SECRET_KEY); client.Timeout = 6000; // 修改超时时间 var image = File.ReadAllBytes(ImgPath); // 调用通用物体识别,可能会抛出网络等异常,请使用try/catch捕获 var result = client.AdvancedGeneral(image)["result"]; List <string> ImgKeywordList = new List <string>(); foreach (var i in result) { ImgKeywordList.Add(i["keyword"].ToString()); } return(ImgKeywordList); }
/// <summary> /// 通过打开一个文件来进行图像识别 /// </summary> public void EasyPicRecognition() { OpenFileDialog ofd = new OpenFileDialog(); ofd.ShowDialog(); string _path = ofd.FileName; var picclient = new Baidu.Aip.ImageClassify.ImageClassify("NpBGfUR6qBGtFo5bIFbiPCO9", "S0L7LXAewfW7BBKmbXd0EQ8iRzEYRGqc") { Timeout = 60000 // 修改超时时间 }; var image = File.ReadAllBytes(_path); var picoptions = new Dictionary <string, object> { }; var results = picclient.AdvancedGeneral(image, picoptions); if (results != null && results.ToString() != null && results.ToString().Length > 0) { var json = JsonConvert.SerializeObject(results); ImageRecognitionModel model = DeserializeJsonToObject <ImageRecognitionModel>(json) ?? new ImageRecognitionModel(); ImageRecognitionBind(model); } }
private IEnumerator getInfoAboutEnumerator(Texture2D tex) { yield return(null); byte[] image = tex.EncodeToJPG(); try { var result = Client.AdvancedGeneral(image); Debug.Log(result); } catch (Exception e) { Console.WriteLine(e); throw; } finally { if (resTex) { Destroy(resTex); } } }
/// <summary> /// 人脸检测 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void skinButton1_Click(object sender, EventArgs e) { if (picbPreview.Image is null) { MessageBox.Show("请先拍照或者选取一张图片,再进行检测操作"); return; } // var APP_ID = "17894506"; Thread threadadd = new Thread(() => { try { var client = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY) { Timeout = 60000 // 修改超时时间 }; var image = ImgToBase64String((Bitmap)this.picbPreview.Image); var imageType = "BASE64"; // 调用人脸检测,可能会抛出网络等异常,请使用try/catch捕获 var result = client.Detect(image, imageType); // 如果有可选参数 var options = new Dictionary <string, object> { { "face_field", "age,beauty,expression,face_shape,gender,glasses," + ",race,quality,eye_status,emotion,face_type,eye_status" }, { "max_face_num", Max_face_num }, { "face_type", "LIVE" }, { "liveness_control", "LOW" } }; // 带参数调用人脸检测 result = client.Detect(image, imageType, options); if (result != null && result.ToString() != null && result.ToString().Length > 0) { var json = JsonConvert.SerializeObject(result); FaceCheckModel model = DeserializeJsonToObject <GetApiJson <FaceCheckModel> >(json)?.Result ?? new FaceCheckModel(); FaceTestBind(model); } ; var picclient = new Baidu.Aip.ImageClassify.ImageClassify("NpBGfUR6qBGtFo5bIFbiPCO9", "S0L7LXAewfW7BBKmbXd0EQ8iRzEYRGqc") { Timeout = 60000 // 修改超时时间 }; Image img = this.picbPreview.Image; MemoryStream ms = new MemoryStream(); byte[] imagedata = null; img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); imagedata = ms.GetBuffer(); var picoptions = new Dictionary <string, object> { { "baike_num", Result_num } }; var results = picclient.AdvancedGeneral(imagedata, picoptions); if (results != null && results.ToString() != null && results.ToString().Length > 0) { var json = JsonConvert.SerializeObject(results); ImageRecognitionModel model = DeserializeJsonToObject <ImageRecognitionModel>(json) ?? new ImageRecognitionModel(); ImageRecognitionBind(model); } } catch (Exception ex) { MessageBox.Show("网络错误!错误信息:" + ex.Message); } }); threadadd.Start(); }
private void button_openfile_Click(object sender, RoutedEventArgs e) { try { OpenFileDialog fdlg = new OpenFileDialog(); fdlg.Title = "选择图像识别文件"; //fdlg.InitialDirectory = @"c:\"; fdlg.Filter = "图片文件(*.jpg,*.jpeg,*.png,*.bmp)|*.jpg;*.jpeg;*.png;*.bmp"; fdlg.FilterIndex = 0; fdlg.RestoreDirectory = false; if (fdlg.ShowDialog() == true) { m_imgPath = fdlg.FileName; } var image = File.ReadAllBytes(m_imgPath); image_recognition.Dispatcher.BeginInvoke(new Action(() => { // 调用图像主体检测,可能会抛出网络等异常,请使用try/catch捕获 var result = mClient.ObjectDetect(image); Console.WriteLine(result); // 如果有可选参数 int with_face = 0; if (checkBox_face.IsChecked.Equals(true)) { with_face = 1; } var options = new Dictionary <string, object> { { "with_face", with_face } }; // 带参数调用图像主体检测 result = mClient.ObjectDetect(image, options); var left = (int)result["result"]["left"]; var top = (int)result["result"]["top"]; var width = (int)result["result"]["width"]; var height = (int)result["result"]["height"]; Rectangle rect = new System.Drawing.Rectangle(left, top, width, height); Image change_img = Image.FromFile(m_imgPath); Graphics plate = Graphics.FromImage(change_img); plate.DrawRectangle(new System.Drawing.Pen(Color.BlueViolet, 2), rect); plate.Save(); plate.Dispose(); MemoryStream ms = new MemoryStream(); change_img.Save(ms, ImageFormat.Bmp); change_img.Dispose(); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); image_recognition.Source = bi; image_recognition.InvalidateVisual(); })); textBox_total.Dispatcher.BeginInvoke(new Action(() => { var options_ag = new Dictionary <string, object> { { "baike_num", 5 } }; // 带参数调用通用物体识别 var result = mClient.AdvancedGeneral(image, options_ag); var infos = result["result"]; List <string> infoList = new List <string>(); foreach (var info in infos) { infoList.Add((string)info["keyword"]); infoList.Add((string)info["baike_info"]["description"]); break; } textBox_total.Text = string.Join("\n", infoList); })); } catch (Exception exception) { MessageBox.Show(exception.Message, "错误"); } }