Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将图像发送到GCP Vision API_Java_Android_Json_Api_Google Cloud Platform - Fatal编程技术网

Java 如何将图像发送到GCP Vision API

Java 如何将图像发送到GCP Vision API,java,android,json,api,google-cloud-platform,Java,Android,Json,Api,Google Cloud Platform,我将使用谷歌云视觉API 在教程中,它说我需要将图像发送到他们的谷歌云存储,然后使用该链接,我需要向API发出请求。 因此,该方案如下所示: 手机照片(本地存储)--下载-->GC存储--获取链接-->使用此链接将请求发送到GC Vision API--获取JSON-->使用JSON 所以问题是。 为什么我需要在云中存储图像?仅用于链接?我可以不使用GC存储直接将图像发送到Vision API吗? 因此,该计划: 手机照片(本地存储)--下载-->到GC Vision API--获取JSON--

我将使用谷歌云视觉API

在教程中,它说我需要将图像发送到他们的谷歌云存储,然后使用该链接,我需要向API发出请求。 因此,该方案如下所示:

手机照片(本地存储)--下载-->GC存储--获取链接-->使用此链接将请求发送到GC Vision API--获取JSON-->使用JSON

所以问题是。 为什么我需要在云中存储图像?仅用于链接?我可以不使用GC存储直接将图像发送到Vision API吗? 因此,该计划:


手机照片(本地存储)--下载-->到GC Vision API--获取JSON-->使用JSON

是您可以直接向Vision API发送图像,如下所示:

package code.logicbeat.vision.controller;

import java.util.Collections;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.EntityAnnotation;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.protobuf.ByteString;
@RestController
public class VisionController {

    @Autowired
    private ImageAnnotatorClient imageAnnotatorClient;

    @PostMapping("/vision")
    public String uploadImage(@RequestParam("file") MultipartFile file) throws Exception {

        byte[] imageBytes = StreamUtils.copyToByteArray(file.getInputStream());
        Image image = Image.newBuilder().setContent(ByteString.copyFrom(imageBytes)).build();

        // Sets the type of request to label detection, to detect broad sets of
        // categories in an image.
        Feature feature = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
        AnnotateImageRequest request = AnnotateImageRequest.newBuilder().setImage(image).addFeatures(feature).build();
        BatchAnnotateImagesResponse responses = this.imageAnnotatorClient
            .batchAnnotateImages(Collections.singletonList(request));
        StringBuilder responseBuilder = new StringBuilder("<table border=\"1\">");
        responseBuilder.append("<tr><th>description</th><th>score</th></tr>");
        // We're only expecting one response.
        if (responses.getResponsesCount() == 1) {
            AnnotateImageResponse response = responses.getResponses(0);
            if (response.hasError()) {
                System.out.println(response.getError());
                throw new Exception(response.getError().getMessage());
            }

            for (EntityAnnotation annotation : response.getLabelAnnotationsList()) {
                responseBuilder.append("<tr><td>").append(annotation.getDescription()).append("</td><td>")
                    .append(annotation.getScore()).append("</td></tr>");
            }
        }
        responseBuilder.append("</table>");
        return responseBuilder.toString();
    }
}
package code.logicbeat.vision.controller;
导入java.util.Collections;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.util.StreamUtils;
导入org.springframework.web.bind.annotation.PostMapping;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.multipart.MultipartFile;
导入com.google.cloud.vision.v1.ImageRequest;
导入com.google.cloud.vision.v1.ImageResponse;
导入com.google.cloud.vision.v1.batch注释图像响应;
导入com.google.cloud.vision.v1.EntityAnnotation;
导入com.google.cloud.vision.v1.Feature;
导入com.google.cloud.vision.v1.Image;
导入com.google.cloud.vision.v1.ImageAnnotatorClient;
导入com.google.protobuf.ByteString;
@RestController
公共类VisionController{
@自动连线
私有ImageAnnotatorClient ImageAnnotatorClient;
@后映射(“/vision”)
公共字符串uploadImage(@RequestParam(“文件”)MultipartFile文件)引发异常{
byte[]imageBytes=StreamUtils.copyToByteArray(file.getInputStream());
Image Image=Image.newBuilder().setContent(ByteString.copyFrom(imageBytes)).build();
//设置要标记检测的请求类型,以检测广泛的
//图像中的类别。
Feature Feature=Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
AnnotateImageRequest请求=AnnotateImageRequest.newBuilder().setImage(图像).addFeatures(特征).build();
BatchAnnotateImagesResponse Response=this.imageAnnotatorClient
.batchAnnotateImages(Collections.singletonList(请求));
StringBuilder responseBuilder=新StringBuilder(“”);
append(“descriptionscore”);
//我们只期待一个回应。
if(responses.getResponseCount()==1){
AnnotateImageResponse=responses.getResponses(0);
if(response.hasError()){
System.out.println(response.getError());
抛出新异常(response.getError().getMessage());
}
for(EntityAnnotation:response.GetLabelNotationsList()){
responseBuilder.append(“”).append(annotation.getDescription()).append(“”)
.append(annotation.getScore()).append(“”);
}
}
responseBuilder.append(“”);
返回responseBuilder.toString();
}
}
请查看我的回购示例的链接以供参考:

此外,您还可以检查此链接:

参见