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。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值