1+ import os .path
2+ from pprint import pprint
3+ import uuid
4+
5+ from azure .cognitiveservices .vision .contentmoderator import ContentModeratorClient
6+ from azure .cognitiveservices .vision .contentmoderator .models import Content , Review
7+ from msrest .authentication import CognitiveServicesCredentials
8+
9+ SUBSCRIPTION_KEY_ENV_NAME = "CONTENTMODERATOR_SUBSCRIPTION_KEY"
10+ CONTENTMODERATOR_LOCATION = os .environ .get ("CONTENTMODERATOR_LOCATION" , "westcentralus" )
11+
12+ def image_review (subscription_key ):
13+ """ImageReview.
14+
15+ This will create a review for images.
16+ """
17+
18+ # The name of the team to assign the job to.
19+ # This must be the team name you used to create your Content Moderator account. You can
20+ # retrieve your team name from the Content Moderator web site. Your team name is the Id
21+ # associated with your subscription.
22+ team_name = "pysdktesting"
23+
24+ # An image to review
25+ image_url = "https://moderatorsampleimages.blob.core.windows.net/samples/sample5.png"
26+
27+ # Where you want to receive the approval/refuse event. This is the only way to get this information.
28+ call_back_endpoint = "https://requestb.in/qmsakwqm"
29+
30+ client = ContentModeratorClient (
31+ CONTENTMODERATOR_LOCATION + '.api.cognitive.microsoft.com' ,
32+ CognitiveServicesCredentials (subscription_key )
33+ )
34+
35+ print ("Create review for {}.\n " .format (image_url ))
36+ review_item = {
37+ "type" : "Image" , # Possible values include: 'Image', 'Text'
38+ "content" : image_url , # How to download the image
39+ "content_id" : uuid .uuid4 (), # Random id
40+ "callback_endpoint" : call_back_endpoint ,
41+ "metadata" : [{
42+ "key" : "sc" ,
43+ "value" : True # will be sent to Azure as "str" cast.
44+ }]
45+ }
46+
47+ reviews = client .reviews .create_reviews (
48+ "application/json" ,
49+ team_name ,
50+ [review_item ] # As many review item as you need
51+ )
52+ review_id = reviews [0 ] # Ordered list of string of review ID
53+
54+ print ("\n Get review details" )
55+ review_details = client .reviews .get_review (team_name , review_id )
56+ pprint (review_details .as_dict ())
57+
58+ input ("\n Perform manual reviews on the Content Moderator Review Site, and hit enter here." )
59+
60+ print ("\n Get review details" )
61+ review_details = client .reviews .get_review (team_name , review_id )
62+ pprint (review_details .as_dict ())
63+
64+ # Your call back endpoint should have received an event like this:
65+ # {
66+ # "ReviewId": "201802idca99b6f3f7d4418b381d8c1bcc7e99a",
67+ # "ModifiedOn": "2018-02-07T22:39:17.2868098Z",
68+ # "ModifiedBy": "unknown",
69+ # "CallBackType": "Review",
70+ # "ContentId": "3dd26599-264a-4c3d-af00-3d8726a59e95",
71+ # "ContentType": "Image",
72+ # "Metadata": {
73+ # "sc": "True"
74+ # },
75+ # "ReviewerResultTags": {
76+ # "a": "True",
77+ # "r": "False"
78+ # }
79+ # }
80+
81+ if __name__ == "__main__" :
82+ import sys , os .path
83+ sys .path .append (os .path .abspath (os .path .join (__file__ , ".." , ".." )))
84+ from tools import execute_samples
85+ execute_samples (globals (), SUBSCRIPTION_KEY_ENV_NAME )
0 commit comments