说明

本文主要介绍使用Java代码,基于HTTP请求调用Microsoft Azure的认知服务。图片来源分别介绍了使用公网的URL和上传本地图片。

依赖的jar包下载地址

key的获取需要登录到Azure门户创建相应的服务获取;

中国版Cognitive Service开发参考链接

使用公网URL作为图片地址

// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
package buct.edu.cn; import java.net.URI; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; public class Describe { public static void main(String[] args) { HttpClient httpclient = HttpClients.createDefault(); try
{
URIBuilder builder = new URIBuilder("https://api.cognitive.azure.cn/vision/v1.0/describe"); builder.setParameter("maxCandidates", "1");
builder.setParameter("language", "en"); URI uri = builder.build();
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", "<key>"); // Request body
StringEntity reqEntity = new StringEntity("{\"url\":\"http://pic30.nipic.com/20130606/12948584_155615545180_2.jpg\"}"); //URL图片地址
request.setEntity(reqEntity); HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity(); if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}

读取本地图片

// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
package buct.edu.cn; import java.io.FileInputStream;
import java.net.URI; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; public class DescribeStream { public static void main(String[] args) { HttpClient httpclient = HttpClients.createDefault(); try
{
URIBuilder builder = new URIBuilder("https://api.cognitive.azure.cn/vision/v1.0/describe"); builder.setParameter("maxCandidates", "1");
//builder.setParameter("details", "{string}");
builder.setParameter("language", "en"); URI uri = builder.build();
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "application/octet-stream");
request.setHeader("Ocp-Apim-Subscription-Key", "<key>"); // Request body
String pic_path = "D:\\test.jpg"; //本地图片地址 System.out.println(pic_path); FileInputStream is = new FileInputStream(pic_path);
int i = is.available(); // 得到文件大小
byte data[] = new byte[i];
is.read(data); // 读数据 将数据读到data中
is.close(); ByteArrayEntity bae = new ByteArrayEntity(data);
request.setEntity(bae); HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity(); if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}

Java 调用Azure认知服务Demo--Computer API的更多相关文章

  1. Azure认知服务之Face API上手体验

    Azure认知服务:Face API Face API是Azure认知服务之一,Face API有两个主要功能: 人脸检测 Face API可在图像中以高精度人脸位置检测多达64个人脸.图像可以通过文 ...

  2. Azure 认知服务 (3) 计算机视觉API - 分析图像,使用C#代码

    <Windows Azure Platform 系列文章目录> 在上一节中Azure 认知服务 (2) 计算机视觉API - 分析图像,笔者介绍了如何使用API测试控制台进行调试. 本章将 ...

  3. Azure 认知服务 (5) 计算机视觉API - 使用C#代码实现读取图片中的文字(OCR)功能

    <Windows Azure Platform 系列文章目录> 在笔者之前的文章:Azure 认知服务 (4) 计算机视觉API - 读取图片中的文字 (OCR) 介绍了使用用户界面,在海 ...

  4. Azure 认知服务 (4) 计算机视觉API - 读取图片中的文字 (OCR)

    <Windows Azure Platform 系列文章目录> 微软Azure认知服务的计算机视觉API,还提供读取图片中的文字功能 在海外的Windows Azure认知服务的读取图片功 ...

  5. Azure 认知服务 (2) 计算机视觉API - 分析图像

    <Windows Azure Platform 系列文章目录> 在上一节内容中,笔者介绍了微软认知服务的概览. 在本节中,笔者将详细介绍微软认知服务中的一种:计算机视觉 (Computer ...

  6. Azure 认知服务--计算机视觉 API - 分析图像

    在本节中,笔者将详细介绍 Azure 认知服务中的一种:计算机视觉 (Computer Vision) API. 我的一个客户有需求,他们需要消费者与自己的产品合照,然后上传到服务器并转发到朋友圈. ...

  7. Azure认知服务的实际应用-资讯采集推送

    Azure认知服务的实际应用-资讯采集推送 演示 实现的是通过使用各种azure服务,每天自动获取资讯.博客,定时推送到公众号的功能! 微信公众号搜索TechViews,或直接扫描二维码关注,每天推送 ...

  8. Azure 认知服务概述

    背景知识 近些年随着机器学习.深度学习等技术的不断发展,人工智能在越来越多的场景得到了应用,如人脸识别.图像识别.语音识别.语音生成.自然语言处理.决策分析等等,让机器拥有了听.说.看和思考的能力,很 ...

  9. Azure认知服务之表格识别器

    认知服务 Azure 认知服务的目标是帮助开发人员创建可以看.听.说.理解甚至开始推理的应用程序. Azure 认知服务中的服务目录可分为五大主要支柱类别:视觉.语音.语言.Web 搜索和决策.开发人 ...

随机推荐

  1. mybaits错误解决:There is no getter for property named 'parentId ' in class 'java.lang.String'

    在使用mybaitis传参数的时候,如果仅传入一个类型为String的参数,那么在 xml文件中应该使用_parameter来代替参数名. 比如mapper中如下方法,只有一个String值 publ ...

  2. 简单谈谈js中的MVC

    MVC是什么? MVC是一种架构模式,它将应用抽象为3个部分:模型(数据).视图.控制器(分发器). 本文将用一个经典的例子todoList来展开(代码在最后). 一个事件发生的过程(通信单向流动): ...

  3. NOIP模拟:能源(二分答案)

    题目描述 小美为了拯救世界能源危机,她准备了 n 台蓄电池.一开始每台蓄电池有 ai 个单位的能量. 现在她想把 n 台蓄电池调整到能量相同.对于每台蓄电池可以给另一台蓄电池传递能量.但是会有能量损耗 ...

  4. Java 9 揭秘(15. 增强的弃用注解)

    Tips 做一个终身学习的人. 主要介绍以下内容: 如何弃用API @deprecate Javadoc标签和@Deprecation注解在弃用的API中的角色 用于生成弃用警告的详细规则 在JDK ...

  5. Scikit-Learn与决策树

    Scikit-Learn(决策树)可以用于方法分类和回归. 一.分类 sklearn.tree.DecisionTreeClassifier(criterion='gini', splitter='b ...

  6. FillConsoleOutputAttribute 函数--指定区域填充控制台输出属性

    FillConsoleOutputAttribute函数 来源:https://msdn.microsoft.com/en-us/library/windows/desktop/ms682663(v= ...

  7. (转)示例化讲解RIP路由更新机制

      目录(?)[+]   以下内容摘自最新上市的“四大金刚”图书之一<Cisco路由器配置与管理完全手册>(第二版)(其它三本分别为<Cisco交换机配置与管理完全手册>(第二 ...

  8. 【HTML】dl dt dd

    摘要 看到没怎么使用过的html 标签,记录下 定义 dl 类似于 ul ,无任何样式,自定义列表容器, ul 为无序列表容器,ol 为有序列表容器 dt dd 类似于 li ,无任何样式,为帮助实现 ...

  9. 抓取60000+QQ空间说说做一次数据分析

    对于QQ空间的数据一直来是垂涎不已,老早就想偷过来研究研究,这几天闲下来便开始动手... 整个程序的流程为:登录-->获取cookie-->获取所有的好友qq_number-->根据 ...

  10. docker在Centos上的安装

    Centos6安装docker 系统:centos6.5 内核:3.10.107-1(已升级),docker对RHEL/Centos的最低内核支持是2.6.32-431,epel源的docker版本推 ...