예제 #1
0
END - Generate Thumbnail
'''

'''
Batch Read File, recognize handwritten text - local
This example extracts text from a handwritten local image, then prints results.
This API call can also recognize printed text (shown in next example, Batch Read File - remote).
'''
print("===== Batch Read File - local =====")
# Get image of handwriting
local_image_handwritten_path = "resources\\handwritten_text.jpg"
# Open the image
local_image_handwritten = open(local_image_handwritten_path, "rb")

# Call API with image and raw response (allows you to get the operation location)
recognize_handwriting_results = computervision_client.batch_read_file_in_stream(local_image_handwritten, raw=True)
# Get the operation location (URL with ID as last appendage)
operation_location_local = recognize_handwriting_results.headers["Operation-Location"]
# Take the ID off and use to get results
operation_id_local = operation_location_local.split("/")[-1]

# Call the "GET" API and wait for the retrieval of the results
while True:
    recognize_handwriting_result = computervision_client.get_read_operation_result(operation_id_local)
    if recognize_handwriting_result.status not in ['NotStarted', 'Running']:
        break
    time.sleep(1)

# Print results, line by line
if recognize_handwriting_result.status == TextOperationStatusCodes.succeeded:
    for text_result in recognize_handwriting_result.recognition_results:
# Recognize text with the Read API in a local image by:
#   1. Specifying whether the text to recognize is handwritten or printed.
#   2. Calling the Computer Vision service's batch_read_file_in_stream with the:
#      - context
#      - image
#      - text recognition mode
#   3. Extracting the Operation-Location URL value from the batch_read_file_in_stream
#      response
#   4. Waiting for the operation to complete.
#   5. Displaying the results.
local_image_path = "resources\\handwritten_text.jpg"
local_image = open(local_image_path, "rb")
text_recognition_mode = TextRecognitionMode.handwritten
num_chars_in_operation_id = 36

client_response = computervision_client.batch_read_file_in_stream(
    local_image, text_recognition_mode, raw=True)
operation_location = client_response.headers["Operation-Location"]
id_location = len(operation_location) - num_chars_in_operation_id
operation_id = operation_location[id_location:]

print("\n\nRecognizing text in a local image with the batch Read API ... \n")

while True:
    result = computervision_client.get_read_operation_result(operation_id)
    if result.status not in ['NotStarted', 'Running']:
        break
    time.sleep(1)

if result.status == TextOperationStatusCodes.succeeded:
    for text_result in result.recognition_results:
        for line in text_result.lines:
예제 #3
0
    print(caption.confidence)

#-----------------TEXT FROM IMAGE-------------#
print("#-----------------TEXT FROM IMAGE-------------#")
# import models
from azure.cognitiveservices.vision.computervision.models import TextOperationStatusCodes
import time
image = open(path, 'rb')

# url = "https://smhttp-ssl-39255.nexcesscdn.net/wp-content/uploads/2016/01/Handwritten-note-on-Presidential-stationery-900x616.jpg"
raw = True
custom_headers = None
numberOfCharsInOperationId = 36

# Async SDK call
rawHttpResponse = client.batch_read_file_in_stream(image, custom_headers, raw)

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

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

# Get data
if result.status == TextOperationStatusCodes.succeeded:
예제 #4
0
# See:
# https://stackoverflow.com/a/51726283
for i in range(len(cropped_images_list)):
    # Convert cropped images back to PIL.JpegImagePlugin.JpegImageFile type
    b = BytesIO()
    cropped_images_list[i].save(b, format="jpeg")
    cropped_images_list[i] = Image.open(b)
    b.close()
'''
Call the API
'''
# Use the Batch Read File API to extract text from the cropped image
for cropped_path in cropped_images_path:
    cropped_bytes = open(cropped_path, "rb")
    # Call API
    results = client.batch_read_file_in_stream(cropped_bytes, raw=True)
    # To get the read results, we need the operation ID from operation location
    operation_location = results.headers["Operation-Location"]
    # Operation ID is at the end of the URL
    operation_id = operation_location.split("/")[-1]

    # Wait for the "get_read_operation_result()" to retrieve the results
    while True:
        get_printed_text_results = client.get_read_operation_result(
            operation_id)
        if get_printed_text_results.status not in ['NotStarted', 'Running']:
            break
        time.sleep(1)
'''
Print results
'''
예제 #5
0
파일: ocr.py 프로젝트: isabella232/azcv
        sys.stderr.write("The --handwritten option is no longer required.\n")
else:
    mode = TextRecognitionMode.printed
raw = True
custom_headers = None
numberOfCharsInOperationId = 36

# Asynchronous call.

if ver(azver) > ver("0.3.0"):
    if is_url(url):
        rawHttpResponse = client.batch_read_file(url, custom_headers, raw)
    else:
        path = os.path.join(get_cmd_cwd(), url)
        with open(path, 'rb') as fstream:
            rawHttpResponse = client.batch_read_file_in_stream(
                fstream, custom_headers, raw)
else:
    if is_url(url):
        rawHttpResponse = client.batch_read_file(url, mode, custom_headers,
                                                 raw)
    else:
        path = os.path.join(get_cmd_cwd(), url)
        with open(path, 'rb') as fstream:
            rawHttpResponse = client.batch_read_file_in_stream(
                fstream, mode, custom_headers, raw)

# Get ID from returned headers.

operationLocation = rawHttpResponse.headers["Operation-Location"]
idLocation = len(operationLocation) - numberOfCharsInOperationId
operationId = operationLocation[idLocation:]
예제 #6
0
        image_name = os.fsdecode(image)

        for suffix in image_filetypes:
            if (image_name.endswith(suffix)):
                '''
                Extracts handwritten text in an image, then print results, line by line.
                '''
                print("===== Processed Image - '" + image_name + "' =====")
                image_output_writer.writerow(
                    ["===== Processed Image - '" + image_name + "' ====="])
                # Get an image with printed text
                local_image = open(directory_of_images_filepath + image_name,
                                   'rb')

                recognize_printed_results = computervision_client.batch_read_file_in_stream(
                    local_image, raw=True)

                # Get the operation location (URL with an ID at the end) from the response
                operation_location_remote = recognize_printed_results.headers[
                    "Operation-Location"]
                # Grab the ID from the URL
                operation_id = operation_location_remote.split("/")[-1]

                # Call the "GET" API and wait for it to retrieve the results
                while True:
                    get_printed_text_results = computervision_client.get_read_operation_result(
                        operation_id)
                    if get_printed_text_results.status not in [
                            'NotStarted', 'Running'
                    ]:
                        break
		for line in text_result.lines:
			print(line.text)
			print(line.bounding_box)
			print()
'''
# Recognize handwritten text - local
# This example extracts handwritten text from a local image file and displays the results
print("=== Detect handwritten text - local ===")

# Open the local file
local_image_path = "resources\\handwritten_text.jpg"

local_image = open(local_image_path, "rb")

# Call API with the image and raw response
result = computervision_client.batch_read_file_in_stream(local_image, raw=True)
# Get the operation location (URL + ID at end)
operation_location_local = result.headers["Operation-Location"]
# Slice the ID and use to retrieve results
operation_id_local = operation_location_local.split("/")[-1]

# Call the GET API and wait for result retrieval
while True:
    recognize_handwriting_result = computervision_client.get_read_operation_result(
        operation_id_local)
    if recognize_handwriting_result.status not in ["'NotStarted", 'Running']:
        break
    time.sleep()

# Print the results, line by line
if recognize_handwriting_result.status == TextOperationStatusCodes.succeeded: