说明

本文主要介绍使用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. 线程池与Threadlocal

    线程池与Threadlocal 线程池: 线程池是为了使线程能够得到循环的利用,线程池里面养着一些线程,有任务需要使用线程的时候就往线程池里抓线程对象出来使用.线程池里的线程能够重复使用,所以在资源上 ...

  2. Serv-U权限设置规则

    Serv-U有服务器--域--组--用户四级账号管理规则,每一级都有相应的根目录,目录访问规则和虚拟路径的设置. 同样其他一些重复设置还有很多,如此很容易给人造成混乱--如果我哪里都设置的话会不会造成 ...

  3. mysql内部级联删除

    1,创建user表 属性:id,name 2,创建userInfo表 属性:id,age 在userInfo表中创建外键id,如下图: 在user表中插入两个用户信息 (1,'1'),(2,'2') ...

  4. 将csv格式的文件数据导入mysql中

    示例如下: load data infile 'test.csv'into table `test`fields terminated by ',' optionally enclosed by '& ...

  5. C#开发模式——单例模式

    一.单例模式简介 单例模式是为保证一个对象,在程序运行的任何时刻只有一个实例存在,我们把这种实现方式称之为“单例模式”. 二.单例模式的核心 单例类只有一个实例存在 单例类提供一个全局访问点 单例类通 ...

  6. maven-配置文件配置src/resource下的文件

    classpath*:只找到applicationContext.xml classpath:/找到了所有

  7. (转)Linux服务器安装配置tomcat

    这里首先说明下,Linux下的Tomcat没有32位和64位的区分. tomcat是分windows版本与linux版本的,windows版本一般以exe或者zip格式:linux版本一般以tar.g ...

  8. Django中ORM模型总结(一)[概述,查询语句]

    理解ORM框架 概述 O:(objects)->类和对象. R:(Relation)->关系,关系数据库中的表格. M:(Mapping)->映射. 作用: 可以通过类和类对象就可以 ...

  9. 双向循环链表(C语言描述)(一)

    双向循环链表是链表的一种,它的每个节点也包含数据域和指针域.为了方便程序维护,可以单独为数据域定义一种数据类型,这里以整型为例: typedef int LinkedListData; 双向循环链表( ...

  10. Premiere&After Effects的实时预览插件开发

    一.介绍 Adobe Premiere和After Effects在影视编辑.渲染领域已经得到广泛应用.全景视频在相应工具拼接好后也可以导入Premiere/After Effects后也可进行剪辑. ...