微软图像分析 Face Verify REST API (2)

女盆友,说他们公司签到,刷脸。我就告诉她,呵呵, so easy, 一局LOL 的时间,搞定人脸识别,刷到?,还能测你颜值呢!

微软牛津计划,人脸识别, 能够验证你face,要不要???

API portal地址:https://www.projectoxford.ai/doc/vision/visual-features

1.首先注册一个订阅号Subscription Management:https://www.projectoxford.ai/doc/general/subscription-key-mgmt

2. 创建一个C#的project, 添加code,

3. 将program.cs的code用下面code替换

class Program
    {
        static void Main(string[] args)
        {
            Verification.test();
            Console.ReadLine();
        }
        
    }

3. Add a detection.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net;
using System.IO;

namespace samplecode
{
    public class Detection
    {
        public static void test()
        {
            DetectFace("KidBigFace.jpg");            
        }

        public static string DetectFace(string imagefile)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.projectoxford.ai/face/v0/detections?subscription-key=***");
                request.Method = "POST";
                request.ContentType = "application/octet-stream"; 
                string url = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, imagefile);
                byte[] requestBody = LoadByteData(url);
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(requestBody, 0, requestBody.Length);

                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        var responseString = response.GetResponseStream();
                        using (var reader = new StreamReader(responseString))
                        {
                            string result = reader.ReadToEnd();
                            Console.Write(response.StatusCode + result);
                            if (response.StatusCode == HttpStatusCode.OK)
                            {                                
                                string[] r = result.Split('"');
                                Console.Write(r[3]);
                                return r[3];
                            }
                            else
                                return "";
                        }
                    }
                }
            }
            catch (WebException wex)
            {
                //verify
                var response = (HttpWebResponse)wex.Response;
                using (var stream = wex.Response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    Console.Write(response.StatusCode + reader.ReadToEnd());
                }
                return "";
            }
        }

        private static byte[] LoadByteData(string filePath)
        {
            FileStream file = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[file.Length];
            file.Read(bytes, 0, bytes.Length);
            file.Close();
            return bytes;
        }
    }
}

 4. 添加 verification.cs, 图片 "Daughter2.jpg", “Daughter3.jpg”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net;
using System.IO;

namespace samplecode
{
    public class Verification
    {
        public static void test()
        {
            string faceid1 = Detection.DetectFace("Daughter2.jpg");
            string faceid2 = Detection.DetectFace("Daughter3.jpg");
            try
            {
                // initial data. 

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.projectoxford.ai/face/v0/verifications?subscription-key=****");
                request.Method = "POST";
                request.ContentType = "application/json";

                string requestdata = "{\"faceId1\":\""+faceid1+"\",\"faceId2\":\""+faceid2+"\"}";
                byte[] requestBody = Encoding.UTF8.GetBytes(requestdata);
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(requestBody, 0, requestBody.Length);

                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        var responseString = response.GetResponseStream();
                        using (var reader = new StreamReader(responseString))
                        {
                            string result = reader.ReadToEnd();
                            Console.Write("\r\n");
                            Console.Write(response.StatusCode + result);

                        }
                    }
                }
            }
            catch (WebException wex)
            {
                //verify
                var response = (HttpWebResponse)wex.Response;
                using (var stream = wex.Response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    Console.Write(response.StatusCode + reader.ReadToEnd());
                }
            }
        }
    }
}

 4.想看看 验证的信息吗?

你自己试试就知道了,so  easy。。。

呵呵,高达桑 吧

 

posted on 2015-07-22 13:35  言满天下  阅读(1005)  评论(0编辑  收藏  举报

导航