Azure Face API 无法处理本地文件
Azure Face API not working with local file
我一直在尝试从我的计算机向此 API 发送图像,但我只收到以下错误:{"error":{"code":"InvalidImageSize","message":"Image size is too small."}}
我的代码如下。
我有一个使用此方法的 PostRequestClass:
public void sendImageRequest(String imagePath) {
try {
HttpClient httpClient = new DefaultHttpClient();
File file = new File(imagePath);
FileEntity reqEntity = new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM);
reqEntity.setChunked(false);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
this.responseResult = EntityUtils.toString(entity);
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
我的主要是这个:
public class Test {
public static void main(String[] args) throws URISyntaxException {
PostRequest p = new PostRequest(
"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceAttributes=emotion"
);
p.addHeader("Content-Type", "application/octet-stream");
p.addHeader("Ocp-Apim-Subscription-Key", "my-api-key");
p.sendImageRequest("/Users/user/Desktop/image.jpg");
System.out.println(p.getResponseResult());
}
}
我用下面的代码解决了它:
public void sendImageRequest(String imagePath) {
try {
HttpClient httpClient = new DefaultHttpClient();
File file = new File(imagePath);
FileInputStream fileInputStreamReader = new FileInputStream(file);
byte[] bytes = new byte[(int)file.length()];
fileInputStreamReader.read(bytes);
ByteArrayEntity reqEntity = new ByteArrayEntity(bytes, ContentType.APPLICATION_OCTET_STREAM);
request.setEntity(reqEntity);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
this.responseResult = EntityUtils.toString(entity);
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
转到 https://azure.microsoft.com/en-us/services/cognitive-services/face/ 并单击 "API reference"。
它将带您到 Face API 参考页 https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236
The Face API 文档说 "JPEG, PNG, GIF (the first frame), and BMP format are supported. The allowed image file size is from 1KB to 4MB."
在标题 "Error code and message returned in JSON" 下,
它说,"InValidImageSize" 表示 "The valid image file size should be larger than or equal to 1KB."
我一直在尝试从我的计算机向此 API 发送图像,但我只收到以下错误:{"error":{"code":"InvalidImageSize","message":"Image size is too small."}}
我的代码如下。 我有一个使用此方法的 PostRequestClass:
public void sendImageRequest(String imagePath) {
try {
HttpClient httpClient = new DefaultHttpClient();
File file = new File(imagePath);
FileEntity reqEntity = new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM);
reqEntity.setChunked(false);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
this.responseResult = EntityUtils.toString(entity);
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
我的主要是这个:
public class Test {
public static void main(String[] args) throws URISyntaxException {
PostRequest p = new PostRequest(
"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceAttributes=emotion"
);
p.addHeader("Content-Type", "application/octet-stream");
p.addHeader("Ocp-Apim-Subscription-Key", "my-api-key");
p.sendImageRequest("/Users/user/Desktop/image.jpg");
System.out.println(p.getResponseResult());
}
}
我用下面的代码解决了它:
public void sendImageRequest(String imagePath) {
try {
HttpClient httpClient = new DefaultHttpClient();
File file = new File(imagePath);
FileInputStream fileInputStreamReader = new FileInputStream(file);
byte[] bytes = new byte[(int)file.length()];
fileInputStreamReader.read(bytes);
ByteArrayEntity reqEntity = new ByteArrayEntity(bytes, ContentType.APPLICATION_OCTET_STREAM);
request.setEntity(reqEntity);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
this.responseResult = EntityUtils.toString(entity);
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
转到 https://azure.microsoft.com/en-us/services/cognitive-services/face/ 并单击 "API reference"。
它将带您到 Face API 参考页 https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236
The Face API 文档说 "JPEG, PNG, GIF (the first frame), and BMP format are supported. The allowed image file size is from 1KB to 4MB."
在标题 "Error code and message returned in JSON" 下, 它说,"InValidImageSize" 表示 "The valid image file size should be larger than or equal to 1KB."