예제 #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}
# 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:
#      - 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.
remote_image_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/printed_text.jpg"
text_recognition_mode = TextRecognitionMode.printed
num_chars_in_operation_id = 36

client_response = computervision_client.batch_read_file(remote_image_url,
                                                        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("\nRecognizing text in a remote 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:
예제 #3
0
# Read the text scanned in https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts-sdk/python-sdk#read-printed-and-handwritten-text
'''
Batch Read File, recognize printed text - remote
This example will extract printed text in an image, then print results, line by line.
This API call can also recognize handwriting (not shown).
'''
print("===== Batch Read File - remote =====")
# Get an image with printed text, there are a few examples on my github repo: https://github.com/tc-nyc/Cog-Services-Demo

#remote_image_printed_text_url = "https://raw.githubusercontent.com/tc-nyc/Cog-Services-Demo/master/Parts%20Needed%20-%20Day%202.jpg"

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)
img_url = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/printed_text.jpg'
endpoint = 'https://computer-vision-python.cognitiveservices.azure.com/vision/v2.0/recognizeText[?Printed]'

computervision_client = ComputerVisionClient(endpoint,
                                             CognitiveServicesCredentials(key))
'''
Batch Read File, recognize printed text - remote
This example will extract printed text in an image, then print results, line by line.
This API call can also recognize handwriting (not shown).
'''
print("===== Batch Read File - remote =====")
# Get an image with printed text
remote_image_printed_text_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/printed_text.jpg"

# Call API with URL and raw response (allows you to get the operation location)
recognize_printed_results = computervision_client.batch_read_file(img_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"]
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
예제 #5
0
# Choose between handwritten and printed. Default is printed. Use
# --handwritten to use the handwritten model.

if args.handwritten:
    mode = TextRecognitionMode.handwritten
else:
    mode = TextRecognitionMode.printed
raw = True
custom_headers = None
numberOfCharsInOperationId = 36

# Asynchronous call.

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:]

# Get the result.

while True: