Google Cloud Vision getting Invalid value at 'requests[0].image.content'

十年热恋 提交于 2019-12-24 02:20:56

问题


I am getting this error:

Invalid value at 'requests[0].image.content' (TYPE_BYTES), "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/...

The image is encoded properly (it works using another visual search service that requires the same base64 encoding, metamind), what's wrong with this call?

This is from AngularJS $http service:

            $http({
                method: "POST",
                data: '{"requests":[{"image":{"content":"' + base64Img + '"},"features":[{"type":"LABEL_DETECTION","maxResults":1}]}]}',
                url: "https://vision.googleapis.com/v1/images:annotate?key=mykey27272772277227292992929"
            }).success(function (result) {
                console.log("SUCCESS");
                $scope.results = result;
                console.log(result);
            }).error(function (err) {
                console.log("FAIL");
                console.log(err);
            });

Any idea?


回答1:


I just realized they dont want the "data:image/jpeg;base64," part so a base64Img.slice(23) did the job

Update: Since slice cuts off at the character level .slice(23) works perfectly for .png & .jpg (and other types with 3 character extensions) but if a user uploads a type 'jpeg' the extra character ruins your data:

var headerless = "data:image/jpeg;base64" 
headerless.slice(23)

This would output: "data:image/jpeg;base6" and the remaining 4 would corrupt your data.

I'd recommend using .replace with a regex statement instead to cover this use case:

var image = <my image in b64 including header goes here>
var noHeader = image.replace(/^data:image\/(png|jpg|jpeg);base64,/, "")



回答2:


you could directly use jpg format with GCV. The following worked for me:

var source = 'http://www.abhishek.info/wp-content/uploads/2015/07/6-ID-Card-for-Downtown-Motors-Pvt-Limited.jpg';
  var apikey = 'yourkeyXXXXXXXXXXxxxxxx';
  $scope.param = '{"requests": [ { "image": { "source": { "imageUri": "' + source + '" } }, "features": [ { "type": "TEXT_DETECTION" } ] } ] }';
  $scope.url = 'https://vision.googleapis.com/v1/images:annotate?key=' + apikey;
  var url = $scope.url;
  var data = $scope.param;
  var config = {
    headers: {
      'Content-Type': 'application/json; charset=utf-8'
    }
  };
  $http.post(url, data,config).then(
    function(response) {
      console.log(response);
    },
    function(response) {
      console.log(response);
    });

  })


来源:https://stackoverflow.com/questions/35925120/google-cloud-vision-getting-invalid-value-at-requests0-image-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!