/// <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); } }
/// <summary> /// 图像识别绑定信息 /// </summary> /// <param name="model"></param> public void ImageRecognitionBind(ImageRecognitionModel model) { //清空文本框 TestTextBoxAdd(textBox2, "", "", true); if (model != null && model.Log_id != 0) { if (model.Result_num > 0 && model.Result != null && model.Result.Count > 0) { int count = 0; foreach (var item in model.Result) { count++; TestTextBoxAdd(textBox2, $"{item.Root}-{item.Keyword}-置信度{item.Score * 100}%", count + ""); if (item.Baike_info != null) { TestTextBoxAdd(textBox2, $"{item.Baike_info.Baike_url}", "百科页面链接:"); TestTextBoxAdd(textBox2, $"{item.Baike_info.Image_url}", "百科图片链接:"); TestTextBoxAdd(textBox2, $"{item.Baike_info.Description}", "百科内容描述:"); } } } } }
/// <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(); }