Skip to content

Commit 4b5a63c

Browse files
iscai-msftlmazuel
authored andcommitted
Content moderator samples update (#29)
* fixed image review sample * fixed team name value to prompt user for value input * fixed video review sample * code reformatting in image and video review samples * fixed text moderation sample * fixed image job samples * fixed image list samples * fixed image moderation sample * fixed term list samples and renamed text moderation sample text file * fixed video transcript sample * Update requirements.txt
1 parent da38d8f commit 4b5a63c

13 files changed

+226
-194
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ ENV/
105105

106106
#DS store
107107
.DS_Store
108-
samples/DS_Store
108+
samples/DS_Store

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ azure-cognitiveservices-search-videosearch
1212
azure-cognitiveservices-search-visualsearch>=0.2.0 # sample won't work with previous versions
1313
azure-cognitiveservices-search-websearch
1414
azure-cognitiveservices-vision-computervision>=0.2.0 # sample won't work with previous versions
15-
azure-cognitiveservices-vision-contentmoderator
15+
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

samples/vision/content_moderator_image_job_samples.py

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from pprint import pprint
33

44
from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient
5-
from azure.cognitiveservices.vision.contentmoderator.models import Content
65
from msrest.authentication import CognitiveServicesCredentials
76

87
SUBSCRIPTION_KEY_ENV_NAME = "CONTENTMODERATOR_SUBSCRIPTION_KEY"
@@ -17,13 +16,13 @@ def image_review_jobs(subscription_key):
1716
# The moderation job will use this workflow that you defined earlier.
1817
# See the quickstart article to learn how to setup custom workflows.
1918
# https://docs.microsoft.com/azure/cognitive-services/content-moderator/review-tool-user-guide/workflows
20-
workflow_name = "OCR"
19+
workflow_name = "insert your workflow name here"
2120

2221
# The name of the team to assign the job to.
2322
# This must be the team name you used to create your Content Moderator account. You can
2423
# retrieve your team name from the Content Moderator web site. Your team name is the Id
2524
# associated with your subscription.
26-
team_name = "pysdktesting"
25+
team_name = "insert your team name here"
2726

2827
# An image with this text:
2928
# IF WE DID ALL THE THINGS WE ARE CAPABLE OF DOING, WE WOULD LITERALLY ASTOUND OURSELVE
@@ -34,54 +33,71 @@ def image_review_jobs(subscription_key):
3433
call_back_endpoint = "https://requestb.in/1l64pe71"
3534

3635
client = ContentModeratorClient(
37-
CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
38-
CognitiveServicesCredentials(subscription_key)
36+
endpoint='https://'+CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
37+
credentials=CognitiveServicesCredentials(subscription_key)
3938
)
4039

4140
print("Create moderation job for an image.\n")
4241
job_result = client.reviews.create_job(
43-
team_name,
44-
"Image", # Possible values include: 'Image', 'Text', 'Video'
45-
"ContentID", # Id/Name to identify the content submitted.
46-
workflow_name,
47-
"application/json", # Possible values include: 'application/json', 'image/jpeg'
48-
image_url,
42+
team_name=team_name,
43+
content_type="Image", # Possible values include: 'Image', 'Text', 'Video'
44+
content_id="ContentID", # Id/Name to identify the content submitted.
45+
workflow_name=workflow_name,
46+
job_content_type="application/json", # Possible values include: 'application/json', 'image/jpeg'
47+
content_value=image_url,
4948
call_back_endpoint=call_back_endpoint
5049
)
5150
job_id = job_result.job_id
5251

5352
print("Get job status before review.")
5453
job_details = client.reviews.get_job_details(
55-
team_name,
56-
job_id,
54+
team_name=team_name,
55+
job_id=job_id,
5756
)
5857
pprint(job_details.as_dict())
5958

6059
input("\nPerform manual reviews on the Content Moderator Review Site, and hit enter here.")
6160
job_details = client.reviews.get_job_details(
62-
team_name,
63-
job_id,
61+
team_name=team_name,
62+
job_id=job_id,
6463
)
6564
pprint(job_details.as_dict())
6665

6766
# Your call back endpoint should have received an event like this:
68-
# {
69-
# "ReviewId": "201802i2957280730e04578aefe678ba6e5ea24",
70-
# "ModifiedOn": "2018-02-06T23:46:26.3121698Z",
71-
# "ModifiedBy": "unknown",
72-
# "CallBackType": "Review",
73-
# "ContentId": "ContentID",
74-
# "ContentType": "Image",
75-
# "Metadata": {
76-
# "hastext": "True",
77-
# "ocrtext": "IF WE DID \r\nALL \r\nTHE THINGS \r\nWE ARE \r\nCAPABLE \r\nOF DOING, \r\nWE WOULD \r\nLITERALLY \r\nASTOUND \r\nOURSELVE \r\n",
78-
# "imagename": "ContentID"
79-
# },
80-
# "ReviewerResultTags": {
81-
# "a": "True",
82-
# "r": "False"
83-
# }
84-
# }
67+
# {'call_back_endpoint': 'https://requestb.in/1l64pe71',
68+
# 'id': '201901d49ee1a417ae45a991c5c1d6af25cace',
69+
# 'job_execution_report': [{'msg': 'Posted results to the Callbackendpoint: '
70+
# 'https://requestb.in/1l64pe71',
71+
# 'ts': '2019-01-11T00:00:15.738452'},
72+
# {'msg': 'Job marked completed and job content has '
73+
# 'been removed',
74+
# 'ts': '2019-01-11T00:00:15.6583757'},
75+
# {'msg': 'Execution Complete',
76+
# 'ts': '2019-01-11T00:00:15.4872128'},
77+
# {'msg': 'Successfully got hasText response from '
78+
# 'Moderator',
79+
# 'ts': '2019-01-11T00:00:14.1389317'},
80+
# {'msg': 'Getting hasText from Moderator',
81+
# 'ts': '2019-01-11T00:00:13.0689178'},
82+
# {'msg': 'Starting Execution - Try 1',
83+
# 'ts': '2019-01-11T00:00:12.1120066'}],
84+
# 'result_meta_data': [{'key': 'hasText', 'value': 'True'},
85+
# {'key': 'ocrText',
86+
# 'value': 'IF WE DID \r\n'
87+
# 'ALL \r\n'
88+
# 'THE THINGS \r\n'
89+
# 'WE ARE \r\n'
90+
# 'CAPABLE \r\n'
91+
# 'OF DOING, \r\n'
92+
# 'WE WOULD \r\n'
93+
# 'LITERALLY \r\n'
94+
# 'ASTOUND \r\n'
95+
# 'OURSELVE \r\n'}],
96+
# 'review_id': '201901i6e4de824b0cf4aa587ac37f922f584c2',
97+
# 'status': 'Complete',
98+
# 'team_name': 'cspythonsdk',
99+
# 'type': 'Image',
100+
# # 'workflow_id': 'textdetection'}
85101

86102

87103
if __name__ == "__main__":

samples/vision/content_moderator_image_list_samples.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ def image_lists(subscription_key):
4848
"""
4949

5050
client = ContentModeratorClient(
51-
CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
52-
CognitiveServicesCredentials(subscription_key)
51+
endpoint='https://'+CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
52+
credentials=CognitiveServicesCredentials(subscription_key)
5353
)
5454

5555
print("Creating list MyList\n")
5656
custom_list = client.list_management_image_lists.create(
57-
"application/json",
58-
{
57+
content_type="application/json",
58+
body={
5959
"name": "MyList",
6060
"description": "A sample list",
6161
"metadata": {
@@ -78,8 +78,8 @@ def add_images(list_id, image_url, label):
7878
print("\nAdding image {} to list {} with label {}.".format(image_url, list_id, label))
7979
try:
8080
added_image = client.list_management_image.add_image_url_input(
81-
list_id,
82-
"application/json",
81+
list_id=list_id,
82+
content_type="application/json",
8383
data_representation="URL",
8484
value=image_url,
8585
label=label
@@ -104,7 +104,7 @@ def add_images(list_id, image_url, label):
104104
# Get all images ids
105105
#
106106
print("\nGetting all image IDs for list {}".format(list_id))
107-
image_ids = client.list_management_image.get_all_image_ids(list_id)
107+
image_ids = client.list_management_image.get_all_image_ids(list_id=list_id)
108108
assert isinstance(image_ids, ImageIds)
109109
pprint(image_ids.as_dict())
110110

@@ -113,9 +113,9 @@ def add_images(list_id, image_url, label):
113113
#
114114
print("\nUpdating details for list {}".format(list_id))
115115
updated_list = client.list_management_image_lists.update(
116-
list_id,
117-
"application/json",
118-
{
116+
list_id=list_id,
117+
content_type="application/json",
118+
body={
119119
"name": "Swimsuits and sports"
120120
}
121121
)
@@ -126,15 +126,15 @@ def add_images(list_id, image_url, label):
126126
# Get list details
127127
#
128128
print("\nGetting details for list {}".format(list_id))
129-
list_details = client.list_management_image_lists.get_details(list_id)
129+
list_details = client.list_management_image_lists.get_details(list_id=list_id)
130130
assert isinstance(list_details, ImageList)
131131
pprint(list_details.as_dict())
132132

133133
#
134134
# Refresh the index
135135
#
136136
print("\nRefreshing the search index for list {}".format(list_id))
137-
refresh_index = client.list_management_image_lists.refresh_index_method(list_id)
137+
refresh_index = client.list_management_image_lists.refresh_index_method(list_id=list_id)
138138
assert isinstance(refresh_index, RefreshIndex)
139139
pprint(refresh_index.as_dict())
140140

@@ -147,8 +147,8 @@ def add_images(list_id, image_url, label):
147147
for image_url in IMAGES_TO_MATCH:
148148
print("\nMatching image {} against list {}".format(image_url, list_id))
149149
match_result = client.image_moderation.match_url_input(
150-
"application/json",
151-
list_id,
150+
content_type="application/json",
151+
list_id=list_id,
152152
data_representation="URL",
153153
value=image_url,
154154
)
@@ -163,15 +163,15 @@ def add_images(list_id, image_url, label):
163163
correction = "https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png"
164164
print("\nRemove image {} from list {}".format(correction, list_id))
165165
client.list_management_image.delete_image(
166-
list_id,
167-
index[correction]
166+
list_id=list_id,
167+
image_id=index[correction]
168168
)
169169

170170
#
171171
# Refresh the index
172172
#
173173
print("\nRefreshing the search index for list {}".format(list_id))
174-
client.list_management_image_lists.refresh_index_method(list_id)
174+
client.list_management_image_lists.refresh_index_method(list_id=list_id)
175175

176176
print("\nWaiting {} minutes to allow the server time to propagate the index changes.".format(LATENCY_DELAY))
177177
time.sleep(LATENCY_DELAY * 60)
@@ -183,8 +183,8 @@ def add_images(list_id, image_url, label):
183183
for image_url in IMAGES_TO_MATCH:
184184
print("\nMatching image {} against list {}".format(image_url, list_id))
185185
match_result = client.image_moderation.match_url_input(
186-
"application/json",
187-
list_id,
186+
content_type="application/json",
187+
list_id=list_id,
188188
data_representation="URL",
189189
value=image_url,
190190
)
@@ -197,13 +197,13 @@ def add_images(list_id, image_url, label):
197197
# Delete all images
198198
#
199199
print("\nDelete all images in the image list {}".format(list_id))
200-
client.list_management_image.delete_all_images(list_id)
200+
client.list_management_image.delete_all_images(list_id=list_id)
201201

202202
#
203203
# Delete list
204204
#
205205
print("\nDelete the image list {}".format(list_id))
206-
client.list_management_image_lists.delete(list_id)
206+
client.list_management_image_lists.delete(list_id=list_id)
207207

208208
#
209209
# Get all list ids

samples/vision/content_moderator_image_moderation_samples.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import os.path
22
from pprint import pprint
3-
import time
43

54
from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient
65
from azure.cognitiveservices.vision.contentmoderator.models import (
7-
APIErrorException,
86
Evaluate,
97
OCR,
108
FoundFaces
@@ -26,27 +24,27 @@ def image_moderation(subscription_key):
2624
"""
2725

2826
client = ContentModeratorClient(
29-
CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
30-
CognitiveServicesCredentials(subscription_key)
27+
endpoint='https://'+CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
28+
credentials=CognitiveServicesCredentials(subscription_key)
3129
)
3230

3331
for image_url in IMAGE_LIST:
3432
print("\nEvaluate image {}".format(image_url))
3533

3634
print("\nEvaluate for adult and racy content.")
3735
evaluation = client.image_moderation.evaluate_url_input(
38-
"application/json",
39-
data_representation="URL",
40-
value=image_url,
36+
content_type="application/json",
4137
cache_image=True,
38+
data_representation="URL",
39+
value=image_url
4240
)
4341
assert isinstance(evaluation, Evaluate)
4442
pprint(evaluation.as_dict())
4543

4644
print("\nDetect and extract text.")
4745
evaluation = client.image_moderation.ocr_url_input(
48-
"eng",
49-
"application/json",
46+
language="eng",
47+
content_type="application/json",
5048
data_representation="URL",
5149
value=image_url,
5250
cache_image=True,
@@ -56,10 +54,10 @@ def image_moderation(subscription_key):
5654

5755
print("\nDetect faces.")
5856
evaluation = client.image_moderation.find_faces_url_input(
59-
"application/json",
60-
data_representation="URL",
61-
value=image_url,
57+
content_type="application/json",
6258
cache_image=True,
59+
data_representation="URL",
60+
value=image_url
6361
)
6462
assert isinstance(evaluation, FoundFaces)
6563
pprint(evaluation.as_dict())

0 commit comments

Comments
 (0)