Skip to content

Commit 4a75c2d

Browse files
authored
Merge branch 'master' into news_search
2 parents 5298ba5 + 4250fb7 commit 4a75c2d

File tree

57 files changed

+2980
-694
lines changed

Some content is hidden

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

57 files changed

+2980
-694
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@ ENV/
101101
.mypy_cache/
102102

103103
# idea
104-
.idea/
104+
.idea/

README.md

Lines changed: 23 additions & 10 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
@@ -12,16 +16,21 @@ These samples will show you how to get up and running using the Python SDKs for
1216

1317
This project framework provides examples for the following services:
1418

19+
### Knowledge
20+
* 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/)
21+
22+
1523
### Language
1624

1725
* 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/)
1826
* 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/)
19-
* 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/)
2028

2129
### Search
2230

23-
* Using the **Bing Autosuggest SDK** [azure-cognitiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-autosuggest) for the [Autosuggest API](https://azure.microsoft.com/services/cognitive-services/autosuggest/)
31+
* 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/)
2432
* 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/)
33+
* 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/)
2534
* 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/)
2635
* 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/)
2736
* 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/)
@@ -35,6 +44,7 @@ This project framework provides examples for the following services:
3544
* 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/)
3645
* 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/)
3746
* 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/)
3848

3949
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.
4050

@@ -85,6 +95,7 @@ We provide several meta-packages to help you install several packages at a time.
8595
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).
8696
3. Set up the environment variable `AUTOSUGGEST_SUBSCRIPTION_KEY` with your key if you want to execute Autosuggest tests.
8797
3. Set up the environment variable `CUSTOMSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomSearch tests.
98+
3. Set up the environment variable `CUSTOMIMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomImageSearch tests.
8899
3. Set up the environment variable `ENTITYSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute EntitySearch tests.
89100
4. Set up the environment variable `IMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute ImageSearch tests.
90101
4. Set up the environment variable `NEWSSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute NewsSearch tests.
@@ -93,8 +104,9 @@ We provide several meta-packages to help you install several packages at a time.
93104
4. Set up the environment variable `WEBSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute WebSearch tests.
94105
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).
95106
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).
96-
4. Set up the environment variable `CUSTOMVISION_TRAINING_KEY` with your key if you want to execute CustomVision Training tests.
97-
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+
98110
99111
## Demo
100112
@@ -104,9 +116,10 @@ To run the complete demo, execute `python example.py`
104116
105117
To run each individual demo, point directly to the file. For example (i.e. not complete list):
106118
107-
2. `python samples/language/spellcheck_samples.py`
108-
1. `python samples/search/entity_search_samples.py`
109-
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`
110123
111124
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.
112125

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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
azure-cognitiveservices-knowledge-qnamaker
12
azure-cognitiveservices-language-luis
23
azure-cognitiveservices-language-spellcheck
34
azure-cognitiveservices-language-textanalytics
4-
azure-cognitiveservices-search-autosuggest
5+
azure-cognitiveservices-search-autosuggest>=1.0
56
azure-cognitiveservices-search-customsearch
67
azure-cognitiveservices-search-entitysearch
78
azure-cognitiveservices-search-imagesearch
89
azure-cognitiveservices-search-newssearch>=2.0.0
910
azure-cognitiveservices-search-videosearch
10-
azure-cognitiveservices-search-visualsearch
11+
azure-cognitiveservices-search-visualsearch>=0.2.0 # sample won't work with previous versions
1112
azure-cognitiveservices-search-websearch
12-
azure-cognitiveservices-vision-computervision>=0.2.0 # sample won't work with previous versions
13-
azure-cognitiveservices-vision-contentmoderator
14-
azure-cognitiveservices-vision-customvision
15-
azure-cognitiveservices-vision-face
13+
azure-cognitiveservices-vision-computervision>=0.3.0 # sample won't work with previous versions
14+
azure-cognitiveservices-vision-contentmoderator>=1.0.0 # sample won't work with previous versions
15+
azure-cognitiveservices-vision-customvision>=0.4.0 # sample won't work with previous versions
16+
azure-cognitiveservices-vision-face
17+
azure-cognitiveservices-anomalydetector>=0.2.0 # sample won't work with previous versions
18+
azure-cognitiveservices-inkrecognizer>=1.0.0b1
19+
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/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)