我正在测试Google Vision API的某些功能,并为我从相机(500万像素相机)点击的图像获取空响应。然而,当我从网上下载任何图像的例子时,一个传送人的图像(具有白色的普通背景)我得到了一个有意义的标签响应。这两组图像都存在于我的本地磁盘上。以下是我通过参考谷歌文档编写的代码,
public class ImageAnalyzer {
final static String APPLICATION_NAME ="My_APP/1.0";
final static String IMAGE_PATH = "E:/Vision/SampleImages/IMAG0013.jpg";
final static int maxResults =3;
private Vision vision;
public ImageAnalyzer(Vision vision){
this.vision=vision;
}
/**
* Connects to the Vision API using Application Default Credentials.
*/
public static Vision getVisionService() throws IOException, GeneralSecurityException {
GoogleCredential credential =
GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
public Map<String, List<String>> labelImage(String imagePath){
final Map<String,List<String>> labels = new HashMap<String, List<String>>();
final Path path = Paths.get(imagePath);
try {
final byte[] raw = Files.readAllBytes(path);
/*final AnnotateImageRequest request =new AnnotateImageRequest().setImage(new Image().encodeContent(raw))
.setFeatures(ImmutableList.of(new Feature().setType("LABEL_DETECTION").setMaxResults(3)
, new Feature().setType("LOGO_DETECTION").setMaxResults(3)));*/
AnnotateImageRequest request =
new AnnotateImageRequest()
.setImage(new Image().encodeContent(raw)).setFeatures(ImmutableList.of(
new Feature()
.setType("LABEL_DETECTION")
.setMaxResults(maxResults),
new Feature()
.setType("LOGO_DETECTION")
.setMaxResults(1),
new Feature()
.setType("TEXT_DETECTION")
.setMaxResults(maxResults),
new Feature()
.setType("LANDMARK_DETECTION")
.setMaxResults(1)));
final Vision.Images.Annotate annotate = vision.images().annotate(
new BatchAnnotateImagesRequest().
setRequests(ImmutableList.of(request)));
final BatchAnnotateImagesResponse batchResponse = annotate.execute();
//assert batchResponse.getResponses().size() == 1;
System.out.println("Size of searches"+batchResponse.getResponses().size());
//final AnnotateImageResponse response = batchResponse.getResponses().get(0);
if (batchResponse.getResponses().get(0).getLabelAnnotations() != null) {
final List<String> label = new ArrayList<String>();
for (EntityAnnotation ea: batchResponse.getResponses().get(0).getLabelAnnotations()) {
label.add(ea.getDescription());
}
labels.put("LABEL_ANNOTATION", label);
}
if (batchResponse.getResponses().get(0).getLandmarkAnnotations() != null) {
final List<String> landMark = new ArrayList<String>();
for (EntityAnnotation ea : batchResponse.getResponses().get(0).getLandmarkAnnotations()) {
landMark.add(ea.getDescription());
}
labels.put("LANDMARK_ANNOTATION", landMark);
}
if (batchResponse.getResponses().get(0).getLogoAnnotations() != null) {
final List<String> logo = new ArrayList<String>();
for (EntityAnnotation ea : batchResponse.getResponses().get(0).getLogoAnnotations()) {
logo.add(ea.getDescription());
}
labels.put("LOGO_ANNOTATION", logo);
}
if (batchResponse.getResponses().get(0).getTextAnnotations() != null) {
List<String> text = new ArrayList<String>();
for (EntityAnnotation ea : batchResponse.getResponses().get(0).getTextAnnotations()) {
text.add(ea.getDescription());
}
labels.put("TEXT_ANNOTATION", text);
}
return labels;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void printAnnotations(final List<EntityAnnotation> entityAnnotations) {
if(entityAnnotations!=null && !entityAnnotations.isEmpty()) {
for (final EntityAnnotation entityAnnotation : entityAnnotations) {
final String desc = entityAnnotation.getDescription();
final Float score = entityAnnotation.getScore();
System.out.println(desc+" "+score);
}
}
}
public static void main(String[] args) throws IOException, GeneralSecurityException {
// TODO Auto-generated method stub
final ImageAnalyzer analyzer = new ImageAnalyzer(getVisionService());
final Map<String, List<String>> labels = analyzer.labelImage(IMAGE_PATH);
for (Entry<String, List<String>> entry : labels.entrySet()) {
final String key = entry.getKey();
final List<String> value = entry.getValue();
if(value!=null && !value.isEmpty()) {
System.out.println("Printing for key"+key);
for (final String myLebel : value) {
System.out.println(" "+myLebel);
}
}
}
//System.out.println(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"))
}
}
任何人都可以帮助我吗?
答案 0 :(得分:1)
根据使用限制,图像必须<&lt; 4MB ...
https://cloud.google.com/vision/docs/image-best-practices#usage_limits