Skip to content

Commit c9a10c9

Browse files
authored
Merge branch 'master' into update-3.0
2 parents 4730cca + 76a0b4a commit c9a10c9

Some content is hidden

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

52 files changed

+1894
-674
lines changed

README.md

Lines changed: 15 additions & 7 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

@@ -101,6 +107,7 @@ We provide several meta-packages to help you install several packages at a time.
101107
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.
102108
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.
103109
110+
104111
## Demo
105112
106113
A demo app is included to show how to use the project.
@@ -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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ azure-cognitiveservices-vision-computervision>=0.3.0 # sample won't work with p
1515
azure-cognitiveservices-vision-contentmoderator>=1.0.0 # sample won't work with previous versions
1616
azure-cognitiveservices-vision-customvision>=0.4.0 # sample won't work with previous versions
1717
azure-cognitiveservices-vision-face
18+
azure-cognitiveservices-anomalydetector>=0.2.0 # sample won't work with previous versions
19+
azure-cognitiveservices-inkrecognizer>=1.0.0b1
20+
pandas
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from azure.cognitiveservices.anomalydetector import AnomalyDetectorClient
2+
from azure.cognitiveservices.anomalydetector.models import Request, Point, Granularity, \
3+
APIErrorException
4+
from datetime import datetime, timezone
5+
from msrest.authentication import CognitiveServicesCredentials
6+
import pandas as pd
7+
import os
8+
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")
14+
15+
16+
def get_series_from_file(path):
17+
df = pd.read_csv(path, header=None, encoding="utf-8", parse_dates=[0])
18+
series = []
19+
for index, row in df.iterrows():
20+
series.append(Point(timestamp=row[0], value=row[1]))
21+
return series
22+
23+
24+
def get_request():
25+
series = get_series_from_file(os.path.join(
26+
CSV_FOLDER, "anomaly_detector_daily_series.csv"))
27+
return Request(series=series, granularity=Granularity.daily)
28+
29+
30+
def entire_detect(subscription_key):
31+
print("Sample of detecting anomalies in the entire series.")
32+
# Add your Azure Anomaly Detector subscription key to your environment variables.
33+
endpoint = os.environ.get["ANOMALY_DETECTOR_ENDPOINT"]
34+
35+
try:
36+
client = AnomalyDetectorClient(
37+
endpoint, CognitiveServicesCredentials(subscription_key))
38+
request = get_request()
39+
response = client.entire_detect(request)
40+
if True in response.is_anomaly:
41+
print("Anomaly was detected from the series at index:")
42+
for i in range(len(request.series)):
43+
if response.is_anomaly[i]:
44+
print(i)
45+
else:
46+
print("There is no anomaly detected from the series.")
47+
except Exception as e:
48+
if isinstance(e, APIErrorException):
49+
print("Error code: {}".format(e.error.code))
50+
print("Error message: {}".format(e.error.message))
51+
else:
52+
print(e)
53+
54+
55+
def last_detect(subscription_key):
56+
print("Sample of detecting whether the latest point in series is anomaly.")
57+
# Add your Azure Anomaly Detector subscription key to your environment variables.
58+
endpoint = os.environ.get["ANOMALY_DETECTOR_ENDPOINT"]
59+
60+
try:
61+
client = AnomalyDetectorClient(
62+
endpoint, CognitiveServicesCredentials(subscription_key))
63+
request = get_request()
64+
response = client.last_detect(request)
65+
if response.is_anomaly:
66+
print("The latest point is detected as anomaly.")
67+
else:
68+
print("The latest point is not detected as anomaly.")
69+
except Exception as e:
70+
if isinstance(e, APIErrorException):
71+
print("Error code: {}".format(e.error.code))
72+
print("Error message: {}".format(e.error.message))
73+
else:
74+
print(e)
75+
76+
77+
if __name__ == "__main__":
78+
import sys
79+
import os.path
80+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
81+
from tools import execute_samples
82+
execute_samples(globals(), SUBSCRIPTION_KEY)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2018-03-01T00:00:00Z,32858923
2+
2018-03-02T00:00:00Z,29615278
3+
2018-03-03T00:00:00Z,22839355
4+
2018-03-04T00:00:00Z,25948736
5+
2018-03-05T00:00:00Z,34139159
6+
2018-03-06T00:00:00Z,33843985
7+
2018-03-07T00:00:00Z,33637661
8+
2018-03-08T00:00:00Z,32627350
9+
2018-03-09T00:00:00Z,29881076
10+
2018-03-10T00:00:00Z,22681575
11+
2018-03-11T00:00:00Z,24629393
12+
2018-03-12T00:00:00Z,34010679
13+
2018-03-13T00:00:00Z,33893888
14+
2018-03-14T00:00:00Z,33760076
15+
2018-03-15T00:00:00Z,33093515
16+
2018-03-16T00:00:00Z,29945555
17+
2018-03-17T00:00:00Z,22676212
18+
2018-03-18T00:00:00Z,25262514
19+
2018-03-19T00:00:00Z,33631649
20+
2018-03-20T00:00:00Z,34468310
21+
2018-03-21T00:00:00Z,34212281
22+
2018-03-22T00:00:00Z,38144434
23+
2018-03-23T00:00:00Z,34662949
24+
2018-03-24T00:00:00Z,24623684
25+
2018-03-25T00:00:00Z,26530491
26+
2018-03-26T00:00:00Z,35445003
27+
2018-03-27T00:00:00Z,34250789
28+
2018-03-28T00:00:00Z,33423012
29+
2018-03-29T00:00:00Z,30744783
30+
2018-03-30T00:00:00Z,25825128
31+
2018-03-31T00:00:00Z,21244209
32+
2018-04-01T00:00:00Z,22576956
33+
2018-04-02T00:00:00Z,31957221
34+
2018-04-03T00:00:00Z,33841228
35+
2018-04-04T00:00:00Z,33554483
36+
2018-04-05T00:00:00Z,32383350
37+
2018-04-06T00:00:00Z,29494850
38+
2018-04-07T00:00:00Z,22815534
39+
2018-04-08T00:00:00Z,25557267
40+
2018-04-09T00:00:00Z,34858252
41+
2018-04-10T00:00:00Z,34750597
42+
2018-04-11T00:00:00Z,34717956
43+
2018-04-12T00:00:00Z,34132534
44+
2018-04-13T00:00:00Z,30762236
45+
2018-04-14T00:00:00Z,22504059
46+
2018-04-15T00:00:00Z,26149060
47+
2018-04-16T00:00:00Z,35250105

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: 20 additions & 13 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
@@ -88,7 +94,8 @@ def _monitor_operation(client, operation):
8894

8995

9096
if __name__ == "__main__":
91-
import sys, os.path
97+
import sys
98+
import os.path
9299
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
93100
from tools import execute_samples
94-
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)
101+
execute_samples(globals(), SUBSCRIPTION_KEY)

0 commit comments

Comments
 (0)