码迷,mamicode.com
首页 > Windows程序 > 详细

微软图像分析 Vision REST API (2)

时间:2015-07-08 14:19:06      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

微软牛津计划,最近出现了一大批图像处理和识别的API,闲来无事研究了下,跟大家分享下心得

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替换

using System;
using System.Text;
using System.Web;
using System.IO;
using System.Net;
using System.Drawing;

namespace MyVisionRESTApi
{
    class Program
    {
        public static string subscriptionKey = @"Your Subscription key";

        public static string imageUrl = @"{‘Url‘:‘http://www.ytravelblog.com/wp-content/uploads/2010/10/Vastness-of-the-Salar-de-Uyuni1.jpg‘}";
        public static string localpath = @"C:\Vastness-of-the-Salar-de-Uyuni1.jpg";
        public static string thumbnailSavePath = @"C:\Vision\";
        static void Main(string[] args)
        {
            // Image Uri
            AnalyzeAnImage(imageUrl, null);
            GenerateThumbnail(imageUrl, null);
            OCRApi(imageUrl, null);
            Console.ReadLine();
            
            // local Image
            AnalyzeAnImage(null, localpath);
            GenerateThumbnail(null, localpath);
            OCRApi(null, localpath);
            Console.ReadLine();
        }

        public static void AnalyzeAnImage(string imageUrl, string localPath)
        {
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Specify values for optional parameters, as needed
            queryString["visualFeatures"] = "All";

            // Specify your subscription key
            queryString["subscription-key"] = subscriptionKey;

            // Specify values for path parameters (shown as {...})
            var uri = "https://api.projectoxford.ai/vision/v1/analyses?" + queryString;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";

            // Specify request body
            if (imageUrl != null)
            {
                byte[] byteData = Encoding.UTF8.GetBytes(imageUrl);
                request.ContentType = @"application/json";
                request.ContentLength = byteData.Length;
                var responseString = "";
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(byteData, 0, byteData.Length);
                }

                var response = (HttpWebResponse)request.GetResponse();
                responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

                Console.WriteLine(responseString);
            }

            if (localPath != null)
            {
                request.ContentType = "application/octet-stream";
                byte[] img = File.ReadAllBytes(localpath);
                request.ContentLength = img.Length;
                var responseString = "";
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(img, 0, img.Length);
                }

                var response = (HttpWebResponse)request.GetResponse();
                responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                Console.WriteLine(responseString);
            }           
        }

        public static void OCRApi(string imageUrl, string localPath)
        {
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Specify values for optional parameters, as needed
            queryString["language"] = "en";
            queryString["detectOrientation "] = "true";

            // Specify your subscription key
            queryString["subscription-key"] = subscriptionKey;

            // Specify values for path parameters (shown as {...})
            var uri = "https://api.projectoxford.ai/vision/v1/ocr?" + queryString;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";

            // Specify request body
            if (imageUrl != null)
            {
                byte[] byteData = Encoding.UTF8.GetBytes(imageUrl);
                request.ContentType = @"application/json";
                request.ContentLength = byteData.Length;
                var responseString = "";
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(byteData, 0, byteData.Length);
                }

                var response = (HttpWebResponse)request.GetResponse();
                responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

                Console.WriteLine(responseString);
            }

            if (localPath != null)
            {
                request.ContentType = "application/octet-stream";
                byte[] img = File.ReadAllBytes(localpath);
                request.ContentLength = img.Length;
                var responseString = "";
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(img, 0, img.Length);
                }

                var response = (HttpWebResponse)request.GetResponse();
                responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                Console.WriteLine(responseString);
            }           
        }

        public static void GenerateThumbnail(string imageUrl, string localPath)
        {
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Specify values for the following required parameters
            queryString["width"] = "50";
            queryString["height"] = "60";

            // Specify values for optional parameters, as needed
            queryString["smartCropping"] = "true";

            // Specify your subscription key
            queryString["subscription-key"] = subscriptionKey;

            // Specify values for path parameters (shown as {...})
            var uri = "https://api.projectoxford.ai/vision/v1/thumbnails?" + queryString;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";

            // Specify request body
            if (imageUrl != null)
            {
                byte[] byteData = Encoding.UTF8.GetBytes(imageUrl);
                request.ContentType = @"application/json";
                request.ContentLength = byteData.Length;
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(byteData, 0, byteData.Length);
                }

                var response = (HttpWebResponse)request.GetResponse();

                Image image = Image.FromStream(response.GetResponseStream());
                image.Save(thumbnailSavePath + Guid.NewGuid().ToString() + @".jpg");

                Console.WriteLine("Thumbnail Width: {0}", image.Width);
                Console.WriteLine("Thumbnail Height: {0}", image.Height);
                Console.WriteLine("Thumbnail path: {0}", thumbnailSavePath + Guid.NewGuid().ToString() + @".jpg");
            }

            if (localPath != null)
            {
                request.ContentType = "application/octet-stream";
                byte[] img = File.ReadAllBytes(localpath);
                request.ContentLength = img.Length;
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(img, 0, img.Length);
                }

                var response = (HttpWebResponse)request.GetResponse();
                Image image = Image.FromStream(response.GetResponseStream());
                image.Save(thumbnailSavePath + Guid.NewGuid().ToString() + @".jpg");

                Console.WriteLine("Thumbnail Width: {0}", image.Width);
                Console.WriteLine("Thumbnail Height: {0}", image.Height);
                Console.WriteLine("Thumbnail path: {0}", thumbnailSavePath + Guid.NewGuid().ToString() + @".jpg");
            }  
        }
    }
}

4.运行下,呵呵

微软图像分析 Vision REST API (2)

标签:

原文地址:http://www.cnblogs.com/yanmantianxia/p/VisionRESTAPI.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!