zoukankan      html  css  js  c++  java
  • 微软图像分析 Vision REST API (2)

    微软牛津计划,最近出现了一大批图像处理和识别的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.运行下,呵呵

  • 相关阅读:
    NOJ 1116 哈罗哈的大披萨 【淡蓝】 [状压dp+各种优化]
    Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]
    noj 2069 赵信的往事 [yy题 无限gcd]
    noj 2068 爱魔法的露露 [线性扫一遍]
    Codeforces Round #275 (Div. 2) B. Friends and Presents 二分+数学
    Word2007文档中怎么输入上标下标
    Linux下查找命令
    矩阵求逆
    LRC算法在人脸识别中应用
    java从txt文档读写数据
  • 原文地址:https://www.cnblogs.com/yanmantianxia/p/VisionRESTAPI.html
Copyright © 2011-2022 走看看