当我尝试使用Google Cloud Vision时,我收到了INVALID_ARGUMENT

时间:2016-09-09 08:37:00

标签: java ocr google-cloud-vision

所以我只想使用谷歌云视觉API从图像中检测文本或标签。但是,当我运行此代码时,我总是得到:com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request

但我不知道为什么......这是我得到的完整的json输出:

{
  "code": 400,
  "errors": [
    {
      "domain": "global",
      "message": "Request Admission Denied.",
      "reason": "badRequest"
    }
  ],
  "message": "Request Admission Denied.",
  "status": "INVALID_ARGUMENT"
}

我的测试代码在这里:

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.vision.v1.Vision;
import com.google.api.services.vision.v1.VisionRequestInitializer;
import com.google.api.services.vision.v1.model.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class GoogleDetection extends Detector {

    private static final String CLOUD_VISION_API_KEY = "MY_API_KEY";

    @Override
    String detect(String filePath) {
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = GsonFactory.getDefaultInstance();

        Vision.Builder builder = new Vision.Builder(httpTransport, jsonFactory, null);
        builder.setVisionRequestInitializer(new
                VisionRequestInitializer(CLOUD_VISION_API_KEY));
        Vision vision = builder.build();

        BatchAnnotateImagesRequest batchAnnotateImagesRequest =
                new BatchAnnotateImagesRequest();
        batchAnnotateImagesRequest.setRequests(new ArrayList<AnnotateImageRequest>() {{
            AnnotateImageRequest annotateImageRequest = new AnnotateImageRequest();

            // Image to bytes
            Image base64EncodedImage = new Image();
            BufferedImage bufferedImage = null;
            File imgPath = new File(filePath);
            try {
                bufferedImage = ImageIO.read(imgPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            WritableRaster raster = bufferedImage.getRaster();
            DataBufferByte data = (DataBufferByte) raster.getDataBuffer();

            byte[] imageBytes = data.getData();

            // Base64 encode the JPEG
            base64EncodedImage.encodeContent(imageBytes);
            annotateImageRequest.setImage(base64EncodedImage);


            // add the features we want
            annotateImageRequest.setFeatures(new ArrayList<Feature>() {{
                Feature labelDetection = new Feature();
                labelDetection.setType("LABEL_DETECTION");
                labelDetection.setMaxResults(10);
                add(labelDetection);
            }});

            // Add the list of one thing to the request
            add(annotateImageRequest);
        }});

        Vision.Images.Annotate annotateRequest;
        BatchAnnotateImagesResponse response = null;
        try {
            annotateRequest = vision.images().annotate(batchAnnotateImagesRequest);
            // Due to a bug: requests to Vision API containing large images fail when GZipped.
            annotateRequest.setDisableGZipContent(true);

            response = annotateRequest.execute();
            System.out.println(response.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return convertResponseToString(response);
    }

    private String convertResponseToString(BatchAnnotateImagesResponse response) {
        String message = "I found these things:\n\n";

        List<EntityAnnotation> labels = response.getResponses().get(0).getLabelAnnotations();
        if (labels != null) {
            for (EntityAnnotation label : labels) {
                message += String.format("%.3f: %s", label.getScore(), label.getDescription());
                message += "\n";
            }
        } else {
            message += "nothing";
        }

        return message;
    }
}

所以问题是......这段代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

检查此答案:https://stackoverflow.com/a/38131991/2734665 图像最大文件大小为4MB,最大请求大小为8MB