forked from divyambhagchandani/ScreenToText
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathd.py
More file actions
56 lines (48 loc) · 2.03 KB
/
d.py
File metadata and controls
56 lines (48 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import requests
# If you are using a Jupyter notebook, uncomment the following line.
#%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from PIL import Image
from io import BytesIO
# Replace <Subscription Key> with your valid subscription key.
subscription_key = "415f1df9cc3b4d3da8684c0d3d579974"
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://centralindia.api.cognitive.microsoft.com/vision/v2.0/"
ocr_url = vision_base_url + "ocr"
# Set image_url to the URL of an image that you want to analyze.
image_url = "https://i.stack.imgur.com/zfaZJ.png"
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
params = {'language': 'en'}
data = {'url': image_url}
response = requests.post(ocr_url, headers=headers, params=params, json=data)
response.raise_for_status()
analysis = response.json()
print(analysis)
# Extract the word bounding boxes and text.
line_infos = [region["lines"] for region in analysis["regions"]]
word_infos = []
for line in line_infos:
for word_metadata in line:
for word_info in word_metadata["words"]:
word_infos.append(word_info)
word_infos
# # Display the image and overlay it with the extracted text.
# plt.figure(figsize=(5, 5))
# image = Image.open(BytesIO(requests.get(image_url).content))
# ax = plt.imshow(image, alpha=0.5)
# for word in word_infos:
# bbox = [int(num) for num in word["boundingBox"].split(",")]
# text = word["text"]
# origin = (bbox[0], bbox[1])
# patch = Rectangle(origin, bbox[2], bbox[3], fill=False, linewidth=2, color='y')
# ax.axes.add_patch(patch)
# plt.text(origin[0], origin[1], text, fontsize=20, weight="bold", va="top")
# plt.axis("off")