植物识别
小雨青青润无声 发布于2019-12 浏览:9581 回复:0
0
收藏
快速回复

百度植物识别用于识别一张图片,即对于输入的一张图片(可正常解码,且长宽比较合适),输出植物识别结果。近期植物识别进行了能力升级——模型升级,Top1准确率绝对值提升11.76%,精度保持业界领先!

 

功能接入:
进入百度AI管理界面:https://console.bce.baidu.com/
进入界面后点击“创建应用”, 输入应用信息及需要的AI功能即可。

百度现在提供每天一定量的免费调用额度,足够大家测试使用了。


获取Access Token
调用植物识别功能前需要先调用鉴权API,确保安全。
具体说明请查阅:http://ai.baidu.com/docs#/Auth/top

请求URL数据格式
向授权服务地址https://aip.baidubce.com/oauth/2.0/token发送请求(推荐使用POST),并在URL中带上以下参数:
grant_type: 必须参数,固定为client_credentials;
client_id: 必须参数,应用的API Key;
client_secret: 必须参数,应用的Secret Key;
例如:
https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR3LT0vuXV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pVw2&


植物识别AP调用
详细说明请参考: https://ai.baidu.com/docs#/ImageClassify-API/

大家需要注意的是:
API访问URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/plant
图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式。注意:图片需要base64编码、去掉编码头后再进行urlencode。


详细代码(C#):

public static class AccessToken
{
// 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存
// 返回token示例
// 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
private static String clientId = "你获得的ID";
// 百度云中开通对应服务应用的 Secret Key
private static String clientSecret = "你获得的KEY";

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

HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
JObject jobject = (JObject)JsonConvert.DeserializeObject(result);
string token = jobject["access_token"].ToString();
return token;
//return result;
}
public class Program
{
// 植物识别
public static string plant(String imagefilename)
{
string token = AccessToken.getAccessToken();
Console.WriteLine(token);
string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
// 图片的base64编码
string base64 = getFileBase64(imagefilename);
String str = "image=" + HttpUtility.UrlEncode(base64);
byte[] buffer = encoding.GetBytes(str);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string result = reader.ReadToEnd();
Console.WriteLine("结果:");
Console.WriteLine(result);
return result;
}
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 static String saveBase64File(string base64Str, string outPath)
{
var contents = Convert.FromBase64String(base64Str);
using (var fs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
fs.Write(contents, 0, contents.Length);
fs.Flush();
}
return "finished";
}
}

功能测试(所有测试图片都来自网络):
植物图片:


返回结果:
结果:
{"log_id": 4113678367978851133, "result": [{"score": 0.8339664936065674, "name": "金边吊兰", "baike_info": {"baike_url": "http://baike.baidu.com/item/%E9%87%91%E8%BE%B9%E5%90%8A%E5%85%B0/3236002", "image_url": "http://imgsrc.baidu.com/baike/pic/item/1c950a7b02087bf460f9cd50f8d3572c11dfcf76.jpg", "description": "金边吊兰隶属于百合科、吊兰属,多年生常绿草本植物。金边吊兰主产北半球温带与寒带,中国吉林、河北、陕西、四川省及华东等地。由于植物生长繁殖能力较强,适合家庭盆养,因此无明显地理分布。金边吊兰叶片呈宽线形,嫩绿色,着生于短茎上,具有肥大的圆柱状肉质根。总状花序长30-60厘米,弯曲下垂,小花白色;顾名思义,金边吊兰的边是金黄色的,非常好看;它常在花茎上生出数丛由株芽形成的带根的小植株,十分有趣。它生长快,栽培容易,在较明亮的房间内可常年栽培欣赏。是悬吊或摆放在橱顶或花架上最适宜的种类之一。"}}, {"score": 0.598055362701416, "name": "吊兰"}, {"score": 0.12664832174777985, "name": "金心吊兰"}]}

 


测试结论及建议
测试后发现效果不错,调用速度很快,识别结果也很准确。

收藏
点赞
0
个赞
TOP
切换版块