Skip to content

Extend the computer vision sample #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 51 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,58 +16,49 @@ These samples will show you how to get up and running using the Python SDKs for

This project framework provides examples for the following services:

### Knowledge
* Using the **QnA SDK** [azure-cognitiveservices-knowledge-qnamaker](http://pypi.python.org/pypi/azure-cognitiveservices-knowledge-qnamaker) for the [QnA API](https://azure.microsoft.com/en-us/services/cognitive-services/qna-maker/)


### Language

* Using the **LUIS SDK** [azure-cognitiveservices-language-luis](http://pypi.python.org/pypi/azure-cognitiveservices-language-luis) for the [LUIS API](https://azure.microsoft.com/services/cognitive-services/language-understanding-intelligent-service/)
* Using the **Bing Spell Check SDK** [azure-cognitiveservices-language-spellcheck](http://pypi.python.org/pypi/azure-cognitiveservices-language-spellcheck) for the [Bing Spell Check API](https://azure.microsoft.com/services/cognitive-services/spell-check/)
* Using the **Text Analytics SDK** [azure-cognitiveservices-language-textanalytics](http://pypi.python.org/pypi/azure-cognitiveservices-language-textanalytics) for the [Text Analytics API](https://azure.microsoft.com/services/cognitive-services/text-analytics/)

### Search

* Using the **Bing Autosuggest SDK** [azure-cognitiveservices-search-autosuggest](http://pypi.python.org/pypi/azure-cognitiveservices-search-autosuggest) for the [Autosuggest API](https://azure.microsoft.com/services/cognitive-services/autosuggest/)
* Using the **Bing Custom Search SDK** [azure-cognitiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-customsearch) for the [Custom Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/)
* Using the **Bing Custom Image Search SDK** [azure-cognitiveservices-search-customimagesearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-customimagesearch) for the [Custom Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/)
* Using the **Bing Entity Search SDK** [azure-cognitiveservices-search-entitysearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-entitysearch) for the [Entity Search API](https://azure.microsoft.com/services/cognitive-services/bing-entity-search-api/)
* Using the **Bing Image Search SDK** [azure-cognitiveservices-search-imagesearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-imagesearch) for the [Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-image-search-api/)
* Using the **Bing News Search SDK** [azure-cognitiveservices-search-newssearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-newssearch) for the [News Search API](https://azure.microsoft.com/services/cognitive-services/bing-news-search-api/)
* Using the **Bing Video Search SDK** [azure-cognitiveservices-search-videosearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-videosearch) for the [Video Search API](https://azure.microsoft.com/services/cognitive-services/bing-video-search-api/)
* Using the **Bing Visual Search SDK** [azure-cognitiveservices-search-visualsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-visualsearch) for the [Visual Search API](https://azure.microsoft.com/services/cognitive-services/bing-visual-search-api/)
* Using the **Bing Web Search SDK** [azure-cognitiveservices-search-websearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-websearch) for the [Web Search API](https://azure.microsoft.com/services/cognitive-services/bing-web-search-api/)

### Vision

* Using the **Face SDK** [azure-cognitiveservices-vision-face](http://pypi.python.org/pypi/azure-cognitiveservices-vision-face) for the [Face API](https://azure.microsoft.com/services/cognitive-services/face/)
* Using the **Computer Vision SDK** [azure-cognitiveservices-vision-computervision](http://pypi.python.org/pypi/azure-cognitiveservices-vision-computervision) for the [Computer Vision API](https://azure.microsoft.com/services/cognitive-services/computer-vision/)
* Using the **Content Moderator SDK** [azure-cognitiveservices-vision-contentmoderator](http://pypi.python.org/pypi/azure-cognitiveservices-vision-contentmoderator) for the [Content Moderator API](https://azure.microsoft.com/services/cognitive-services/content-moderator/)
* Using the **Custom Vision SDK** [azure-cognitiveservices-vision-customvision](http://pypi.python.org/pypi/azure-cognitiveservices-vision-customvision) for the [Custom Vision API](https://azure.microsoft.com/services/cognitive-services/custom-vision-service/)
* Using the **Ink Recognizer SDK** [azure-cognitiveservices-inkrecognizer](https://pypi.org/project/azure-cognitiveservices-inkrecognizer/) for the [Ink Recognizer API](https://azure.microsoft.com/services/cognitive-services/ink-recognizer/)
| Area | Service | SDK |
|------|---------|-----|
| Knowledge | [QnA API](https://azure.microsoft.com/en-us/services/cognitive-services/qna-maker/) | [azure-cognitiveservices-knowledge-qnamaker](http://pypi.python.org/pypi/azure-cognitiveservices-knowledge-qnamaker) |
| Language | [LUIS API](https://azure.microsoft.com/services/cognitive-services/language-understanding-intelligent-service/) | [azure-cognitiveservices-language-luis](http://pypi.python.org/pypi/azure-cognitiveservices-language-luis) |
| Language | [Bing Spell Check API](https://azure.microsoft.com/services/cognitive-services/spell-check/) | [azure-cognitiveservices-language-spellcheck](http://pypi.python.org/pypi/azure-cognitiveservices-language-spellcheck) |
| Language | [Text Analytics API](https://azure.microsoft.com/services/cognitive-services/text-analytics/) | [azure-cognitiveservices-language-textanalytics](http://pypi.python.org/pypi/azure-cognitiveservices-language-textanalytics) |
| Search | [Bing Autosuggest API](https://azure.microsoft.com/services/cognitive-services/autosuggest/) | [azure-cognitiveservices-search-autosuggest](http://pypi.python.org/pypi/azure-cognitiveservices-search-autosuggest) |
| Search | [Bing Custom Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/) | [azure-cognitiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-customsearch) |
| Search | [Bing Custom Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/) | [azure-cognitiveservices-search-customimagesearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-customimagesearch) |
| Search | [Bing Entity Search API](https://azure.microsoft.com/services/cognitive-services/bing-entity-search-api/) | [azure-cognitiveservices-search-entitysearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-entitysearch) |
| Search | [Bing Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-image-search-api/) | [azure-cognitiveservices-search-imagesearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-imagesearch) |
| Search | [Bing News Search API](https://azure.microsoft.com/services/cognitive-services/bing-news-search-api/) | [azure-cognitiveservices-search-newssearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-newssearch) |
| Search | [Bing Video Search API](https://azure.microsoft.com/services/cognitive-services/bing-video-search-api/) | [azure-cognitiveservices-search-videosearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-videosearch) |
| Search | [Bing Visual Search API](https://azure.microsoft.com/services/cognitive-services/bing-visual-search-api/) | [azure-cognitiveservices-search-visualsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-visualsearch) |
| Search | [Bing Web Search API](https://azure.microsoft.com/services/cognitive-services/bing-web-search-api/) | [azure-cognitiveservices-search-websearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-websearch) |
| Vision | [Face API](https://azure.microsoft.com/services/cognitive-services/face/) | [azure-cognitiveservices-vision-face](http://pypi.python.org/pypi/azure-cognitiveservices-vision-face) |
| Vision | [Computer Vision API](https://azure.microsoft.com/services/cognitive-services/computer-vision/) | [azure-cognitiveservices-vision-computervision](http://pypi.python.org/pypi/azure-cognitiveservices-vision-computervision) |
| Vision | [Content Moderator API](https://azure.microsoft.com/services/cognitive-services/content-moderator/) | [azure-cognitiveservices-vision-contentmoderator](http://pypi.python.org/pypi/azure-cognitiveservices-vision-contentmoderator) |
| Vision | [Custom Vision API](https://azure.microsoft.com/services/cognitive-services/custom-vision-service/) | [azure-cognitiveservices-vision-customvision](http://pypi.python.org/pypi/azure-cognitiveservices-vision-customvision) |
| Vision | [Ink Recognizer API](https://azure.microsoft.com/services/cognitive-services/ink-recognizer/) | [azure-cognitiveservices-inkrecognizer](https://pypi.org/project/azure-cognitiveservices-inkrecognizer/) |

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.

## Getting Started

### Prerequisites

1. A cognitive services API key with which to authenticate the SDK's calls. [Create a new Azure account, and try Cognitive Services for free.](https://azure.microsoft.com/free/cognitive-services/)
1. A cognitive services API key with which to authenticate the SDK's calls. [Create a new Azure account, and try Cognitive Services for free.](https://azure.microsoft.com/free/cognitive-services/)

> Subscription keys are usually per service. For example, the subscription key for Spell Check will not be the same than Custom Search. Read the previous *sign up* link or the Azure portal for details on subscription keys.

### Installation

1. If you don't already have it, [install Python](https://www.python.org/downloads/).
1. If you don't already have it, [install Python](https://www.python.org/downloads/).

This sample (and the SDK) is compatible with Python 2.7, 3.3, 3.4, 3.5 and 3.6.

2. General recommendation for Python development is to use a Virtual Environment.
For more information, see https://docs.python.org/3/tutorial/venv.html
2. General recommendation for Python development is to use a Virtual Environment.
For more information, see <https://docs.python.org/3/tutorial/venv.html>

Install and initialize the virtual environment with the "venv" module on Python 3 (you must install [virtualenv](https://pypi.python.org/pypi/virtualenv) for Python 2.7):

```
```bash
python -m venv mytestenv # Might be "python3" or "py -3.6" depending on your Python installation
cd mytestenv
source bin/activate # Linux shell (Bash, ZSH, etc.) only
Expand All @@ -77,36 +68,39 @@ We provide several meta-packages to help you install several packages at a time.

### Quickstart

1. Clone the repository.
1. Clone the repository.

```
```bash
git clone https://github.com/Azure-Samples/cognitive-services-python-sdk-samples.git
```

2. Install the dependencies using pip.
2. Install the dependencies using pip.

```
```bash
cd cognitive-services-python-sdk-samples
pip install -r requirements.txt
```

4. Set up the environment variable `LUIS_SUBSCRIPTION_KEY` with your key if you want to execute LUIS tests.
4. Set up the environment variable `SPELLCHECK_SUBSCRIPTION_KEY` with your key if you want to execute SpellCheck tests.
4. Set up the environment variable `TEXTANALYTICS_SUBSCRIPTION_KEY` with your key if you want to execute TextAnalytics tests. You might override too `TEXTANALYTICS_LOCATION` (westcentralus by default).
3. Set up the environment variable `AUTOSUGGEST_SUBSCRIPTION_KEY` with your key if you want to execute Autosuggest tests.
3. Set up the environment variable `CUSTOMSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomSearch tests.
3. Set up the environment variable `CUSTOMIMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomImageSearch tests.
3. Set up the environment variable `ENTITYSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute EntitySearch tests.
4. Set up the environment variable `IMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute ImageSearch tests.
4. Set up the environment variable `NEWSSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute NewsSearch tests.
4. Set up the environment variable `VIDEOSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute VideoSearch tests.
4. Set up the environment variable `VISUALSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute VideoSearch tests.
4. Set up the environment variable `WEBSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute WebSearch tests.
4. Set up the environment variable `COMPUTERVISION_SUBSCRIPTION_KEY` with your key if you want to execute Computer Vision tests. You might override too `COMPUTERVISION_LOCATION` (westcentralus by default).
4. Set up the environment variable `CONTENTMODERATOR_SUBSCRIPTION_KEY` with your key if you want to execute Content Moderator tests. You might override too `CONTENTMODERATOR_LOCATION` (westcentralus by default).
4. Set up the environment variable `CUSTOMVISION_TRAINING_KEY` with your key and `CUSTOMVISION_PREDICTION_ID` with a valid prediction resource id if you want to execute CustomVision Training tests.
4. Set up the environment variable `CUSTOMVISION_PREDICTION_KEY` with your key and `CUSTOMVISION_PREDICTION_ID` with a valid prediction resource id if you want to execute CustomVision Prediction tests.

3. Set up the environment variables with corresponding keys to execute samples of interest

| Environment variable | Service | Notes |
|----------------------|---------|-------|
| `LUIS_SUBSCRIPTION_KEY` | LUIS | |
| `SPELLCHECK_SUBSCRIPTION_KEY` | SpellCheck | |
| `TEXTANALYTICS_SUBSCRIPTION_KEY` | TextAnalytics | You might override too `TEXTANALYTICS_LOCATION` (westcentralus by default). |
| `AUTOSUGGEST_SUBSCRIPTION_KEY` | Autosuggest | |
| `CUSTOMSEARCH_SUBSCRIPTION_KEY` | CustomSearch | |
| `CUSTOMIMAGESEARCH_SUBSCRIPTION_KEY` | CustomImageSearch | |
| `ENTITYSEARCH_SUBSCRIPTION_KEY` | EntitySearch | |
| `IMAGESEARCH_SUBSCRIPTION_KEY` | ImageSearch | |
| `NEWSSEARCH_SUBSCRIPTION_KEY` | NewsSearch | |
| `VIDEOSEARCH_SUBSCRIPTION_KEY` | VideoSearch | |
| `VISUALSEARCH_SUBSCRIPTION_KEY` | VideoSearch | |
| `WEBSEARCH_SUBSCRIPTION_KEY` | WebSearch | |
| `COMPUTERVISION_SUBSCRIPTION_KEY` | Computer Vision | You might override too `COMPUTERVISION_LOCATION` (westcentralus by default). |
| `CONTENTMODERATOR_SUBSCRIPTION_KEY` | Content Moderator | You might override too `CONTENTMODERATOR_LOCATION` (westcentralus by default). |
| `CUSTOMVISION_TRAINING_KEY`, `CUSTOMVISION_TRAINING_ID` | CustomVision Training | |
| `CUSTOMVISION_PREDICTION_KEY`, `CUSTOMVISION_PREDICTION_ID` | CustomVision Prediction | |

## Demo

Expand All @@ -125,5 +119,5 @@ To see the code of each example, simply look at the examples in the Samples fold

## Resources

- https://docs.microsoft.com/python/api/overview/azure/cognitive-services
- https://github.com/Azure/azure-sdk-for-python
- <https://docs.microsoft.com/python/api/overview/azure/cognitive-services>
- <https://github.com/Azure/azure-sdk-for-python>
22 changes: 21 additions & 1 deletion samples/vision/computer_vision_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def image_analysis_in_stream(subscription_key):
VisualFeatureTypes.categories, # Could use simple str "Categories"
VisualFeatureTypes.color, # Could use simple str "Color"
VisualFeatureTypes.tags, # Could use simple str "Tags"
VisualFeatureTypes.description # Could use simple str "Description"
VisualFeatureTypes.description, # Could use simple str "Description"
VisualFeatureTypes.brands # Could use simple str "Brands"
]
)

Expand Down Expand Up @@ -102,6 +103,25 @@ def recognize_printed_text_in_stream(subscription_key):
line_text = " ".join([word.text for word in line.words])
print(line_text)

def recognize_brands_in_stream(subscription_key):
"""RecognizeBrandsInStream.

This will recognize brands of the given image using the recognizeBrands API.
"""
client = ComputerVisionClient(
endpoint="https://" + COMPUTERVISION_LOCATION + ".api.cognitive.microsoft.com/",
credentials=CognitiveServicesCredentials(subscription_key)
)

with open(os.path.join(IMAGES_FOLDER, "make_things_happen.jpg"), "rb") as image_stream:
image_analysis = client.recognize_brand_in_stream(
image=image_stream
)

print("Recognized:\n")
for brand in image_analysis.brands:
if brand.confidence >= 0.75:
print(f"\nLogo of {brand.name} between {brand.rectangle.x}, {brand.rectangle.y} and {brand.rectangle.w}, {brand.rectangle.h}")

if __name__ == "__main__":
import sys, os.path
Expand Down