我一直在尝试调用 detectImageLabels 方法并将结果放入用户定义的数组 ObjectLabel 中,但我不断收到错误消息。我不知道该怎么做。labelImage 方法调用main 中的getFile 方法,获取文件的绝对路径,然后将路径输入到GoogleVision 中。应该从用户选择的图像文件中输出一组标签。例如,如果用户选择了一张鸟照片,它应该返回一个包含鸟类、羽毛、飞行、翅膀、自然等值的数组。也许我在 main.js 中错误地处理了抛出异常。
public static void labelImage(String filename) throws IOException, InterruptedException {
String userFile = filename;
JFrame img = new JFrame();
ImageIcon icon = new ImageIcon(userFile);
JLabel label = new JLabel(icon);
img.add(label);
img.pack();
img.setVisible(true);
ArrayList<ObjectLabel> labels = GoogleCloudVision.detectImageLabels(userFile);
System.out.println(labels);
如果我正确处理了异常,这就是我的 Main 。
String userFile = getFilename();
try {
labelImage(userFile);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
然后这是我的 ObjectLabel 方法
class ObjectLabel {
private String labels;
private double confidence;
public ObjectLabel(String labels, double confidence) {
this.labels = labels;
this.confidence = confidence;
}
public String getlabels() {
return labels;
}
public void setLabels(String labels) {
this.labels = labels;
}
public double getConfidence() {
return confidence;
}
public void setConfidence(double confidence) {
this.confidence = confidence;
}
@Override
public String toString() {
return "ObjectLabel{" +
"labels='" + labels + '\'' +
", confidence=" + confidence +
'}';
}
}
这是 GoogleCloudVision
ArrayList<ObjectLabel> results = new ArrayList<>();
// create the ImageAnnotatorClient to make the call to Google Cloud Vision
try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create(imageAnnotatorSettings)) {
// call the Google Cloud Vision service to detect the labels
BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(requests);
// process the results
for (AnnotateImageResponse res : response.getResponsesList()) {
// if an error occurred, print it and return an empty list
if (res.hasError()) {
System.err.println("Error: " + res.getError().getMessage());
return results;
} // end if error
// put the labels into our results ArrayList
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
results.add(new ObjectLabel(annotation.getDescription(),
annotation.getScore()));
} // end for each annotation
} // end for each response
} // end try with resources
return results;
} // end labelImage
然后这是我得到的错误
java.nio.file.NoSuchFileException: C:\Users\DELL Laptop\Downloads\Exam3Starter\src
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:533)
at java.base/java.nio.file.Files.newDirectoryStream(Files.java:544)
at GoogleCloudVision.findJsonCredentialsFile(GoogleCloudVision.java:51)
at GoogleCloudVision.detectImageLabels(GoogleCloudVision.java:80)
at SeeFood.labelImage(SeeFood.java:61)
at SeeFood.main(SeeFood.java:93)