Ejemplo n.º 1
0
def get_gas_price(url):
    subscription_key = AZURE_COMPUTERVISION_SUBSCRIPTION_KEY
    endpoint = "https://eastus.api.cognitive.microsoft.com/"
    computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))

    # Get an image with printed text
    remote_image_printed_text_url = url

    # Call API with URL and raw response (allows you to get the operation location)
    recognize_printed_results = computervision_client.batch_read_file(remote_image_printed_text_url, 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
        time.sleep(1)

    # Print the detected text, line by line
    numbers_list = []
    strings_list = []
    newdict = {}

    if get_printed_text_results.status == TextOperationStatusCodes.succeeded:
        for text_result in get_printed_text_results.recognition_results:
            for line in text_result.lines:
                new_line = line.text.replace(" ", "")
                if (is_float(new_line)):
                    numbers_list.append(float(new_line))
                elif (is_string(new_line)):
                    strings_list.append(new_line)
                    newdict[new_line] = find_area(line.bounding_box)

    numbers_list = [x for x in numbers_list if (x > 0 and x < 6) or (check_three_digit((x)))]
    for x in range(len(numbers_list)):
        while (numbers_list[x] >= 10.0):
            numbers_list[x] /= 10.0

    max = 0
    gas_type = ""
    for x in range(len(strings_list)):
        if (newdict[strings_list[x]] > max):
            max = newdict[strings_list[x]]
            gas_type = strings_list[x]

    price = numbers_list[0] if len(numbers_list) > 0 else 0

    return {gas_type: price}
#   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:
            print(line.text)
            print(line.bounding_box)
            print()
#   END - Recognizing printed and handwritten text with the batch read API in a local image

# Recognize text with the Read API in a remote 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:
Ejemplo n.º 3
0
remote_image_printed_text_url = "https://raw.githubusercontent.com/tc-nyc/Cog-Services-Demo/master/Target%20Data.PNG"

# Call API with URL and raw response (allows you to get the operation location)
recognize_printed_results = computervision_client.batch_read_file(
    remote_image_printed_text_url, 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
    time.sleep(1)

myData = []

125688

# Print the detected text, line by line
if get_printed_text_results.status == TextOperationStatusCodes.succeeded:
    for text_result in get_printed_text_results.recognition_results:
        for line in text_result.lines:
            print(line.text)
            myData.append(line.text)
            #finalDataset = pandas.DataFrame(line.text, columns = line.bounding_box)
Ejemplo n.º 4
0
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:
        for line in text_result.lines:
            print(line.text)
            print(line.bounding_box)
print()
'''
END - Batch Read File - local
'''
Ejemplo n.º 5
0
# 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:
    for textResult in result.recognition_results:
        for line in textResult.lines:
            print(line.text)
            print(line.bounding_box)
Ejemplo n.º 6
0
'''
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
'''
# Print the extracted text, line by line
if get_printed_text_results.status == OperationStatusCodes.succeeded:
    for text_result in get_printed_text_results.recognition_results:
        for line in text_result.lines:
            print("Extracted text:")
            print(line.text)
            print()
            print('Bounding box for each line:')
            print(line.bounding_box)