问题是如何加载图像文件并将其作为对象传递给 Microsoft Computer Vision API,Microsoft 网站中的所有示例代码都是从 url 读取图像。
// This sample uses the Apache HTTP client library(org.apache.httpcomponents:httpclient:4.2.4)
// and the org.json library (org.json:json:20170516).
package com.mparnisari.test;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
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.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
public class Main
{
public static final String subscriptionKey = "MY-KEY-HERE";
public static final String uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze";
public static void main(String[] args)
{
HttpClient httpclient = new DefaultHttpClient();
try
{
URIBuilder builder = new URIBuilder(uriBase);
builder.setParameter("visualFeatures", "Categories,Description,Color");
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", subscriptionKey);
// Request body.
BufferedImage image = null;
File f = null;
f = new File("C:\\Coffee.jpg"); //image file path
image = ImageIO.read(f);
File file = new File("C:\\Coffee.jpg");
FileEntity reqEntityF =
new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM);
request.setEntity(reqEntityF);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null)
{
// Format and display the JSON response.
String jsonString = EntityUtils.toString(entity);
JSONObject json = new JSONObject(jsonString);
System.out.println("REST Response:\n");
System.out.println(json.toString(2));
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
输出为:
REST Response:
{
"code": "BadArgument",
"requestId": "7ecf2198-1b7f-44d0-9cc2-e05e28791281",
"message": "JSON format error."
}
如 stackoverflow 指南中的其他帖子所述,使用 FileEntity
上传图像。但它不起作用。
我认为这部分应该以某种方式重构以读取图像而不是 URL。
// Execute the REST API call and get the response entity.
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
让我知道解决此问题的最佳解决方案是什么,因为如果可以将图像从本地传递到 API,那么使用 for 循环来分析图像集会很棒。
最佳答案
正如@Jon所说,您需要更改content-type
header 。
// Request headers.
request.setHeader("Content-Type", "application/octet-stream");
// Request body.
File file = new File(imagePath);
FileEntity reqEntity = new FileEntity(file);
request.setEntity(reqEntity);
关于java - 如何使用 JAVA 将本地镜像而不是 URL 发送到 Microsoft 计算机视觉 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49463736/