Skip to content

Commit

Permalink
Computer Vision 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lmazuel committed Jun 22, 2018
1 parent 000a34d commit f3a71b8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ azure-cognitiveservices-search-newssearch
azure-cognitiveservices-search-videosearch
azure-cognitiveservices-search-visualsearch
azure-cognitiveservices-search-websearch
azure-cognitiveservices-vision-computervision
azure-cognitiveservices-vision-computervision>=0.2.0 # sample won't work with previous versions
azure-cognitiveservices-vision-contentmoderator
azure-cognitiveservices-vision-customvision
50 changes: 49 additions & 1 deletion samples/vision/computer_vision_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,62 @@ def image_analysis_in_stream(subscription_key):
]
)

print("This imsage can be described as: {}\n".format(image_analysis.description.captions[0].text))
print("This image can be described as: {}\n".format(image_analysis.description.captions[0].text))

print("Tags associated with this image:\nTag\t\tConfidence")
for tag in image_analysis.tags:
print("{}\t\t{}".format(tag.name, tag.confidence))

print("\nThe primary colors of this image are: {}".format(image_analysis.color.dominant_colors))

def recognize_text(subscription_key):
"""RecognizeTextUsingRecognizeAPI.
This will recognize text of the given image using the recognizeText API.
"""
import time
client = ComputerVisionAPI(COMPUTERVISION_LOCATION, CognitiveServicesCredentials(subscription_key))

with open(os.path.join(IMAGES_FOLDER, "make_things_happen.jpg"), "rb") as image_stream:
job = client.recognize_text_in_stream(
image_stream,
mode="Printed",
raw=True
)
operation_id = job.headers['Operation-Location'].split('/')[-1]

image_analysis = client.get_text_operation_result(operation_id)
while image_analysis.status in ['NotStarted', 'Running']:
time.sleep(1)
image_analysis = client.get_text_operation_result(operation_id)

print("Job completion is: {}\n".format(image_analysis.status))

print("Recognized:\n")
lines = image_analysis.recognition_result.lines
print(lines[0].words[0].text) # "make"
print(lines[1].words[0].text) # "things"
print(lines[2].words[0].text) # "happen"

def recognize_printed_text_in_stream(subscription_key):
"""RecognizedPrintedTextUsingOCR_API.
This will do an OCR analysis of the given image.
"""
import time
client = ComputerVisionAPI(COMPUTERVISION_LOCATION, CognitiveServicesCredentials(subscription_key))

with open(os.path.join(IMAGES_FOLDER, "computer_vision_ocr.png"), "rb") as image_stream:
image_analysis = client.recognize_printed_text_in_stream(
image_stream,
language="en"
)

lines = image_analysis.regions[0].lines
print("Recognized:\n")
for line in lines:
line_text = " ".join([word.text for word in line.words])
print(line_text)

if __name__ == "__main__":
import sys, os.path
Expand Down
Binary file added samples/vision/images/computer_vision_ocr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/vision/images/make_things_happen.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f3a71b8

Please sign in to comment.