人臉識別開源SDK原始碼
阿新 • • 發佈:2019-01-31
程式碼是呼叫開源SDk的FaceCore關鍵程式碼。附件中有詳細的介面呼叫說明
FaceCore人臉識別開放平臺 (SERVICE INTERFACE PLATFORM)是基於人臉檢測、比對核心業務技術的服務平臺。平臺可為外部合作伙伴提供基於高精度人臉識別技術為基礎的相關服務,例如Api、人臉識別、資料安全等。作為人臉識別的重要開發途徑,FaceCore平臺將推動各行各業定製、創新、進化,並最終促成新商業文明生態圈的建立。我們的使命是把人臉識別技術、規範等一系列核心技術基礎服務,像水、電、煤一樣輸送給所有需要的合作伙伴、開發者、社群媒體、安全機構和各行各業。幫助社會各界通過使用此平臺獲得更豐厚的商業價值。
關鍵程式碼段:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using FaceCore.OpenPlatform.DTO; namespace Com.Facecore.platform { public class FaceClient { private string faceKey = string.Empty; private string faceUrlbase = string.Empty; public FaceClient(string key) { faceKey = key; } #region 特徵值檢測 /// <summary> /// 特徵值檢測 /// </summary> /// <param name="filePath">檔案地址</param> /// <returns></returns> public FaceDetectResponse FaceDetect(string filePath) { if (string.IsNullOrEmpty(filePath))//空 return null; if (!File.Exists(Path.GetFullPath(filePath))) //檔案不存在 return null; FileStream fs = new FileStream(Path.GetFullPath(filePath), FileMode.Open); byte[] bs = new byte[fs.Length]; fs.Read(bs, 0, bs.Length); fs.Close(); fs.Dispose(); return FaceDetect(bs); } /// <summary> /// 特徵值檢測 /// </summary> /// <param name="img">包含人臉的Image物件</param> /// <returns></returns> public FaceDetectResponse FaceDetect(Image img) { if (img == null) return null; var newImage = new Bitmap(img); MemoryStream ms = new MemoryStream(); newImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); newImage.Dispose(); return FaceDetect(ms.ToArray()); } /// <summary> /// 特徵值檢測 /// </summary> /// <param name="bts">圖片物件的byte陣列</param> /// <returns></returns> public FaceDetectResponse FaceDetect(byte[] bts) { FaceHttp http = new FaceHttp(); string image = Convert.ToBase64String(bts); FaceDetectRequest request = new FaceDetectRequest() { faceimage = image }; string url = ConstUrl.URL_CN + ConstUrl.FaceDeteiveResouse + "?appkey=" + faceKey; var value = http.HttpPost(url, Newtonsoft.Json.JsonConvert.SerializeObject(request)); return Newtonsoft.Json.JsonConvert.DeserializeObject<FaceDetectResponse>(value); } #endregion #region 特徵值檢測包含年齡性別 /// <summary> /// 特徵值檢測 /// </summary> /// <param name="filePath">檔案地址</param> /// <returns></returns> public FaceDetectResponse FaceDetectAgeAndSex(string filePath) { if (string.IsNullOrEmpty(filePath))//空 return null; if (!File.Exists(Path.GetFullPath(filePath))) //檔案不存在 return null; FileStream fs = new FileStream(Path.GetFullPath(filePath), FileMode.Open); byte[] bs = new byte[fs.Length]; fs.Read(bs, 0, bs.Length); fs.Close(); fs.Dispose(); return FaceDetectAgeAndSex(bs); } /// <summary> /// 特徵值檢測 /// </summary> /// <param name="img">包含人臉的Image物件</param> /// <returns></returns> public FaceDetectResponse FaceDetectAgeAndSex(Image img) { if (img == null) return null; var newImage = new Bitmap(img); MemoryStream ms = new MemoryStream(); newImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); newImage.Dispose(); return FaceDetectAgeAndSex(ms.ToArray()); } /// <summary> /// 特徵值檢測 /// </summary> /// <param name="bts">圖片物件的byte陣列</param> /// <returns></returns> public FaceDetectResponse FaceDetectAgeAndSex(byte[] bts) { FaceHttp http = new FaceHttp(); string image = Convert.ToBase64String(bts); //http.HttpPost("", ""); FaceDetectRequest request = new FaceDetectRequest() { faceimage = image }; string url = ConstUrl.URL_CN + ConstUrl.FaceDeteiveAgeAndSexResouse + "?appkey=" + faceKey; var value = http.HttpPost(url, Newtonsoft.Json.JsonConvert.SerializeObject(request)); return Newtonsoft.Json.JsonConvert.DeserializeObject<FaceDetectResponse>(value); } #endregion #region 特徵值比較 /// <summary> /// 特徵值比較 /// </summary> /// <param name="facetrue1">特徵值返回串</param> /// <param name="facetrue2">特徵值返回串</param> public FaceCompareResponse FaceContrast(string facetrue1, string facetrue2) { FaceCompareRequest request = new FaceCompareRequest(); request.facefeature1 = facetrue1; request.facefeature2 = facetrue2; string url = ConstUrl.URL_CN + ConstUrl.FaceCompareResouse + "?appkey=" + faceKey; FaceHttp http = new FaceHttp(); return Newtonsoft.Json.JsonConvert.DeserializeObject<FaceCompareResponse>(http.HttpPost(url, Newtonsoft.Json.JsonConvert.SerializeObject(request))); } #endregion } }
人臉比對客戶端程式碼:
using System; using System.Drawing; using System.Windows.Forms; using Com.Facecore.platform; using System.IO; using Com.Facecore.platform.Common; namespace faceView.UcControl { public partial class UcCompar : UserControl { public UcCompar() { InitializeComponent(); ImgSelect = Common.ConvertImage(Path.GetFullPath("Images\\button\\選擇照片-人臉比對認證.png")); ImgSelect_Click = Common.ConvertImage(Path.GetFullPath("Images\\button\\選擇照片-點選-人臉比對認證.png")); ImgCompar = Common.ConvertImage(Path.GetFullPath("Images\\button\\開始比對-人臉比對認證.png")); ImgCompar_Click = Common.ConvertImage(Path.GetFullPath("Images\\button\\開始比對-點選-人臉比對認證.png")); pic1Select.Image = ImgSelect; pic2Select.Image = ImgSelect; picCompar.Image = ImgCompar; } private Image ImgSelect { get; set; } private Image ImgSelect_Click { get; set; } private Image ImgCompar { get; set; } private Image ImgCompar_Click { get; set; } string face1true = string.Empty; string face2true = string.Empty; private string SelectPicture() { var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "JPEG File Interchange Format (*.jpg)|*.jpg;*.jpeg|" + "Windows Bitmap(*.bmp)|*.bmp|" + "Graphics Interchange Format (*.gif)|(*.gif)|" + "Portable Network Graphics (*.png)|*.png|" + "Tag Image File Format (*.tif)|*.tif;*.tiff"; openFileDialog.ShowDialog(); return openFileDialog.FileName; } private void btn1Select_Click(object sender, EventArgs e) { string filename = SelectPicture(); if (!string.IsNullOrEmpty(filename)) { //8c1be718fa976083a6940009f36c056e FaceClient client = new FaceClient("8c1be718fa976083a6940009f36c056e"); byte[] bt = GetFilebyte(filename); var face = client.FaceDetect(filename);//獲取特徵值 pic1.Image = Image.FromStream(new MemoryStream(bt)); if (face != null && face.facemodels.Count > 0) { face1true = face.facemodels[0].base64feature; } else { MessageBox.Show("未讀取到人臉資訊"); pic1.Image = null; face1true = string.Empty; } } else { pic1.Image = null; face1true = string.Empty; } } private byte[] GetFilebyte(string filename) { if (File.Exists(filename)) { FileStream stream = new FileStream(filename, FileMode.Open); byte[] bts = new byte[stream.Length]; stream.Read(bts, 0, bts.Length); stream.Close(); stream.Dispose(); return bts; } return null; } private void btn2Select_Click(object sender, EventArgs e) { string filename = SelectPicture(); if (!string.IsNullOrEmpty(filename)) { FaceClient client = new FaceClient("8c1be718fa976083a6940009f36c056e"); byte[] bt = GetFilebyte(filename); var face = client.FaceDetect(bt);//獲取特徵值 pic2.Image = Image.FromStream(new MemoryStream(bt)); if (face != null && face.facemodels.Count > 0) { face2true = face.facemodels[0].base64feature; } else { MessageBox.Show("未讀取到人臉資訊"); pic2.Image = null; face2true = string.Empty; } } else { pic2.Image = null; face2true = string.Empty; } } private void btnCompar_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(face1true) || string.IsNullOrEmpty(face2true)) { MessageBox.Show("缺少人臉資訊"); return; } FaceClient client = new FaceClient("8c1be718fa976083a6940009f36c056e"); var contrast = client.FaceContrast(face1true, face2true); if (contrast != null) { lblCompar.Text = "相似度" + (contrast.similar * 100).ToString("0.00") + "%"; } } private void pic2Select_MouseEnter(object sender, EventArgs e) { PictureBox pic = (PictureBox)sender; pic.Image = ImgSelect_Click; } private void pic2Select_MouseLeave(object sender, EventArgs e) { PictureBox pic = (PictureBox)sender; pic.Image = ImgSelect; } private void picCompar_MouseEnter(object sender, EventArgs e) { picCompar.Image = ImgCompar_Click; } private void picCompar_MouseLeave(object sender, EventArgs e) { picCompar.Image = ImgCompar; } } }