-
Notifications
You must be signed in to change notification settings - Fork 0
/
Request.py
80 lines (48 loc) · 1.8 KB
/
Request.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import json
import requests
import time
from PIL import Image, ImageDraw
subscription_key = "20d4fbf92f884f349db21874dd04186a"
endpoint = "https://first-vision.cognitiveservices.azure.com/"
text_recognition_url = endpoint + "/vision/v3.1/read/analyze"
image_url = "https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/master/articles/cognitive-services/Computer-vision/Images/readsample.jpg"
image= 'C:\\Users\\20812018100700\\Downloads\\readsample.jpg'
#headers = {'Ocp-Apim-Subscription-Key': subscription_key}
headers = {'Ocp-Apim-Subscription-Key': subscription_key,
'Content-Type': 'application/octet-stream'}
with open(image, 'rb') as f:
data = f.read()
#data = {'url': image_url}
response = requests.post(text_recognition_url, headers=headers, data=data)
response.raise_for_status()
operation_url = response.headers["Operation-Location"]
while True:
response_final = requests.get(
response.headers["Operation-Location"], headers=headers)
analysis = response_final.json()
# final = json.dumps(analysis, indent=4)
# print(final)
time.sleep(1)
if ("analyzeResult" in analysis):
break
if ("status" in analysis and analysis['status'] == 'failed'):
break
final_text = [(line["boundingBox"], line["text"])
for line in analysis["analyzeResult"]["readResults"][0]["lines"]]
print(final_text)
"""
draw in image
img = Image.open(image).convert('RGBA')
txt = Image.new('RGBA', img.size, (255,255,255,0))
d = ImageDraw.Draw(txt)
d.rectangle(
(38, 650, 2572, 815),
fill=(255, 0, 0),
outline=(0, 0, 0))
d.rectangle(
(184, 1053, 580, 1128),
fill=(255, 255, 0),
outline=(0, 0, 0))
out = Image.alpha_composite(img, txt)
out.show()
"""