11import os .path
22from pprint import pprint
3+ import time
34
45from azure .cognitiveservices .vision .contentmoderator import ContentModeratorClient
6+ from azure .cognitiveservices .vision .contentmoderator .models import (
7+ APIErrorException ,
8+ ImageList ,
9+ ImageIds ,
10+ Image ,
11+ RefreshIndex ,
12+ MatchResponse
13+ )
514from msrest .authentication import CognitiveServicesCredentials
615
716SUBSCRIPTION_KEY_ENV_NAME = "CONTENTMODERATOR_SUBSCRIPTION_KEY"
817CONTENTMODERATOR_LOCATION = os .environ .get ("CONTENTMODERATOR_LOCATION" , "westcentralus" )
918
19+ # The number of minutes to delay after updating the search index before
20+ # performing image match operations against the list.
21+ LATENCY_DELAY = 0.5
22+
23+ IMAGE_LIST = {
24+ "Sports" : [
25+ "https://moderatorsampleimages.blob.core.windows.net/samples/sample4.png" ,
26+ "https://moderatorsampleimages.blob.core.windows.net/samples/sample6.png" ,
27+ "https://moderatorsampleimages.blob.core.windows.net/samples/sample9.png"
28+ ],
29+ "Swimsuit" : [
30+ "https://moderatorsampleimages.blob.core.windows.net/samples/sample1.jpg" ,
31+ "https://moderatorsampleimages.blob.core.windows.net/samples/sample3.png" ,
32+ "https://moderatorsampleimages.blob.core.windows.net/samples/sample4.png" ,
33+ "https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png"
34+ ]
35+ }
36+
37+ IMAGES_TO_MATCH = [
38+ "https://moderatorsampleimages.blob.core.windows.net/samples/sample1.jpg" ,
39+ "https://moderatorsampleimages.blob.core.windows.net/samples/sample4.png" ,
40+ "https://moderatorsampleimages.blob.core.windows.net/samples/sample5.png" ,
41+ "https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png"
42+ ]
1043
1144def image_lists (subscription_key ):
1245 """ImageList.
@@ -31,20 +64,153 @@ def image_lists(subscription_key):
3164 }
3265 }
3366 )
34- print ("Item created:" )
67+ print ("List created:" )
68+ assert isinstance (custom_list , ImageList )
3569 pprint (custom_list .as_dict ())
3670 list_id = custom_list .id
3771
72+ #
73+ # Add images
74+ #
75+
3876 def add_images (list_id , image_url , label ):
39- print ("Adding image {} to list {} with label {}." .format (image_url , list_id , label ))
40- added_image = client .list_management_image .add_image_url_input (
77+ """Generic add_images from url and label."""
78+ print ("\n Adding image {} to list {} with label {}." .format (image_url , list_id , label ))
79+ try :
80+ added_image = client .list_management_image .add_image_url_input (
81+ list_id ,
82+ "application/json" ,
83+ data_representation = "URL" ,
84+ value = image_url ,
85+ label = label
86+ )
87+ except APIErrorException as err :
88+ # sample4 will fail
89+ print ("Unable to add image to list: {}" .format (err ))
90+ else :
91+ assert isinstance (added_image , Image )
92+ pprint (added_image .as_dict ())
93+ return added_image
94+
95+ print ("\n Adding images to list {}" .format (list_id ))
96+ index = {} # Keep an index url to id for later removal
97+ for label , urls in IMAGE_LIST .items ():
98+ for url in urls :
99+ image = add_images (list_id , url , label )
100+ if image :
101+ index [url ] = image .content_id
102+
103+ #
104+ # Get all images ids
105+ #
106+ print ("\n Getting all image IDs for list {}" .format (list_id ))
107+ image_ids = client .list_management_image .get_all_image_ids (list_id )
108+ assert isinstance (image_ids , ImageIds )
109+ pprint (image_ids .as_dict ())
110+
111+ #
112+ # Update list details
113+ #
114+ print ("\n Updating details for list {}" .format (list_id ))
115+ updated_list = client .list_management_image_lists .update (
116+ list_id ,
117+ "application/json" ,
118+ {
119+ "name" : "Swimsuits and sports"
120+ }
121+ )
122+ assert isinstance (updated_list , ImageList )
123+ pprint (updated_list .as_dict ())
124+
125+ #
126+ # Get list details
127+ #
128+ print ("\n Getting details for list {}" .format (list_id ))
129+ list_details = client .list_management_image_lists .get_details (list_id )
130+ assert isinstance (list_details , ImageList )
131+ pprint (list_details .as_dict ())
132+
133+ #
134+ # Refresh the index
135+ #
136+ print ("\n Refreshing the search index for list {}" .format (list_id ))
137+ refresh_index = client .list_management_image_lists .refresh_index_method (list_id )
138+ assert isinstance (refresh_index , RefreshIndex )
139+ pprint (refresh_index .as_dict ())
140+
141+ print ("\n Waiting {} minutes to allow the server time to propagate the index changes." .format (LATENCY_DELAY ))
142+ time .sleep (LATENCY_DELAY * 60 )
143+
144+ #
145+ # Match images against the image list.
146+ #
147+ for image_url in IMAGES_TO_MATCH :
148+ print ("\n Matching image {} against list {}" .format (image_url , list_id ))
149+ match_result = client .image_moderation .match_url_input (
150+ "application/json" ,
41151 list_id ,
152+ data_representation = "URL" ,
153+ value = image_url ,
154+ )
155+ assert isinstance (match_result , MatchResponse )
156+ print ("Is match? {}" .format (match_result .is_match ))
157+ print ("Complete match details:" )
158+ pprint (match_result .as_dict ())
159+
160+ #
161+ # Remove images
162+ #
163+ correction = "https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png"
164+ print ("\n Remove image {} from list {}" .format (correction , list_id ))
165+ client .list_management_image .delete_image (
166+ list_id ,
167+ index [correction ]
168+ )
169+
170+ #
171+ # Refresh the index
172+ #
173+ print ("\n Refreshing the search index for list {}" .format (list_id ))
174+ client .list_management_image_lists .refresh_index_method (list_id )
175+
176+ print ("\n Waiting {} minutes to allow the server time to propagate the index changes." .format (LATENCY_DELAY ))
177+ time .sleep (LATENCY_DELAY * 60 )
178+
179+ #
180+ # Re-match
181+ #
182+ print ("\n Matching image. The removed image should not match" )
183+ for image_url in IMAGES_TO_MATCH :
184+ print ("\n Matching image {} against list {}" .format (image_url , list_id ))
185+ match_result = client .image_moderation .match_url_input (
42186 "application/json" ,
187+ list_id ,
43188 data_representation = "URL" ,
44- value = image_url ,
45- label = label
189+ value = image_url ,
46190 )
47- pprint (added_image .as_dict ())
191+ assert isinstance (match_result , MatchResponse )
192+ print ("Is match? {}" .format (match_result .is_match ))
193+ print ("Complete match details:" )
194+ pprint (match_result .as_dict ())
195+
196+ #
197+ # Delete all images
198+ #
199+ print ("\n Delete all images in the image list {}" .format (list_id ))
200+ client .list_management_image .delete_all_images (list_id )
201+
202+ #
203+ # Delete list
204+ #
205+ print ("\n Delete the image list {}" .format (list_id ))
206+ client .list_management_image_lists .delete (list_id )
207+
208+ #
209+ # Get all list ids
210+ #
211+ print ("\n Verify that the list {} was deleted." .format (list_id ))
212+ image_lists = client .list_management_image_lists .get_all_image_lists ()
213+ assert not any (list_id == image_list .id for image_list in image_lists )
48214
49215if __name__ == "__main__" :
50216 import sys , os .path
0 commit comments