gpt4 book ai didi

java - 如何将图像发送到 GCP Vision API

转载 作者:行者123 更新时间:2023-12-01 06:05:02 30 4
gpt4 key购买 nike

我将使用 Google Cloud Vision API。

在教程中,它说我需要将图像发送到他们的 Google Cloud Storage,然后通过使用该链接我需要向 API 发出请求。所以该方案如下所示:

手机照片(本地存储)--download--> GC Storage --get link--> 使用此链接发送请求到 GC Vision API --get JSON--> 使用 JSON

所以问题是。我为什么需要将图像存储在云端?只为了一个链接?我可以在不使用 GC 存储的情况下将图像直接发送到 Vision API 吗?所以方案:

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

最佳答案

是的,您可以直接将图像发送到视觉 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();
}
}

请检查我的存储库示例的链接以供引用: https://github.com/logicbeat/googlevisionapidemo

此外,您可以查看此链接: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/vision/label/src/main/java/com/google/cloud/vision/samples/label/LabelApp.java

关于java - 如何将图像发送到 GCP Vision API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46966332/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com