Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions samples/vision/computer_vision_extract_text.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.vision.computervision.models import TextRecognitionMode
from azure.cognitiveservices.vision.computervision.models import TextOperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import ReadOperationResult, OperationStatusCodes
import time

'''
Expand All @@ -27,30 +26,39 @@
# Create client
client = ComputerVisionClient(endpoint, credentials)

url = "https://azurecomcdn.azureedge.net/cvt-1979217d3d0d31c5c87cbd991bccfee2d184b55eeb4081200012bdaf6a65601a/images/shared/cognitive-services-demos/read-text/read-1-thumbnail.png"
mode = TextRecognitionMode.handwritten
# change this URL to reflect the image that you would like to test.
url = "https://azurecomcdn.azureedge.net/cvt-181c82bceabc9fab9ec6f3dca486738800e04b45a0b3c1268609c94f4d67173a/images/shared/cognitive-services-demos/analyze-image/analyze-6-thumbnail.jpg"
# image_path = "images/computer_vision_ocr.png"
lang = 'en'
raw = True
custom_headers = None
numberOfCharsInOperationId = 36

# Async SDK call
rawHttpResponse = client.batch_read_file(url, mode, custom_headers, raw)
# Read an image from a url
rawHttpResponse = client.read(url, language=lang, custom_headers=custom_headers, raw=raw)

# Uncomment the following code and comment out line 37 to read from image stream
# with open(image_path, "rb") as image_stream:
# rawHttpResponse = client.read_in_stream(
# image=image_stream, language=lang,
# # Raw will return the raw response which can be used to find the operation_id
# raw=True
# )

# Get ID from returned headers
operationLocation = rawHttpResponse.headers["Operation-Location"]
idLocation = len(operationLocation) - numberOfCharsInOperationId
operationId = operationLocation[idLocation:]
operationId = operationLocation.split('/')[-1]

# SDK call
while True:
result = client.get_read_operation_result(operationId)
if result.status not in ['NotStarted', 'Running']:
result = client.get_read_result(operationId)
if result.status not in [OperationStatusCodes.not_started, OperationStatusCodes.running]:
break
time.sleep(1)

# Get data: displays text captured and its bounding box (position in the image)
if result.status == TextOperationStatusCodes.succeeded:
for textResult in result.recognition_results:
for line in textResult.lines:

if result.status == OperationStatusCodes.succeeded:
for read_result in result.analyze_result.read_results:
for line in read_result.lines:
print(line.text)
print(line.bounding_box)