Guest User

Untitled

a guest
Apr 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import base64
  2. import requests
  3. import io
  4.  
  5. # Read the image file and transform it into a base64 string
  6. with io.open("photos/foo.jpg", 'rb') as image_file:
  7. image = image_file.read()
  8. content = base64.b64encode(image)
  9.  
  10. # Prepare the data for request
  11. # Format copied from https://cloud.google.com/vision/docs/ocr
  12. sending_request = {
  13. "requests": [
  14. {
  15. "image": {
  16. "content": content
  17. },
  18. "features": [
  19. {
  20. "type": "TEXT_DETECTION"
  21. }
  22. ]
  23. }
  24. ]
  25. }
  26.  
  27. # Send the request and get the response
  28. # Format copied from https://cloud.google.com/vision/docs/using-python
  29. response = requests.post(
  30. url='https://vision.googleapis.com/v1/images:annotate?key={}'.format(API_KEY),
  31. data=sending_request,
  32. headers={'Content-Type': 'application/json'}
  33. )
  34.  
  35. # Then get 400 code
  36. response
  37. # <Response [400]>
  38. print(response.text)
  39. {
  40. "error": {
  41. "code": 400,
  42. "message": "Invalid JSON payload received. Unexpected token.nrequests=image&requen^",
  43. "status": "INVALID_ARGUMENT"
  44. }
  45. }
  46.  
  47. response = requests.post(
  48. url='https://vision.googleapis.com/v1/images:annotate?key={}'.format(API_KEY),
  49. # import json module
  50. # dumps the object to JSON
  51. data=json.dumps(sending_request),
  52. headers={'Content-Type': 'application/json'}
Add Comment
Please, Sign In to add comment