Skip to content

Commit 090f4cf

Browse files
committed
Computer Vision
1 parent 4aa6617 commit 090f4cf

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ This project framework provides examples for the following services:
1313

1414
### Search
1515

16-
* Using the **Bing Custom Search SDK** [azure-cognititiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognititiveservices-search-customsearch) for the [Custom Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search//)
16+
* Using the **Bing Custom Search SDK** [azure-cognititiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognititiveservices-search-customsearch) for the [Custom Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/)
1717
* Using the **Bing Entity Search SDK** [azure-cognititiveservices-search-entitysearch](http://pypi.python.org/pypi/azure-cognititiveservices-search-entitysearch) for the [Entity Search API](https://azure.microsoft.com/services/cognitive-services/bing-entity-search-api/)
1818
* Using the **Bing Image Search SDK** [azure-cognititiveservices-search-imagesearch](http://pypi.python.org/pypi/azure-cognititiveservices-search-imagesearch) for the [Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-image-search-api/)
1919
* Using the **Bing News Search SDK** [azure-cognititiveservices-search-newssearch](http://pypi.python.org/pypi/azure-cognititiveservices-search-newssearch) for the [News Search API](https://azure.microsoft.com/services/cognitive-services/bing-news-search-api/)
2020
* Using the **Bing Video Search SDK** [azure-cognititiveservices-search-videosearch](http://pypi.python.org/pypi/azure-cognititiveservices-search-videosearch) for the [Video Search API](https://azure.microsoft.com/services/cognitive-services/bing-video-search-api/)
2121
* Using the **Bing Web Search SDK** [azure-cognititiveservices-search-websearch](http://pypi.python.org/pypi/azure-cognititiveservices-search-websearch) for the [Web Search API](https://azure.microsoft.com/services/cognitive-services/bing-web-search-api/)
2222

23+
### Vision
24+
25+
* Using the **Computer Vision SDK** [azure-cognititiveservices-vision-computervision](http://pypi.python.org/pypi/azure-cognititiveservices-vision-computervision) for the [Computer Vision API](https://azure.microsoft.com/services/cognitive-services/computer-vision/)
26+
2327
We provide several meta-packages to help you install several packages at a time. Please note that meta-packages are only recommended for development purpose. It's recommended in production to always pin specific version of individual packages.
2428

2529
## Getting Started
@@ -72,6 +76,7 @@ We provide several meta-packages to help you install several packages at a time.
7276
4. Set up the environment variable `NEWSSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute NewsSearch tests.
7377
4. Set up the environment variable `VIDEOSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute VideoSearch tests.
7478
4. Set up the environment variable `WEBSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute WebSearch tests.
79+
4. Set up the environment variable `COMPUTERVISION_SUBSCRIPTION_KEY` with your key if you want to execute SpellCheck tests. You might override too `COMPUTERVISION_LOCATION` (westcentralus by default).
7580
7681
## Demo
7782

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ azure-cognitiveservices-search-imagesearch
66
azure-cognitiveservices-search-newssearch
77
azure-cognitiveservices-search-videosearch
88
azure-cognitiveservices-search-websearch
9+
azure-cognitiveservices-vision-computervision

samples/vision/__init__.py

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import os.path
2+
3+
from azure.cognitiveservices.vision.computervision import ComputerVisionAPI
4+
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
5+
from msrest.authentication import CognitiveServicesCredentials
6+
7+
SUBSCRIPTION_KEY_ENV_NAME = "COMPUTERVISION_SUBSCRIPTION_KEY"
8+
COMPUTERVISION_LOCATION = os.environ.get("COMPUTERVISION_LOCATION", "westcentralus")
9+
10+
IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "images")
11+
12+
def image_analysis_in_stream(subscription_key):
13+
"""ImageAnalysisInStream.
14+
15+
This will analysis an image from a stream and return all available features.
16+
"""
17+
client = ComputerVisionAPI(COMPUTERVISION_LOCATION, CognitiveServicesCredentials(subscription_key))
18+
19+
with open(os.path.join(IMAGES_FOLDER, "house.jpg"), "rb") as image_stream:
20+
image_analysis = client.analyze_image_in_stream(
21+
image_stream,
22+
visual_features=[
23+
VisualFeatureTypes.image_type, # Could use simple str "ImageType"
24+
VisualFeatureTypes.faces, # Could use simple str "Faces"
25+
VisualFeatureTypes.categories, # Could use simple str "Categories"
26+
VisualFeatureTypes.color, # Could use simple str "Color"
27+
VisualFeatureTypes.tags, # Could use simple str "Tags"
28+
VisualFeatureTypes.description # Could use simple str "Description"
29+
]
30+
)
31+
32+
print("This imsage can be described as: {}\n".format(image_analysis.description.captions[0].text))
33+
34+
print("Tags associated with this image:\nTag\t\tConfidence")
35+
for tag in image_analysis.tags:
36+
print("{}\t\t{}".format(tag.name, tag.confidence))
37+
38+
print("\nThe primary colors of this image are: {}".format(image_analysis.color.dominant_colors))
39+
40+
41+
if __name__ == "__main__":
42+
import sys, os.path
43+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
44+
from tools import execute_samples
45+
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

samples/vision/images/house.jpg

92.9 KB
Loading

0 commit comments

Comments
 (0)