Skip to content

Instantly share code, notes, and snippets.

@heyAyushh
Created May 29, 2019 13:31
Show Gist options
  • Save heyAyushh/7d822e178a6e4aeb1646ba83d2ad9dd3 to your computer and use it in GitHub Desktop.
Save heyAyushh/7d822e178a6e4aeb1646ba83d2ad9dd3 to your computer and use it in GitHub Desktop.
from PIL import Image, ImageDraw, ImageFont #dynamic import
filename = '1'
gif='./data/'+filename+'.gif' #1
img = Image.open(gif)
img.save('./readydata/'+filename+".png",'png', optimize=True, quality=70) #2
# Create a file in Documents to test the upload and download.
import sys
from azure.storage.blob import BlockBlobService
# https://pushstore.blob.core.windows.net/ocr/
# enter your own blob storage keys
block_blob_service = BlockBlobService(account_name='pushstore', account_key='/utAfm2EQAU4Y1pl8LsytfPfN4co0Qqp/zFpEWpaWyZ45vRwjPNnPwHIXAnm2D5k0yTYGV671I8hgdhACJbEaA==')
# Upload the created file, use local_file_name for the blob name
block_blob_service.create_blob_from_path('ocr', filename+'.png', './readydata/'+filename+'.png')
""" block_blob_service.create_blob_from_path """
""" ('container_name', sys.argv[1], sys.argv[2]) """
print("hogya upload")
import requests
import time
# If you are using a Jupyter notebook, uncomment the following line.
#%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from PIL import Image
from io import BytesIO
import json
# Replace <Subscription Key> with your valid subscription key.
subscription_key = "4218d522b88f4f42bfa2f90143e58c3d"
assert subscription_key
# You must use the same region in your REST call as you used to get your
# subscription keys. For example, if you got your subscription keys from
# westus, replace "westcentralus" in the URI below with "westus".
#
# Free trial subscription keys are generated in the westcentralus region.
# If you use a free trial subscription key, you shouldn't need to change
# this region.
vision_base_url = "https://eastus.api.cognitive.microsoft.com/vision/v1.0/"
text_recognition_url = vision_base_url + "recognizeText"
# Set image_url to the URL of an image that you want to analyze.
image_url = "https://pushstore.blob.core.windows.net/ocr/"+filename+'.png'
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
# Note: The request parameter changed for APIv2.
# For APIv1, it is 'handwriting': 'true'.
params = {'mode': 'Handwritten'}
data = {'url': image_url}
response = requests.post(
text_recognition_url, headers=headers, params=params, json=data)
response.raise_for_status()
# Extracting handwritten text requires two API calls: One call to submit the
# image for processing, the other to retrieve the text found in the image.
# Holds the URI used to retrieve the recognized text.
operation_url = response.headers["Operation-Location"]
# The recognized text isn't immediately available, so poll to wait for completion.
analysis = {}
while "recognitionResult" not in analysis:
response_final = requests.get(
response.headers["Operation-Location"], headers=headers)
analysis = response_final.json()
time.sleep(1)
data = response_final.json()
# Format the data.json to a text readable file.
import json
with open('data.json', 'w') as outfile:
json.dump(data, outfile)
import os
from subprocess import Popen
devnull = open(os.devnull, 'wb')
Popen(['node', 'writeto.js','--file',filename], stdout=devnull, stderr=devnull)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment