forked from eshetty/mlip-api-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.py
53 lines (40 loc) · 1.44 KB
/
analyze.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
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
import time
from dotenv import load_dotenv
import os
load_dotenv()
endpoint = os.getenv('LAB_end')
key = os.getenv('LAB_KEY')
credentials = CognitiveServicesCredentials(key)
client = ComputerVisionClient(
endpoint=endpoint,
credentials=credentials
)
def read_image(uri):
numberOfCharsInOperationId = 36
maxRetries = 10
# SDK call
rawHttpResponse = client.read(uri, language="en", raw=True)
# Get ID from returned headers
operationLocation = rawHttpResponse.headers["Operation-Location"]
idLocation = len(operationLocation) - numberOfCharsInOperationId
operationId = operationLocation[idLocation:]
# SDK call
result = client.get_read_result(operationId)
# Try API
retry = 0
while retry < maxRetries:
if result.status.lower () not in ['notstarted', 'running']:
break
time.sleep(1)
result = client.get_read_result(operationId)
retry += 1
if retry == maxRetries:
return "max retries reached"
if result.status == OperationStatusCodes.succeeded:
res_text = " ".join([line.text for line in result.analyze_result.read_results[0].lines])
return res_text
else:
return "error"