Emgucv不完整图像分割试验(二)——百度api识别图像

做偏了,最近突然需要识别,直接调的百度api,涉及到很多小知识点,老样子罗列一下。

1.c#winform模拟post数据

 public static String getAccessToken()
        {
            String authHost = "https://aip.baidubce.com/oauth/2.0/token";
            HttpClient client = new HttpClient();
            List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>();
            paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
            paraList.Add(new KeyValuePair<string, string>("client_id", clientId));
            paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret));

            HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
            String result = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(result);
            return result;
        }

若出现关键词没高亮,检查下“引用”,很多web相关的dll默认是不在winform下执行的,需要人工强制添加一遍。

2.c# json数据解析

网搜方法很多种,我是Newtonsoft.Json(NuGet一搜就能搜到)。就是一个反序列化,代码超简单。

 private void getBaiduToken_Click(object sender, EventArgs e)
        {
            string tempString = getAccessToken();
            BaiduApi descJson = JsonConvert.DeserializeObject<BaiduApi>(tempString);//反序列化
            this.textBox2.Text = descJson.access_token;
        }
注意需要事先定义一个类该接口对应数据的类就行。
      [DataContract]
        public class BaiduApi
        {
            [DataMember]
            public string access_token { get; set; }
            [DataMember]
            public string session_key { get; set; }
            [DataMember]
            public string scope { get; set; }
            [DataMember]
            public string refresh_token { get; set; }
            [DataMember]
            public string session_secret { get; set; }
            [DataMember]
            public string expires_in { get; set; }

        }

中挂号关键词不能漏,如果没高亮的话记得引用System.Runtime.Serialization,并且代码前部using System.Runtime.Serialization 就行了。

3. c#图像转base64(路径 和 图像两种都有了)

public static String getFileBase64(String fileName)
        {
            FileStream filestream = new FileStream(fileName, FileMode.Open);
            byte[] arr = new byte[filestream.Length];
            filestream.Read(arr, 0, (int)filestream.Length);
            string baser64 = Convert.ToBase64String(arr);
            filestream.Close();
            return baser64;
        }

        public string GetBase64FromImage(Bitmap bmp)
        {
            string strbaser64 = "";
            try
            {
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                strbaser64 = Convert.ToBase64String(arr);
            }
            catch (Exception)
            {
                throw new Exception("Something wrong during convert!");
            }
            return strbaser64;
        }

最终流程如下:拍摄照片-选取照片-连接百度api获取Token-图像转base64-post给百度获取返回值。(简单测试常见物体识别尚可。)



初开博客,目的是交流与合作,本人QQ:273651820。

猜你喜欢

转载自blog.csdn.net/qq_26996385/article/details/80522686
今日推荐