Skip to content

Instantly share code, notes, and snippets.

@thejungwon
Last active October 5, 2020 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejungwon/b46696c983ffa06c061499f58cefa4e7 to your computer and use it in GitHub Desktop.
Save thejungwon/b46696c983ffa06c061499f58cefa4e7 to your computer and use it in GitHub Desktop.
import requests
import uuid
import time
import base64
import json
api_url = '<YOUR_API_URL>'
secret_key = '<YOUR_SECERET_KEY>'
# image_url = 'YOUR_IMAGE_URL'
image_file = 'korean.jpg'
with open(image_file,'rb') as f:
file_data = f.read()
request_json = {
'images': [
{
'format': 'jpg',
'name': 'demo',
'data': base64.b64encode(file_data).decode(),
}
],
'requestId': str(uuid.uuid4()),
'version': 'V2',
'timestamp': int(round(time.time() * 1000))
}
payload = json.dumps(request_json).encode('UTF-8')
headers = {
'X-OCR-SECRET': secret_key,
'Content-Type': 'application/json'
}
response = requests.request("POST", api_url, headers=headers, data = payload)
result = json.loads(response.text)
extracted_text = ''
for box in result['images'][0]['fields']:
extracted_text+=box['inferText'] +" "
extracted_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment