Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import base64
- import requests
- import io
- # Read the image file and transform it into a base64 string
- with io.open("photos/foo.jpg", 'rb') as image_file:
- image = image_file.read()
- content = base64.b64encode(image)
- # Prepare the data for request
- # Format copied from https://cloud.google.com/vision/docs/ocr
- sending_request = {
- "requests": [
- {
- "image": {
- "content": content
- },
- "features": [
- {
- "type": "TEXT_DETECTION"
- }
- ]
- }
- ]
- }
- # Send the request and get the response
- # Format copied from https://cloud.google.com/vision/docs/using-python
- response = requests.post(
- url='https://vision.googleapis.com/v1/images:annotate?key={}'.format(API_KEY),
- data=sending_request,
- headers={'Content-Type': 'application/json'}
- )
- # Then get 400 code
- response
- # <Response [400]>
- print(response.text)
- {
- "error": {
- "code": 400,
- "message": "Invalid JSON payload received. Unexpected token.nrequests=image&requen^",
- "status": "INVALID_ARGUMENT"
- }
- }
- response = requests.post(
- url='https://vision.googleapis.com/v1/images:annotate?key={}'.format(API_KEY),
- # import json module
- # dumps the object to JSON
- data=json.dumps(sending_request),
- headers={'Content-Type': 'application/json'}
Add Comment
Please, Sign In to add comment