-
-
Save viticci/46f15aff47cdd2b5204a55d38c4ea985 to your computer and use it in GitHub Desktop.
OCR Canvas.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httplib, urllib, base64, clipboard, json, requests, datetime, appex, photos, console, dialogs | |
timestamp = datetime.datetime.now() | |
name = timestamp.strftime("%Y-%m-%d-%H%M%S") + '.jpeg' | |
#Kraken API | |
apiKey = 'YOUR API KEY' | |
apiSecret = 'YOUR API SECRET' | |
params = { | |
'auth': { | |
'api_key': apiKey, | |
'api_secret': apiSecret | |
}, | |
'wait': True, | |
"convert": { | |
"format": "jpeg" | |
} | |
} | |
data = json.dumps(params) | |
if appex.is_running_extension() is True: | |
image = appex.get_image_data() | |
else: | |
image = photos.pick_image(original=True, raw_data=True) | |
print 'Uploading to Kraken...' | |
console.show_activity() | |
request = requests.post( | |
url = 'http://api.kraken.io/v1/upload', | |
files = { 'name': (name, image)}, | |
data = { 'data': data } | |
) | |
response = json.loads(str(request.content)) | |
if (response['success']): | |
console.hud_alert('Lossless image uploaded to Kraken.', 'success') | |
final = response['kraked_url'] | |
else: | |
print 'Fail. Error message: %s ' % response['error'] | |
urlOcr = '/vision/v1/ocr' | |
headers = { | |
# Request headers | |
'User-Agent': 'python', | |
'Ocp-Apim-Subscription-Key': 'OXFORD SUB KEY', | |
} | |
params2 = urllib.urlencode({ | |
# Request parameters | |
'language': 'en', | |
'detectOrientation': 'true', | |
}) | |
body = { | |
"Url": final, | |
} | |
print 'Performing OCR...' | |
body = json.dumps(body) | |
conn = httplib.HTTPSConnection('api.projectoxford.ai') | |
conn.request("POST", urlOcr, body, headers) | |
response = conn.getresponse() | |
back = response.read() | |
conn.close() | |
print 'OCR successfully performed.' | |
data = json.loads(back) | |
s = '' | |
for item in data["regions"]: | |
for line in item["lines"]: | |
for word in line["words"]: | |
s += ' ' + word["text"] | |
print s | |
dialogs.share_text(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment