百度人臉對比

dotnetcms.org工作室發表於2018-07-23

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Baidu.Aip.Face;
using Newtonsoft.Json.Linq;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        // 設定APPID/AK/SK
    
        var API_KEY = " 訂單";
        var SECRET_KEY = "";

        var client = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY);
        client.Timeout = 60000;  // 修改超時時間

        string data1 = ReadImgToString(FileUpload1);
        string data2 = ReadImgToString(FileUpload2);


        var pic1= new JObject
        {
            {"image", data1},
            {"image_type", "BASE64"},
            {"face_type", "LIVE"}
        };


        var pic2= new JObject
        {
            {"image",data2},
            {"image_type", "BASE64"},
            {"face_type", "IDCARD"}
           
        };
 


        JArray faces = new JArray {  pic1,pic2   };

    
        var result = client.Match(faces);

        txt_result.Text = result.ToString();

          // R r = Newtonsoft.Json.JsonConvert.DeserializeObject<R>(result);

          // txt_r.Text = r.score;
    }











    public string ReadImgToString(FileUpload f)
    {
        Stream fstream = f.PostedFile.InputStream;
        byte[] imgData = new byte[fstream.Length];
        fstream.Read(imgData, 0, imgData.Length);
        fstream.Close();
        string imgdata = Convert.ToBase64String(imgData);
        return  imgdata;
    }


}




public class R
{

    public string score;
    public item[] face_list;
}

public class item
{
    public string face_token;
}

https://files.cnblogs.com/files/mqingqing123/baidu_face_api_Bin.rar

 

    protected void Page_Load(object sender, EventArgs e)
    {
        string base64 = Request["data"];

        //var bytes = Convert.FromBase64String(base64);
        //using (var imageFile = new FileStream(Server.MapPath("a10.jpg"), FileMode.Create))
        //{
        //    imageFile.Write(bytes, 0, bytes.Length);
        //    imageFile.Flush();
        //}

        // System.Drawing.Image img = Base64ToImage(base64);

        //img.Save(Server.MapPath("a0.jpg"),System.Drawing.Imaging.ImageFormat.Jpeg);
        Base64ToImage(base64);
    }

    public System.Drawing.Image Base64ToImage(string base64String)
    {
        // Convert base 64 string to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        // Convert byte[] to Image
        using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
            image.Save(Server.MapPath("x.jpg"));
            return image;
        }
    }


    // public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
    // {
    //using (MemoryStream ms = new MemoryStream())
    //{
    //    // Convert Image to byte[]
    //    image.Save(ms, format);
    //    byte[] imageBytes = ms.ToArray();

    //    // Convert byte[] to base 64 string
    //    string base64String = Convert.ToBase64String(imageBytes);
    //    return base64String;
    //}
    // }

 

相關文章