百度AI技术「通⽤物体与场景识别」开发使⽤体验
⼀、需求描述
⼤家在出去旅游的时候,往往会对景点⾥的特⾊事物感兴趣,⽽⼀般情况下,如果没有导游的介绍,我们不太清楚这个景区⾥的特⾊景点是什么,有时候即使看到了⼀个事物,都不认识它,只能等着导游来介绍,这样的话,旅游的可玩性、⾃由度都⼤⼤降低了。
⽽如果能够使⽤百度的【通⽤物体与场景识别】技术,只需要简单的拍照上传,就能知道这个物品是什么,这个景点叫什么名字,它的由来它的故事等等,那么基本上可以脱离导游,⾃⼰⼀⾏⼈按照⾃⼰的喜好去游玩,不仅⾃由,还能增长见识,这样的旅游才有意思,否则的话,只能跟着导游⾛。
⼆、应⽤价值
利⽤百度【通⽤物体与场景识别】技术,识别⾃⼰旅游/⽣活中遇到的不认识的物体、场景,了解其背景,增长见识。
三、使⽤攻略
说明:本⽂采⽤C# 语⾔,开发环境为.Net Core 2.1,采⽤在线API接⼝⽅式实现。
(2)、根据 API Key 和 Secret Key 获取 AccessToken。
/// <summary>
/// 获取百度access_token
/// </summary>
/// <param name="clientId">API Key</param>
/// <param name="clientSecret">Secret Key</param>
指物/// <returns></returns>
手动挡车public static string GetAccessToken(string clientId, string clientSecret)
{
string authHost = "/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_cret", clientSecret));
HttpResponMessage respon = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;鹿寨王维古诗
string result = respon.Content.ReadAsStringAsync().Result;
JObject jo = (JObject)JsonConvert.DerializeObject(result);
string token = jo["access_token"].ToString();
return token;
}
(3)、调⽤API接⼝获取识别结果
1、在Startup.cs ⽂件 的 Configure(IApplicationBuilder app, IHostingEnvironment env) ⽅法中开启虚拟⽬录映射功能:
string webRootPath = HostingEnvironment.WebRootPath;//wwwroot⽬录
莴笋的功效app.UStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(webRootPath, "Uploads", "BaiduAIs")),
RequestPath = "/BaiduAIs"
});
2、 建⽴Index.cshtml⽂件
2.1 前台代码:
由于html代码⽆法原⽣显⽰,只能简单说明⼀下:
主要是⼀个form表单,需要设置属性enctype="multipart/form-data",否则⽆法上传图⽚;
form表单⾥⾯有两个控件:
⼀个Input:type="file",asp-for="FileUpload" ,上传图⽚⽤;
⼀个Input:type="submit",asp-page-handler="Advanced" ,提交并返回识别结果。
⼀个img:src="@Model.curPath",显⽰识别的图⽚。
最后显⽰后台 msg 字符串列表信息,如果需要输出原始Html代码,则需要使⽤@Html.Raw()函数。
2.2 后台代码:
春节的来历50字[BindProperty]
public IFormFile FileUpload { get; t; }
private readonly IHostingEnvironment HostingEnvironment;
public List<string> msg = new List<string>();
public string curPath { get; t; }
public BodySearchModel(IHostingEnvironment hostingEnvironment)
{
HostingEnvironment = hostingEnvironment;
}
public async Task<IActionResult> OnPostAdvancedAsync()
{
if (FileUpload is null)
{
ModelState.AddModelError(string.Empty, "本地图⽚!");
}
鬼手居
if (!ModelState.IsValid)
{
return Page();
msg = new List<string>();
string webRootPath = HostingEnvironment.WebRootPath;//wwwroot⽬录
string fileDir = Path.Combine(webRootPath, "Uploads//BaiduAIs//");
string imgName = await UploadFile(FileUpload, fileDir);
string fileName = Path.Combine(fileDir, imgName);
string imgBa64 = GetFileBa64(fileName);
curPath = Path.Combine("/BaiduAIs/", imgName);//需在Startup.cs ⽂件 的 Configure(IApplicationBuilder app, IHostingEnvironment env)⽅法中开启虚拟⽬录映射功能
string result = GetImageJson(imgBa64, “你的API KEY”, “你的SECRET KEY”);
JObject jo =(JObject)JsonConvert.DerializeObject(result);
try
{
List<JToken> msgList = jo["result"].ToList();
int number = int.Par(jo["result_num"].ToString());
int curNumber = 1;
msg.Add("<strong>返回结果:" + number + "</strong>");
foreach (JToken ms in msgList)
{
五十岁生日感言if (number > 1)
{
msg.Add("<strong>第 " + (curNumber++).ToString() + " 条:</strong>");
}
msg.Add("置信度:" + ms["score"].ToString());
msg.Add("标签:" + ms["root"].ToString());
msg.Add("名称:" + ms["keyword"].ToString());
if (ms["baike_info"] != null)
{
msg.Add("百科词条:");
if (ms["baike_info"]["baike_url"] != null)放荡是啥意思
{
msg.Add("<a href='" + ms["baike_info"]["baike_url"].ToString() + "' target='_blank' >页⾯链接</a>");
}
if (ms["baike_info"]["description"] != null)
msg.Add("内容描述:" + ms["baike_info"]["description"].ToString());
}
if (ms["baike_info"]["image_url"] != null)
{
msg.Add("<a href='" + ms["baike_info"]["image_url"].ToString() + "' target='_blank' ><img src='" + ms["baike_info"] ["image_url"].ToString() + "' width=\"100 % \" /></a>");
}
}
}
}
catch(Exception e1)
{
msg.Add(result);
}
return Page();
}
/// <summary>
/// 上传⽂件,返回⽂件名
/// </summary>
/// <param name="formFile">⽂件上传控件</param>
/// <param name="fileDir">⽂件绝对路径</param>
/
// <returns></returns>
public static async Task<string> UploadFile(IFormFile formFile, string fileDir)
{
if (!Directory.Exists(fileDir))
{
Directory.CreateDirectory(fileDir);
}
string extension = Path.GetExtension(formFile.FileName);
string imgName = Guid.NewGuid().ToString("N") + extension;
var filePath = Path.Combine(fileDir, imgName);
using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
await formFile.CopyToAsync(fileStream);
}
return imgName;
}
/// <summary>
/// 返回图⽚的ba64编码
/// </summary>
/// <param name="fileName">⽂件绝对路径名称</param>
/// <returns></returns>
public static String GetFileBa64(string fileName)
{
FileStream filestream = new FileStream(fileName, FileMode.Open);
byte[] arr = new byte[filestream.Length];
filestream.Read(arr, 0, (int)filestream.Length);
string bar64 = Convert.ToBa64String(arr);
filestream.Clo();
return bar64;
}
/// <summary>
/// 图像识别Json字符串
/// </summary>
/
// <param name="strbar64">图⽚ba64编码</param>
/// <param name="clientId">API Key</param>
/// <param name="clientSecret">Secret Key</param>
/// <returns></returns>
public static string GetImageJson(string strbar64, string clientId, string clientSecret)
{
string token = GetAccessToken(clientId, clientSecret);
string host = "/rest/2.0/image-classify/v2/advanced_general?access_token=" + token; Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
string str = "image=" + HttpUtility.UrlEncode(strbar64)+”&baike_num=5“;