gpt4 book ai didi

java - 在 Eclipse 中使用 Java 测试 Google Cloud Vision

转载 作者:行者123 更新时间:2023-12-02 02:19:42 24 4
gpt4 key购买 nike

我正在 Eclipse 中使用 Java 测试 Google Cloud Vision。

我已经从https://cloud.google.com/vision/docs/reference/libraries#client-libraries-install-java复制了java代码

// Imports the Google Cloud client library

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.Feature.Type;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.protobuf.ByteString;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class QuickstartSample {
public static void main(String... args) throws Exception {
// Instantiates a client
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);
byte[] data = Files.readAllBytes(path);
ByteString imgBytes = ByteString.copyFrom(data);

// 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.printf("Error: %s\n", res.getError().getMessage());
return;
}

for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
annotation.getAllFields().forEach((k, v) ->
System.out.printf("%s : %s\n", k, v.toString()));
}
}
}
}
}

我可以从哪里下载 .JAR 以便一切都能正确编译?

Google 自己说(请参阅上面的链接)

If you are using Maven, add this to your pom.xml file:

    <groupId>com.google.cloud</groupId>  
<artifactId>google-cloud-vision</artifactId>
<version>1.14.0</version> </dependency>

但是,我没有使用 Maven(也没有使用 Gradle 或 SBT,这是他们的其他建议)。

所以我想在 Eclipse 中打开一个新的 Maven 项目,然后它会自动下载所有 JAR,然后将它们复制到我的项目中。

所以我在 Eclipse 中执行了“new Maven Project”,然后当它说“输入 Artifact 的组 ID”时,我输入了上面粘贴的来自 Google 的详细信息,但它没有下载任何内容。

有什么想法可以获取 JAR 以便代码可以编译吗?

Eclipse自动生成的pom.xml文件是

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.14.0</version>
<packaging>jar</packaging>

<name>google-cloud-vision</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

最佳答案

现在,当您拥有 Maven 时,您可以添加依赖项。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- THIS IS YOUR APP RELATED STUFF -->
<groupId>com.your.app.group.id</groupId> <!-- YOU SHOULD PROVIDE THIS -->
<artifactId>your-app-name</artifactId> <!-- YOU SHOULD PROVIDE THIS -->
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- THIS ARE YOUR APP dependencies -->
<!-- Google lib should be declared here -->
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.14.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

如果此依赖项位于 Maven Central - Maven 将下载它。

关于java - 在 Eclipse 中使用 Java 测试 Google Cloud Vision,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48690047/

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