一、功能介紹
對(duì)于輸入的一張圖片(可正常解碼,且長(zhǎng)寬比適宜),檢測(cè)圖像中的所有人體并返回每個(gè)人體的矩形框位置,識(shí)別人體的靜態(tài)屬性和行為,共支持20余種屬性,包括:性別、年齡階段、衣著(含類別/顏色)、是否戴帽子、是否戴眼鏡、是否背包、是否使用手機(jī)、身體朝向等。
主要適用于監(jiān)控場(chǎng)景的中低空斜拍視角,支持人體輕度重疊、輕度遮擋、背面、側(cè)面、動(dòng)作變化等復(fù)雜場(chǎng)景。
攝像頭硬件選型無(wú)特殊要求,分辨率建議720p以上,更低分辨率的圖片也能識(shí)別,只是效果可能有差異。暫不適用夜間紅外監(jiān)控圖片,后續(xù)會(huì)考慮擴(kuò)展。
二、應(yīng)用場(chǎng)景
1、安防監(jiān)控
識(shí)別人體的性別年齡、衣著外觀等特征,輔助定位追蹤特定人員;監(jiān)測(cè)預(yù)警各類危險(xiǎn)、違規(guī)行為(如公共場(chǎng)所跑跳、抽煙),減少安全隱。
2、智能零售
商場(chǎng)、門店等線下零售場(chǎng)景,識(shí)別入店及路過(guò)客群的屬性信息,收集消費(fèi)者畫像,輔助精準(zhǔn)營(yíng)銷、個(gè)性化推薦、門店選址、流行趨勢(shì)分析等應(yīng)用。
3、線下廣告投放
樓宇、戶外等廣告屏智能化升級(jí),采集人體信息,分析人群屬性,定向投放廣告物料,提升用戶體驗(yàn)和商業(yè)效率。
三、使用攻略
說(shuō)明:本文采用C# 語(yǔ)言,開發(fā)環(huán)境為.Net Core 2.1,采用在線API接口方式實(shí)現(xiàn)。
(1)、登陸百度智能云-管理中心創(chuàng)建 “人體分析”應(yīng)用,獲取 “API Key ”和 “Secret Key” :https://console.bce.baidu.com/ai/?_=1561441540695&fromai=1#/ai/body/overview/index
(2)、根據(jù) API Key 和 Secret Key 獲取 AccessToken。
///
/// 獲取百度access_token
///
/// API Key
/// Secret Key
///
public static string GetAccessToken(string clientId, string clientSecret)
{
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;
JObject jo = (JObject)JsonConvert.DeserializeObject(result);
string token = jo["access_token"].ToString();
return token;
}
(3)、調(diào)用API接口獲取識(shí)別結(jié)果
1、在Startup.cs文件 的Configure(IApplicationBuilder app, IHostingEnvironment env) 方法中開啟虛擬目錄映射功能:
string webRootPath = HostingEnvironment.WebRootPath;//wwwroot目錄
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(webRootPath, "Uploads", "BaiduAIs")),
RequestPath = "/BaiduAIs"
});
2、建立BodySearch.cshtml文件
2.1 前臺(tái)代碼:
??? 由于html代碼無(wú)法原生顯示,只能簡(jiǎn)單說(shuō)明一下:
??? 主要是一個(gè)form表單,需要設(shè)置屬性enctype="multipart/form-data",否則無(wú)法上傳圖片;
??? form表單里面有兩個(gè)控件:
??? 一個(gè)Input:type="file",asp-for="FileUpload" ,上傳圖片用;
??? 一個(gè)Input:type="submit",asp-page-handler="BodyAttr" ,提交并返回識(shí)別結(jié)果。
??? 一個(gè)img:src="@Model.curPath",顯示識(shí)別的圖片。
??? 最后顯示后臺(tái) msg 字符串列表信息。
2.2 后臺(tái)代碼:
[BindProperty]
public IFormFile FileUpload { get; set; }
private readonly IHostingEnvironment HostingEnvironment;
public List msg = new List();
public string curPath { get; set; }
public BodySearchModel(IHostingEnvironment hostingEnvironment)
{
HostingEnvironment = hostingEnvironment;
}
public async Task OnPostBodyAttrAsync()
{
if (FileUpload is null)
{
ModelState.AddModelError(string.Empty, "請(qǐng)先選擇
本地圖片!");
}
if (!ModelState.IsValid)
{
return Page();
}
msg = new List();
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 imgBase64 = GetFileBase64(fileName);
curPath = Path.Combine("/BaiduAIs/", imgName);//需在Startup.cs 文件 的 Configure(IApplicationBuilder app, IHostingEnvironment env)方法中開啟虛擬目錄映射功能
string result = GetBodyeJson(imgBase64, “你的API KEY”, “你的SECRET KEY”);
JObject jo = (JObject)JsonConvert.DeserializeObject(result);
List msgList = jo["person_info"].ToList();
int number = int.Parse(jo["person_num"].ToString());
int curNumber = 1;
msg.Add("人數(shù):" + number);
foreach (JToken ms in msgList)
{
if (number > 1)
{
msg.Add("第 " + (curNumber++).ToString() + " 人:");
}
msg.Add("性別:" + ms["attributes"]["gender"]["name"].ToString());
msg.Add("年齡:" + ms["attributes"]["age"]["name"].ToString());
msg.Add("身體朝向:" + ms["attributes"]["orientation"]["name"].ToString());
msg.Add("下半身服飾:" + ms["attributes"]["lower_wear"]["name"].ToString());
msg.Add("下半身衣著顏色:" + ms["attributes"]["lower_color"]["name"].ToString());
msg.Add("上半身服飾:" + ms["attributes"]["lower_wear"]["name"].ToString());
msg.Add("上半身衣著顏色:" + ms["attributes"]["upper_color"]["name"].ToString());
msg.Add("上身服飾分類:" + ms["attributes"]["upper_wear"]["name"].ToString());
msg.Add("上身服飾紋理:" + ms["attributes"]["upper_wear_texture"]["name"].ToString());
msg.Add("是否戴眼鏡:" + ms["attributes"]["glasses"]["name"].ToString());
msg.Add("是否戴帽子:" + ms["attributes"]["headwear"]["name"].ToString());
msg.Add("是否吸煙:" + ms["attributes"]["smoke"]["name"].ToString());
msg.Add("交通工具:" + ms["attributes"]["vehicle"]["name"].ToString());
msg.Add("使用手機(jī):" + ms["attributes"]["cellphone"]["name"].ToString());
msg.Add("是否撐傘:" + ms["attributes"]["umbrella"]["name"].ToString());
msg.Add("背包:" + ms["attributes"]["bag"]["name"].ToString());
}
return Page();
}
///
/// 上傳文件,返回文件名
///
/// 文件上傳控件
/// 文件絕對(duì)路徑
///
public static async Task 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;
}
///
/// 返回圖片的base64編碼
///
/// 文件絕對(duì)路徑名稱
///
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;
}
///
/// 人體檢測(cè)Json字符串
///
/// 圖片base64編碼
/// API Key
/// Secret Key
///
public static string GetBodyeJson(string strbaser64, string clientId, string clientSecret)
{
string token = GetAccessToken(clientId, clientSecret);
string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_attr?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
string str = "image=" + HttpUtility.UrlEncode(strbaser64);
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();
return result;
}
四、效果測(cè)試
1、頁(yè)面:
2、識(shí)別結(jié)果:
2.1
完整識(shí)別結(jié)果:
人數(shù):2
第 1 人:
性別:女性
年齡:青年
身體朝向:正面
下半身服飾:長(zhǎng)裙
下半身衣著顏色:藍(lán)
上半身服飾:長(zhǎng)裙
上半身衣著顏色:白
上身服飾分類:短袖
上身服飾紋理:純色
是否戴眼鏡:戴眼鏡
是否戴帽子:無(wú)帽
是否吸煙:未吸煙
交通工具:無(wú)交通工具
使用手機(jī):未使用手機(jī)
是否撐傘:未打傘
背包:無(wú)背包
第 2 人:
性別:女性
年齡:青年
身體朝向:正面
下半身服飾:不確定
下半身衣著顏色:不確定
上半身服飾:不確定
上半身衣著顏色:黑
上身服飾分類:短袖
上身服飾紋理:純色
是否戴眼鏡:無(wú)眼鏡
是否戴帽子:無(wú)帽
是否吸煙:未吸煙
交通工具:無(wú)交通工具
使用手機(jī):未使用手機(jī)
是否撐傘:未打傘
背包:無(wú)背包
2.2
完整識(shí)別結(jié)果:
人數(shù):1
性別:男性
年齡:青年
身體朝向:正面
下半身服飾:不確定
下半身衣著顏色:黑
上半身服飾:不確定
上半身衣著顏色:綠
上身服飾分類:長(zhǎng)袖
上身服飾紋理:純色
是否戴眼鏡:無(wú)眼鏡
是否戴帽子:無(wú)帽
是否吸煙:未吸煙
交通工具:無(wú)交通工具
使用手機(jī):未使用手機(jī)
是否撐傘:未打傘
背包:無(wú)背包
2.3
完整識(shí)別結(jié)果:
人數(shù):1
性別:男性
年齡:青年
身體朝向:正面
下半身服飾:長(zhǎng)褲
下半身衣著顏色:黑
上半身服飾:長(zhǎng)褲
上半身衣著顏色:灰
上身服飾分類:長(zhǎng)袖
上身服飾紋理:純色
是否戴眼鏡:無(wú)眼鏡
是否戴帽子:無(wú)帽
是否吸煙:未吸煙
交通工具:無(wú)交通工具
使用手機(jī):未使用手機(jī)
是否撐傘:未打傘
背包:無(wú)背包
根據(jù)識(shí)別結(jié)果可以看出,該接口對(duì)于性別、服飾、服飾類型、顏色等的識(shí)別比較準(zhǔn)確,但是對(duì)于是否吸煙、是否戴眼鏡等識(shí)別就比較差了,還需要再改進(jìn)。