I am trying to test out google cloud vision api by following Google's tutorial on using cloud vision api.
Step 1: Generating JSON Requests by typing the following command in the terminal
$ python path/to/generate_json.py -i path/to/cloudVisionInputFile -o path/to/request.json
The above command generates request.json file.
Step 2: Using Curl to Send Generated Requests
$ curl -v -k -s -H "Content-Type: application/json" https://vision.googleapis.com/v1/images:annotate?key=AIzaSyD7Pm-ebpjas62ihvp9v1gAhTk --data-binary @/path/to/request.json > result.json
Output in Terminal (following step 2)
Notice that the output in the terminal (see below) shows Content-Length: 0
and [data not shown]
.
Can someone please advise why the content length is zero ? and also why I am unable to obtain the JSON response from google cloud vision api ?
The below is the out put in Terminal
* Hostname was NOT found in DNS cache
* Trying 216.58.347.74...
* Connected to vision.googleapis.com (216.58.347.74) port 443 (#0)
* successfully set certificate verify locations:
* CAfile: /opt/local/share/curl/curl-ca-bundle.crt
CApath: none
* SSLv3, TLS handshake, Client hello (1):
} [data not shown]
* SSLv3, TLS handshake, Server hello (2):
{ [data not shown]
* SSLv3, TLS handshake, CERT (11):
{ [data not shown]
* SSLv3, TLS handshake, Server key exchange (12):
{ [data not shown]
* SSLv3, TLS handshake, Server finished (14):
{ [data not shown]
* SSLv3, TLS handshake, Client key exchange (16):
} [data not shown]
* SSLv3, TLS change cipher, Client hello (1):
} [data not shown]
* SSLv3, TLS handshake, Finished (20):
} [data not shown]
* SSLv3, TLS change cipher, Client hello (1):
{ [data not shown]
* SSLv3, TLS handshake, Finished (20):
{ [data not shown]
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
* subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.googleapis.com
* start date: 2016-10-06 12:44:36 GMT
* expire date: 2016-12-29 12:28:00 GMT
* issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
* SSL certificate verify ok.
> POST /v1/images:annotate?key=AIzaSyD7Pm-ebpjas62ihvp9v1gAhTk HTTP/1.1
> User-Agent: curl/7.37.1
> Host: vision.googleapis.com
> Accept: */*
> Content-Type: application/json
> Content-Length: 0
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Vary: X-Origin
< Vary: Referer
< Date: Mon, 17 Oct 2016 13:02:56 GMT
* Server ESF is not blacklisted
< Server: ESF
< Cache-Control: private
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< X-Content-Type-Options: nosniff
< Alt-Svc: quic=":443"; ma=2592000; v="36,35,34,33,32"
< Accept-Ranges: none
< Vary: Origin,Accept-Encoding
< Transfer-Encoding: chunked
<
{ [data not shown]
* Connection #0 to host vision.googleapis.com left intact
Below is the JSON request generated in request.json file
{
"requests": [{
"image": {
"content": "/9j/4AAQSkZJRgABAQAA..."
},
"features": [{
"type": "TYPE_UNSPECIFIED",
"maxResults": 10
}, {
"type": "FACE_DETECTION",
"maxResults": 10
}, {
"type": "LANDMARK_DETECTION",
"maxResults": 10
}, {
"type": "LOGO_DETECTION",
"maxResults": 10
}, {
"type": "LABEL_DETECTION",
"maxResults": 10
}, {
"type": "TEXT_DETECTION",
"maxResults": 10
}, {
"type": "SAFE_SEARCH_DETECTION",
"maxResults": 10
}]
}, {
"image": {
"content": "/9j/4AAQSkZJRgABAQAAAQABAAD..."
},
"features": [{
"type": "TYPE_UNSPECIFIED",
"maxResults": 10
}, {
"type": "FACE_DETECTION",
"maxResults": 10
}, {
"type": "LANDMARK_DETECTION",
"maxResults": 10
}, {
"type": "LOGO_DETECTION",
"maxResults": 10
}, {
"type": "LABEL_DETECTION",
"maxResults": 10
}, {
"type": "TEXT_DETECTION",
"maxResults": 10
}, {
"type": "SAFE_SEARCH_DETECTION",
"maxResults": 10
}]
}]
}
Below is the Code in generate_json.py
import argparse
import base64
import json
import sys
def main(cloudVisionInputFile, request):
"""Translates the input file into a json output file.
Args:
input_file: a file object, containing lines of input to convert.
output_filename: the name of the file to output the json to.
"""
# Collect all requests into an array - one per line in the input file
request_list = []
for line in input_file:
# The first value of a line is the image. The rest are features.
image_filename, features = line.lstrip().split(' ', 1)
# First, get the image data
with open(image_filename, 'rb') as image_file:
content_json_obj = {
'content': base64.b64encode(image_file.read()).decode('UTF-8')
}
# Then parse out all the features we want to compute on this image
feature_json_obj = []
for word in features.split(' '):
feature, max_results = word.split(':', 1)
feature_json_obj.append({
'type': get_detection_type(feature),
'maxResults': int(max_results),
})
# Now add it to the request
request_list.append({
'features': feature_json_obj,
'image': content_json_obj,
})
# Write the object to a file, as json
# with open(output_filename, 'w') as output_file:
with open(request, 'w') as output_file:
json.dump({'requests': request_list}, output_file)
DETECTION_TYPES = [
'TYPE_UNSPECIFIED',
'FACE_DETECTION',
'LANDMARK_DETECTION',
'LOGO_DETECTION',
'LABEL_DETECTION',
'TEXT_DETECTION',
'SAFE_SEARCH_DETECTION',
]
def get_detection_type(detect_num):
"""Return the Vision API symbol corresponding to the given number."""
detect_num = int(detect_num)
if 0 < detect_num < len(DETECTION_TYPES):
return DETECTION_TYPES[detect_num]
else:
return DETECTION_TYPES[0]
# [END generate_json]
FILE_FORMAT_DESCRIPTION = '''
Each line in the input file must be of the form:
file_path feature:max_results feature:max_results ....
where 'file_path' is the path to the image file you'd like
to annotate, 'feature' is a number from 1 to %s,
corresponding to the feature to detect, and max_results is a
number specifying the maximum number of those features to
detect.
The valid values - and their corresponding meanings - for
'feature' are:
%s
'''.strip() % (
len(DETECTION_TYPES) - 1,
# The numbered list of detection types
'\n '.join(
# Don't present the 0th detection type ('UNSPECIFIED') as an option.
'%s: %s' % (i + 1, detection_type)
for i, detection_type in enumerate(DETECTION_TYPES[1:])))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument(
'-i', dest='input_file', required=True,
help='The input file to convert to json.\n' + FILE_FORMAT_DESCRIPTION)
parser.add_argument(
'-o', dest='output_file', required=True,
help='The name of the json file to output to.')
args = parser.parse_args()
try:
with open(args.input_file, 'r') as input_file:
main(input_file, args.output_file)
except ValueError as e:
sys.exit('Invalid input file format.\n' + FILE_FORMAT_DESCRIPTION)
The below is the text inside cloudVisionInputFile
/Users/pravishanthmadepally/documents/machineLearning/googleCloudVisionAPI/images/img1.jpeg 0:10 1:10 2:10 3:10 4:10 5:10 6:10
/Users/pravishanthmadepally/documents/machineLearning/googleCloudVisionAPI/images/img2.jpeg 0:10 1:10 2:10 3:10 4:10 5:10 6:10