简体   繁体   English

尝试使用Google Cloud Vision时出现INVALID_ARGUMENT

[英]I get INVALID_ARGUMENT when I try to use Google Cloud Vision

So I just want to detect text or labels from an image using the google cloud vision API. 因此,我只想使用google cloud vision API从图像中检测文本或标签。 But When I run this code I always get: com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request 但是,当我运行此代码时,我总是得到: com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request

But I don't know why...here is the full json output what I get: 但是我不知道为什么...这是我得到的完整的json输出:

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

My test code is here: 我的测试代码在这里:

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;
    }
}

So the question is.. what is wrong with this code? 所以问题是..这段代码有什么问题?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 Java API 创建 Google Cloud Function:“io.grpc.StatusRuntimeException: INVALID_ARGUMENT” - Creating Google Cloud Function via Java API: “io.grpc.StatusRuntimeException: INVALID_ARGUMENT” 尝试将 AutoML Vision 客户端与 Google Cloud Platform 一起使用时出现 DEADLINE_EXCEEDED - DEADLINE_EXCEEDED when try to use AutoML Vision client with Google Cloud Platform Google Dataflow / Dataprep Shuffle键太大(INVALID_ARGUMENT) - Google Dataflow / Dataprep Shuffle key too large (INVALID_ARGUMENT) 使用 IamCredentialsClient 对 Blob 进行签名时的 INVALID_ARGUMENT 响应 - INVALID_ARGUMENT response when signingBlob using IamCredentialsClient Google DocumentAI Java 示例因 io.grpc.StatusRuntimeException 失败:INVALID_ARGUMENT:请求包含无效参数 - Google DocumentAI Java example fails with io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Request contains an invalid argument Java-当我尝试使用盒子布局时出现错误 - Java - I get an error when i try to use a box layout Unity 广告返回 INVALID_ARGUMENT - Unity ads returns INVALID_ARGUMENT 当我尝试在Spring Boot和MVC中使用get映射时发生异常 - Exception when I try use get mapping in Spring Boot and MVC 在应用程序引擎内部使用时,Google Cloud Vision API会挂起 - Google Cloud Vision API hangs when used inside app engine 尝试按地址获取GPS坐标时,我应该在哪里添加Google API密钥? - Where should i add the Google API key when i try to get gps coordinates by address?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM