-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathocr-demo.py
55 lines (46 loc) · 2.01 KB
/
ocr-demo.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
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
def main():
try:
# Get Configuration Settings
load_dotenv()
endpoint = os.getenv('COG_SERVICE_ENDPOINT')
key = os.getenv('COG_SERVICE_KEY')
# Authenticate Computer Vision client
computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(key))
# Extract test
images_folder = os.path.join (os.path.dirname(os.path.abspath(__file__)), "images")
read_image_path = os.path.join (images_folder, "notes1.jpg")
get_text(read_image_path,computervision_client)
print('\n')
read_image_path = os.path.join (images_folder, "notes2.jpg")
get_text(read_image_path,computervision_client)
except Exception as ex:
print(ex)
def get_text(image_file,computervision_client):
# Open local image file
with open(image_file, "rb") as image:
# Call the API
read_response = computervision_client.read_in_stream(image, raw=True)
# Get the operation location (URL with an ID at the end)
read_operation_location = read_response.headers["Operation-Location"]
# Grab the ID from the URL
operation_id = read_operation_location.split("/")[-1]
# Retrieve the results
while True:
read_result = computervision_client.get_read_result(operation_id)
if read_result.status.lower() not in ['notstarted', 'running']:
break
time.sleep(1)
# Get the detected text
if read_result.status == OperationStatusCodes.succeeded:
for page in read_result.analyze_result.read_results:
for line in page.lines:
# Print line
print(line.text)
if __name__ == "__main__":
main()