Skip to content

Commit b6ec2db

Browse files
authored
Merge branch 'master' into patch-3
2 parents 6208d24 + 4b5c95a commit b6ec2db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1833
-647
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,5 @@ ENV/
100100
# mypy
101101
.mypy_cache/
102102

103-
# idea files
103+
# idea
104104
.idea/
105-
106-
#DS store
107-
.DS_Store

README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
---
2-
services: cognitive-services
3-
platforms: python
4-
author: lmazuel
2+
page_type: sample
3+
languages:
4+
- python
5+
products:
6+
- azure
7+
description: "These samples will show you how to get up and running using the Python SDKs for various Cognitive Services services."
8+
urlFragment: cognitive-services-python-sdk-samples
59
---
610

711
# Cognitive Services Python SDK Samples
@@ -15,11 +19,12 @@ This project framework provides examples for the following services:
1519
### Knowledge
1620
* 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/)
1721

22+
1823
### Language
1924

2025
* 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/)
2126
* 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/)
22-
* Using the **Text Analytics SDK** [azure-cognitiveservices-language-textanalytics](http://pypi.python.org/pypi/azure-cognitiveservices-language-textanalytics) for the [Tet Analytics API](https://azure.microsoft.com/services/cognitive-services/text-analytics/)
27+
* 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/)
2328

2429
### Search
2530

@@ -39,6 +44,7 @@ This project framework provides examples for the following services:
3944
* 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/)
4045
* 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/)
4146
* 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/)
47+
* 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/)
4248

4349
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.
4450

@@ -98,8 +104,9 @@ We provide several meta-packages to help you install several packages at a time.
98104
4. Set up the environment variable `WEBSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute WebSearch tests.
99105
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).
100106
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).
101-
4. Set up the environment variable `CUSTOMVISION_TRAINING_KEY` with your key if you want to execute CustomVision Training tests.
102-
4. Set up the environment variable `CUSTOMVISION_PREDICTION_KEY` with your key if you want to execute CustomVision Prediction tests.
107+
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.
108+
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.
109+
103110
104111
## Demo
105112
@@ -109,9 +116,10 @@ To run the complete demo, execute `python example.py`
109116
110117
To run each individual demo, point directly to the file. For example (i.e. not complete list):
111118
112-
2. `python samples/language/spellcheck_samples.py`
113-
1. `python samples/search/entity_search_samples.py`
114-
2. `python samples/search/video_search_samples.py`
119+
1. `python samples/language/spellcheck_samples.py`
120+
2. `python samples/search/entity_search_samples.py`
121+
3. `python samples/search/video_search_samples.py`
122+
4. `python samples/vision/inkrecognizer_sample.py`
115123
116124
To see the code of each example, simply look at the examples in the Samples folder. They are written to be isolated in scope so that you can see only what you're interested in.
117125

example.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,27 @@
1212

1313
import samples.tools
1414

15+
1516
def run_all_samples():
1617
for _, section_name_name, ispkg in pkgutil.walk_packages(samples.__path__):
1718
if not ispkg:
1819
continue
1920
section_package_name = "samples."+section_name_name
2021
section_package = importlib.import_module(section_package_name)
2122
for _, sample_name, _ in pkgutil.iter_modules(section_package.__path__):
22-
sample_module = importlib.import_module(section_package_name+"."+sample_name)
23-
subkey_env_name = getattr(sample_module, "SUBSCRIPTION_KEY_ENV_NAME", None)
23+
sample_module = importlib.import_module(
24+
section_package_name+"."+sample_name)
25+
subkey_env_name = getattr(
26+
sample_module, "SUBSCRIPTION_KEY_ENV_NAME", None)
2427
if not subkey_env_name:
2528
continue
2629
print("Executing sample from ", sample_name)
2730
try:
28-
samples.tools.execute_samples(sample_module.__dict__, subkey_env_name)
31+
samples.tools.execute_samples(
32+
sample_module.__dict__, subkey_env_name)
2933
except samples.tools.SubscriptionKeyError as err:
3034
print("{}\n".format(err))
3135

36+
3237
if __name__ == "__main__":
3338
run_all_samples()

requirements.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
azure-cognitiveservices-knowledge-qnamaker
22
azure-cognitiveservices-language-luis
33
azure-cognitiveservices-language-spellcheck
4-
azure-cognitiveservices-language-textanalytics>=0.2.0 # sample won't work with previous versions
5-
azure-cognitiveservices-search-autosuggest
6-
azure-cognitiveservices-search-customimagesearch
7-
azure-cognitiveservices-search-customsearch>=0.2.0 # sample won't work with previous versions
4+
azure-cognitiveservices-language-textanalytics
5+
azure-cognitiveservices-search-autosuggest>=1.0
6+
azure-cognitiveservices-search-customsearch
87
azure-cognitiveservices-search-entitysearch
98
azure-cognitiveservices-search-imagesearch
10-
azure-cognitiveservices-search-newssearch
9+
azure-cognitiveservices-search-newssearch>=2.0.0
1110
azure-cognitiveservices-search-videosearch
1211
azure-cognitiveservices-search-visualsearch>=0.2.0 # sample won't work with previous versions
1312
azure-cognitiveservices-search-websearch
@@ -16,4 +15,5 @@ azure-cognitiveservices-vision-contentmoderator>=1.0.0 # sample won't work with
1615
azure-cognitiveservices-vision-customvision>=0.4.0 # sample won't work with previous versions
1716
azure-cognitiveservices-vision-face
1817
azure-cognitiveservices-anomalydetector>=0.2.0 # sample won't work with previous versions
18+
azure-cognitiveservices-inkrecognizer>=1.0.0b1
1919
pandas

samples/anomaly_detector_samples.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import pandas as pd
77
import os
88

9-
SUBSCRIPTION_KEY_ENV_NAME = "ANOMALYDETECTOR_SUBSCRIPTION_KEY"
10-
ANOMALYDETECTOR_LOCATION = os.environ.get("ANOMALYDETECTOR_LOCATION", "westcentralus")
9+
# Add your Azure Anomaly Detector subscription key to your environment variables.
10+
SUBSCRIPTION_KEY = os.environ.get["ANOMALY_DETECTOR_SUBSCRIPTION_KEY"]
11+
12+
CSV_FOLDER = os.path.join(os.path.dirname(
13+
os.path.realpath(__file__)), "csv_files")
1114

12-
CSV_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "csv_files")
1315

1416
def get_series_from_file(path):
1517
df = pd.read_csv(path, header=None, encoding="utf-8", parse_dates=[0])
@@ -18,16 +20,21 @@ def get_series_from_file(path):
1820
series.append(Point(timestamp=row[0], value=row[1]))
1921
return series
2022

23+
2124
def get_request():
22-
series = get_series_from_file(os.path.join(CSV_FOLDER, "anomaly_detector_daily_series.csv"))
25+
series = get_series_from_file(os.path.join(
26+
CSV_FOLDER, "anomaly_detector_daily_series.csv"))
2327
return Request(series=series, granularity=Granularity.daily)
2428

29+
2530
def entire_detect(subscription_key):
2631
print("Sample of detecting anomalies in the entire series.")
27-
28-
endpoint = "https://{}.api.cognitive.microsoft.com".format(ANOMALYDETECTOR_LOCATION)
32+
# Add your Azure Anomaly Detector subscription key to your environment variables.
33+
endpoint = os.environ.get["ANOMALY_DETECTOR_ENDPOINT"]
34+
2935
try:
30-
client = AnomalyDetectorClient(endpoint, CognitiveServicesCredentials(subscription_key))
36+
client = AnomalyDetectorClient(
37+
endpoint, CognitiveServicesCredentials(subscription_key))
3138
request = get_request()
3239
response = client.entire_detect(request)
3340
if True in response.is_anomaly:
@@ -44,12 +51,15 @@ def entire_detect(subscription_key):
4451
else:
4552
print(e)
4653

54+
4755
def last_detect(subscription_key):
4856
print("Sample of detecting whether the latest point in series is anomaly.")
49-
50-
endpoint = "https://{}.api.cognitive.microsoft.com".format(ANOMALYDETECTOR_LOCATION)
57+
# Add your Azure Anomaly Detector subscription key to your environment variables.
58+
endpoint = os.environ.get["ANOMALY_DETECTOR_ENDPOINT"]
59+
5160
try:
52-
client = AnomalyDetectorClient(endpoint, CognitiveServicesCredentials(subscription_key))
61+
client = AnomalyDetectorClient(
62+
endpoint, CognitiveServicesCredentials(subscription_key))
5363
request = get_request()
5464
response = client.last_detect(request)
5565
if response.is_anomaly:
@@ -63,8 +73,10 @@ def last_detect(subscription_key):
6373
else:
6474
print(e)
6575

76+
6677
if __name__ == "__main__":
67-
import sys, os.path
78+
import sys
79+
import os.path
6880
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
6981
from tools import execute_samples
70-
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)
82+
execute_samples(globals(), SUBSCRIPTION_KEY)

samples/face/identify.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import os
2+
import uuid
3+
import time
4+
5+
from azure.cognitiveservices.vision.face import FaceClient
6+
from msrest.authentication import CognitiveServicesCredentials
7+
from azure.cognitiveservices.vision.face.models import TrainingStatusType, Person
8+
9+
# NOTE: Replace this with a valid Face subscription key.
10+
SUBSCRIPTION_KEY = "INSERT KEY HERE"
11+
12+
# You must use the same region as you used to get your subscription
13+
# keys. For example, if you got your subscription keys from westus,
14+
# replace "westcentralus" with "westus".
15+
#
16+
# Free trial subscription keys are generated in the westcentralus
17+
# region. If you use a free trial subscription key, you shouldn't
18+
# need to change the region.
19+
FACE_LOCATION = "westcentralus"
20+
21+
face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION)
22+
face_client = FaceClient(face_base_url, CognitiveServicesCredentials(SUBSCRIPTION_KEY))
23+
24+
# This image should contain a single face.
25+
remote_image_URL_1 = "https://www.biography.com/.image/t_share/MTQ1MzAyNzYzOTgxNTE0NTEz/john-f-kennedy---mini-biography.jpg"
26+
27+
# This image should contain several faces, at least one of which is similar to the face in remote_image_URL_1.
28+
remote_image_URL_2 = "https://www.biography.com/.image/t_share/MTQ1NDY3OTIxMzExNzM3NjE3/john-f-kennedy---debating-richard-nixon.jpg"
29+
30+
# Detect faces in a remote image.
31+
def detect_faces(face_client, image_url):
32+
print ("Detecting faces...")
33+
detected_faces = face_client.face.detect_with_url(url=image_url)
34+
if not detected_faces:
35+
raise Exception('No face detected from image {}'.format(image_url))
36+
if not detected_faces[0]:
37+
raise Exception("Parameter return_face_id of detect_with_stream or detect_with_url must be set to true (by default) for recognition purpose.")
38+
return detected_faces
39+
40+
# Find similar faces to @face_ID in @face_IDs.
41+
def find_similar_faces(face_client, face_ID, face_IDs):
42+
print("Finding similar faces ...")
43+
return face_client.face.find_similar(face_id=face_ID, face_ids=face_IDs)
44+
45+
# Detect a face in the first image.
46+
faces_1 = detect_faces(face_client, remote_image_URL_1)
47+
if not faces_1[0]:
48+
print("No faces detected in " + remote_image_URL_1 + ".")
49+
else:
50+
print("Face IDs of faces detected in " + remote_image_URL_1 + ":")
51+
for x in faces_1: print (x.face_id)
52+
53+
print("Using first face ID.")
54+
face_ID = faces_1[0].face_id
55+
56+
# Detect a list of faces in the second image.
57+
faces_2 = detect_faces(face_client, remote_image_URL_2)
58+
if not faces_2[0]:
59+
print("No faces detected in " + remote_image_URL_2 + ".")
60+
else:
61+
print("Face IDs of faces detected in " + remote_image_URL_2 + ":")
62+
for x in faces_2: print (x.face_id)
63+
64+
# Search the faces detected in the second image to find a similar face to the first one.
65+
similar_faces = find_similar_faces(face_client, face_ID, list(map(lambda x: x.face_id, faces_2)))
66+
if not similar_faces[0]:
67+
print("No similar faces found in " + remote_image_URL_2 + ".")
68+
else:
69+
print("Similar faces found in " + remote_image_URL_2 + ":")
70+
for face in similar_faces:
71+
face_ID = face.face_id
72+
# SimilarFace only contains a Face ID, Persisted Face ID, and confidence score.
73+
# So we look up the Face ID in the list of DetectedFaces found in
74+
# remote_image_URL_2 to get the rest of the face information.
75+
face_info = next(x for x in faces_2 if x.face_id == face_ID)
76+
if face_info:
77+
print("Face ID: " + face_ID)
78+
print("Face rectangle:")
79+
print("Left: " + str(face_info.face_rectangle.left))
80+
print("Top: " + str(face_info.face_rectangle.top))
81+
print("Width: " + str(face_info.face_rectangle.width))
82+
print("Height: " + str(face_info.face_rectangle.height))

samples/knowledge/qna_maker_samples.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
from azure.cognitiveservices.knowledge.qnamaker.models import QnADTO, MetadataDTO, CreateKbDTO, OperationStateType, UpdateKbOperationDTO, UpdateKbOperationDTOAdd
66
from msrest.authentication import CognitiveServicesCredentials
77

8-
SUBSCRIPTION_KEY_ENV_NAME = "QNA_SUBSCRIPTION_KEY"
9-
QNA_LOCATION = os.environ.get("QNA_LOCATION", "westus")
8+
# Add your QnaMaker subscription key and endpoint to your environment variables.
9+
SUBSCRIPTION_KEY = os.environ['QNA_MAKER_SUBSCRIPTION_KEY']
10+
QNA_ENDPOINT = os.environ['QNA_MAKER_ENDPOINT']
11+
1012

1113
def knowledge_based_crud_sample(subscription_key):
1214
"""KnowledgeBasedCRUDSample.
@@ -23,17 +25,18 @@ def _create_sample_kb(client):
2325
questions=["How do I manage my knowledgebase?"],
2426
metadata=[MetadataDTO(name="Category", value="api")]
2527
)
26-
urls = ["https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs"]
28+
urls = [
29+
"https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs"]
2730
create_kb_dto = CreateKbDTO(
2831
name="QnA Maker FAQ from quickstart",
2932
qna_list=[qna],
3033
urls=urls
3134
)
32-
create_op = client.knowledgebase.create(create_kb_payload=create_kb_dto)
35+
create_op = client.knowledgebase.create(
36+
create_kb_payload=create_kb_dto)
3337
create_op = _monitor_operation(client=client, operation=create_op)
3438
return create_op.resource_location.replace("/knowledgebases/", "")
3539

36-
3740
def _monitor_operation(client, operation):
3841
"""Helper function for knowledge_based_crud_sample.
3942
@@ -42,17 +45,19 @@ def _monitor_operation(client, operation):
4245
"""
4346
for i in range(20):
4447
if operation.operation_state in [OperationStateType.not_started, OperationStateType.running]:
45-
print("Waiting for operation: {} to complete.".format(operation.operation_id))
48+
print("Waiting for operation: {} to complete.".format(
49+
operation.operation_id))
4650
time.sleep(5)
47-
operation = client.operations.get_details(operation_id=operation.operation_id)
51+
operation = client.operations.get_details(
52+
operation_id=operation.operation_id)
4853
else:
4954
break
5055
if operation.operation_state != OperationStateType.succeeded:
51-
raise Exception("Operation {} failed to complete.".format(operation.operation_id))
56+
raise Exception("Operation {} failed to complete.".format(
57+
operation.operation_id))
5258
return operation
5359

54-
55-
client = QnAMakerClient(endpoint="https://{}.api.cognitive.microsoft.com".format(QNA_LOCATION), credentials=CognitiveServicesCredentials(subscription_key))
60+
client = QnAMakerClient(endpoint=QNA_ENDPOINT, credentials=CognitiveServicesCredentials(subscription_key))
5661

5762
# Create a KB
5863
print("Creating KB...")
@@ -68,7 +73,8 @@ def _monitor_operation(client, operation):
6873
]
6974
)
7075
)
71-
update_op = client.knowledgebase.update(kb_id=kb_id, update_kb=update_kb_operation_dto)
76+
update_op = client.knowledgebase.update(
77+
kb_id=kb_id, update_kb=update_kb_operation_dto)
7278
_monitor_operation(client=client, operation=update_op)
7379

7480
# Publish the KB

0 commit comments

Comments
 (0)