我将使用Google Cloud Vision API。
在教程中,它说我需要将图像发送到他们的Google云端存储,然后使用该链接我需要向API发出请求。 所以这个计划看起来像这样:
手机照片(本地存储) - 下载 - > GC存储 - 目标链接 - > 通过此链接发送请求到GC Vision API --get JSON - >使用JSON
所以问题是。 在云中存储图像需要什么?仅限链接?我是否可以在没有GC存储的情况下将图像直接发送到Vision API? 所以方案:
手机照片(本地存储) - 下载 - >到GC Vision API --get JSON - >使用JSON
答案 0 :(得分:-1)
是的,您可以将图像直接发送到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();
}
}
请查看我的回购示例的链接以供参考: https://github.com/logicbeat/googlevisionapidemo