提交 ab41433d authored 作者: Whispa's avatar Whispa

commit

上级 9187b918
package com.example.afrishop_v3.controllers;
import com.example.afrishop_v3.base.Result;
import com.example.afrishop_v3.enums.ResultCodeEnum;
import com.google.api.client.util.IOUtils;
......@@ -14,6 +15,7 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.io.InputStream;
......@@ -28,7 +30,7 @@ import java.util.stream.Collectors;
@RestController
@RequestMapping("search/image")
public class ImageSearchController extends Controller{
public class ImageSearchController extends Controller {
// Imports the Google Cloud client library
private final CloudVisionTemplate cloudVisionTemplate;
......@@ -39,6 +41,7 @@ public class ImageSearchController extends Controller{
public ImageSearchController(CloudVisionTemplate cloudVisionTemplate, ResourceLoader resourceLoader) {
this.cloudVisionTemplate = cloudVisionTemplate;
this.resourceLoader = resourceLoader;
// try {
// authExplicit();
// } catch (IOException e) {
......@@ -50,23 +53,45 @@ public class ImageSearchController extends Controller{
@GetMapping
public Result main() throws Exception {
//authExplicit();
AnnotateImageResponse response =
this.cloudVisionTemplate.analyzeImage(
this.resourceLoader.getResource("Blue_Tshirt.jpg"), Type.LABEL_DETECTION);
Map<String, Float> imageLabels =
response
.getLabelAnnotationsList()
.stream()
.collect(
Collectors.toMap(
EntityAnnotation::getDescription,
EntityAnnotation::getScore,
(u, v) -> {
throw new IllegalStateException(String.format("Duplicate key %s", u));
},
LinkedHashMap::new));
return new Result<>(imageLabels);
try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
// The path to the image file to annotate
String fileName = "./resources/wakeupcat.jpg";
// Reads the image file into memory
Path path = Paths.get(fileName);
RestTemplate restTemplate = new RestTemplate();
String url = "http://img.championat.com/news/big/l/c/ujejn-runi_1439911080563855663.jpg";
byte[] imageBytes = restTemplate.getForObject(url, byte[].class);
ByteString imgBytes = ByteString.copyFrom(imageBytes);
// Builds the image annotation request
List<AnnotateImageRequest> requests = new ArrayList<>();
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
// Performs label detection on the image file
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage());
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode());
}
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
annotation
.getAllFields()
.forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString()));
}
}
}
return new Result();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论