Java 调用Azure认知服务Demo--Computer API
说明
本文主要介绍使用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的更多相关文章
- Azure认知服务之Face API上手体验
Azure认知服务:Face API Face API是Azure认知服务之一,Face API有两个主要功能: 人脸检测 Face API可在图像中以高精度人脸位置检测多达64个人脸.图像可以通过文 ...
- Azure 认知服务 (3) 计算机视觉API - 分析图像,使用C#代码
<Windows Azure Platform 系列文章目录> 在上一节中Azure 认知服务 (2) 计算机视觉API - 分析图像,笔者介绍了如何使用API测试控制台进行调试. 本章将 ...
- Azure 认知服务 (5) 计算机视觉API - 使用C#代码实现读取图片中的文字(OCR)功能
<Windows Azure Platform 系列文章目录> 在笔者之前的文章:Azure 认知服务 (4) 计算机视觉API - 读取图片中的文字 (OCR) 介绍了使用用户界面,在海 ...
- Azure 认知服务 (4) 计算机视觉API - 读取图片中的文字 (OCR)
<Windows Azure Platform 系列文章目录> 微软Azure认知服务的计算机视觉API,还提供读取图片中的文字功能 在海外的Windows Azure认知服务的读取图片功 ...
- Azure 认知服务 (2) 计算机视觉API - 分析图像
<Windows Azure Platform 系列文章目录> 在上一节内容中,笔者介绍了微软认知服务的概览. 在本节中,笔者将详细介绍微软认知服务中的一种:计算机视觉 (Computer ...
- Azure 认知服务--计算机视觉 API - 分析图像
在本节中,笔者将详细介绍 Azure 认知服务中的一种:计算机视觉 (Computer Vision) API. 我的一个客户有需求,他们需要消费者与自己的产品合照,然后上传到服务器并转发到朋友圈. ...
- Azure认知服务的实际应用-资讯采集推送
Azure认知服务的实际应用-资讯采集推送 演示 实现的是通过使用各种azure服务,每天自动获取资讯.博客,定时推送到公众号的功能! 微信公众号搜索TechViews,或直接扫描二维码关注,每天推送 ...
- Azure 认知服务概述
背景知识 近些年随着机器学习.深度学习等技术的不断发展,人工智能在越来越多的场景得到了应用,如人脸识别.图像识别.语音识别.语音生成.自然语言处理.决策分析等等,让机器拥有了听.说.看和思考的能力,很 ...
- Azure认知服务之表格识别器
认知服务 Azure 认知服务的目标是帮助开发人员创建可以看.听.说.理解甚至开始推理的应用程序. Azure 认知服务中的服务目录可分为五大主要支柱类别:视觉.语音.语言.Web 搜索和决策.开发人 ...
随机推荐
- SpringWeb增删改查
模型类: package com; public class Model { private int id; private String name; private String dtype; pr ...
- 禁止UIWebView随键盘的弹起而往上滚动
问题:当UIWebView中的html有输入框,点击输入框,UIWebView会随键盘的弹起而整体往上移动,收起键盘后,UIWebView无法回到原来的位置; 问题的原因:由于UIWebView继承的 ...
- 机器学习 —— 基础整理(三)生成式模型的非参数方法: Parzen窗估计、k近邻估计;k近邻分类器
本文简述了以下内容: (一)生成式模型的非参数方法 (二)Parzen窗估计 (三)k近邻估计 (四)k近邻分类器(k-nearest neighbor,kNN) (一)非参数方法(Non-param ...
- Nginx+keepalive局域网其它主机ping vip不通
有两台高可用server server1 192.168.11.10 server2 192.168.11.11 vip :192.168.11.12 配置好keepalive之后在server1 ...
- 如何模拟click事件,打开一个a标签链接?
在项目开发过程中,我们经常会碰到通过接口返回一个地址,同时在新的tab页面打开一个网址的情况,最直观的想法就是通过window.open(url)的方式,打开一个新的页面,但是大部分浏览器会遭遇拦截. ...
- VM虚拟机中安装Linux操作系统
本文操作步骤,笔者已实验成功 (前提:正确安装VM并激活) 1,点击新建虚拟机,在页面上选择"自定义",点击下一步 2,进入选择虚拟机硬件兼容页面,这里一般不用操作,直接点击下一步 ...
- 聪明的质监员[NOIP2011]
时间限制:1 s 内存限制:128 MB [问题描述] 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有n个矿石,从 1 到n逐一编号,每个矿石都有自己的重量wi以及价值vi.检 ...
- 《开发技巧》WEB APP开发调试技巧
前言 随着html5和nodejs的兴起.web APP越来越火,一套代码可以多平台使用.减少了很大的开发成本.很多APP中也集成了很多的html5页面,增强很高的应用体验.所以移动端页面也事关重要! ...
- 自定义Git之配置别名
有没有经常敲错命令?比如git status?status这个单词真心不好记. 如果敲git st就表示git status那就简单多了,当然这种偷懒的办法我们是极力赞成的. 我们只需要敲一行命令,告 ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...