微软图像分析 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.运行下,呵呵

时间: 2024-10-26 01:12:28

微软图像分析 Vision REST API (2)的相关文章

微软将推出新API 融入Bing搜索知识

据<VB>报道,本周四,微软宣布将推出一个新的应用程序编程接口(API),以方便开发商将Bing的专业知识应用于他们开发的应用程序.开发商将能通过该API“发掘”Bing在人.地点.事物领域的相关知识.也就是说,当用户在使用一个应用程序时,用户不必特意去搜索这个应用程序的相关消息,该API可以将Bing知道的有关该App的消息直接告诉用户.微软搜索总经理Ryan Gavin称API在开发和测试阶段该是免费使用的. 包装袋 http://www.biyinjishi.com/products/a

PHP使用微软认知服务Face API

下面主要介绍基于PHP语言,基于guzzle类库,调用微软最新推出的认知服务:人脸识别. 实验环境: IDE:Eclipse for PHP Developers Version: Neon.1 Release (4.6.1) Server:WampServer Version 2.5 HttpClient:guzzle 1. 使用composer安装Guzzle composer.json文件 { "require": { "guzzlehttp/guzzle":

微软提供的虚拟磁盘API

https://msdn.microsoft.com/en-us/library/windows/desktop/dd323684(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/dd323699(v=vs.85).aspx 例子: http://stackoverflow.com/questions/6485579/creating-vhd-files-with-the-virtualdisk-api

ASP.NET Web API是什么?

[翻译]ASP.NET Web API是什么? 说明:随微 软ASP.NET MVC 4一起发布的还有一个框架,叫做ASP.NET Web API.目前国内关注这项技术的人似乎还很少,这方面的文章也不多见.开发Web应用程序也许可以只用MVC这样的技术,而不用这项Web API技术,但如果用了,会给你的应用程序带来极大的好处.为此,本人转载并翻译了以下这篇文章,后面还会陆续翻译该项技术的一些官方教程.大家一起学 习,共同提高. Microsoft ASP.NET: What's This New

【翻译】ASP.NET Web API是什么?

参考页面: http://www.yuanjiaocheng.net/entity/linq-to-entities-projection.html http://www.yuanjiaocheng.net/entity/dbset-class.html http://www.yuanjiaocheng.net/entity/dbentityentry-class.html http://www.yuanjiaocheng.net/entity/change-tracking.html http

ASP.NET Web API系列教程(目录)(转)

注:微软随ASP.NET MVC 4一起还发布了一个框架,叫做ASP.NET Web API.这是一个用来在.NET平台上建立HTTP服务的Web API框架,是微软的又一项令人振奋的技术.目前,国内对此关注的人似乎还不多,有关ASP.NET Web API的文章也不多见.为此,本人打算对微软ASP.NET Web API官方网站上的一些教程进行翻译,以期让更多的国人了解.学习和使用这项ASP.NET Web API. ASP.NET Web API系列教程目录 Introduction:Wha

利用微软认知服务实现语音识别功能

想实现语音识别已经很久了,也尝试了许多次,终究还是失败了,原因很多,识别效果不理想,个人在技术上没有成功实现,种种原因,以至于花费了好多时间在上面.语音识别,我尝试过的有科大讯飞.百度语音,微软系.最终还是喜欢微软系的简洁高效.(勿喷,纯个人感觉) 最开始自己的想法是我说一句话(暂且在控制台上做Demo),控制台程序能识别我说的是什么,然后显示出来,并且根据我说的信息,执行相应的行为.(想法很美好,现实很糟心)初入语音识别,各种错误各种来,徘徊不定的选择哪家公司的api,百度上查找各种语音识别的

MAC OS X API知识摘抄

本文为信息为网上各个地方收集整理Carbon和Cocoa,Toolbox,POSIX,JAVA并列成为Mac OS X五个主要的API.与Cocoa相较之下,Carbon是非物件导向(Procedural)编程语言API,而Cocoa是面向对象(Object Oriented)的编程语言API.Carbon是比Cocoa更为低层次的API,比较类似于微软视窗操作系统的Win32 API.调用Carbon的程序可以使用包括C,C++在内的多种编程语言.而Cocoa只能支持Obejctive-C和J

微软认知服务开发实践(3) - 人脸识别

前言 人们对人脸识别的研究已经有很长一段时间,起初局限于获取基础的人脸信息,随着机器学习领域的发展,人脸识别的应用更加广泛,已经可以被用于人脸搜索.人脸鉴权等相关应用.本文将针对微软认知服务中提供的人脸识别API的调用方法进行一些初步的讲解. Face API Face API中提供了3方面功能: 人脸检测 人脸分组 人脸识别(搜索) 首先是人脸检测,主要是指传统概念上的人脸识别功能,识别图片中的人的面孔,给出人脸出现的坐标区域,并根据识别出来的人脸分析出一些基本的信息(例如年龄). 其次是人脸