|
| 1 | +import os.path |
| 2 | +from pprint import pprint |
| 3 | +import time |
| 4 | + |
| 5 | +from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient |
| 6 | +from azure.cognitiveservices.vision.contentmoderator.models import ( |
| 7 | + APIErrorException, |
| 8 | + Evaluate, |
| 9 | + OCR, |
| 10 | + FoundFaces |
| 11 | +) |
| 12 | +from msrest.authentication import CognitiveServicesCredentials |
| 13 | + |
| 14 | +SUBSCRIPTION_KEY_ENV_NAME = "CONTENTMODERATOR_SUBSCRIPTION_KEY" |
| 15 | +CONTENTMODERATOR_LOCATION = os.environ.get("CONTENTMODERATOR_LOCATION", "westcentralus") |
| 16 | + |
| 17 | +IMAGE_LIST = [ |
| 18 | + "https://moderatorsampleimages.blob.core.windows.net/samples/sample2.jpg", |
| 19 | + "https://moderatorsampleimages.blob.core.windows.net/samples/sample5.png" |
| 20 | +] |
| 21 | + |
| 22 | +def image_moderation(subscription_key): |
| 23 | + """ImageModeration. |
| 24 | +
|
| 25 | + This will review an image using workflow and job. |
| 26 | + """ |
| 27 | + |
| 28 | + client = ContentModeratorClient( |
| 29 | + CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com', |
| 30 | + CognitiveServicesCredentials(subscription_key) |
| 31 | + ) |
| 32 | + |
| 33 | + for image_url in IMAGE_LIST: |
| 34 | + print("\nEvaluate image {}".format(image_url)) |
| 35 | + |
| 36 | + print("\nEvaluate for adult and racy content.") |
| 37 | + evaluation = client.image_moderation.evaluate_url_input( |
| 38 | + "application/json", |
| 39 | + data_representation="URL", |
| 40 | + value=image_url, |
| 41 | + cache_image=True, |
| 42 | + ) |
| 43 | + assert isinstance(evaluation, Evaluate) |
| 44 | + pprint(evaluation.as_dict()) |
| 45 | + |
| 46 | + print("\nDetect and extract text.") |
| 47 | + evaluation = client.image_moderation.ocr_url_input( |
| 48 | + "eng", |
| 49 | + "application/json", |
| 50 | + data_representation="URL", |
| 51 | + value=image_url, |
| 52 | + cache_image=True, |
| 53 | + ) |
| 54 | + assert isinstance(evaluation, OCR) |
| 55 | + pprint(evaluation.as_dict()) |
| 56 | + |
| 57 | + print("\nDetect faces.") |
| 58 | + evaluation = client.image_moderation.find_faces_url_input( |
| 59 | + "application/json", |
| 60 | + data_representation="URL", |
| 61 | + value=image_url, |
| 62 | + cache_image=True, |
| 63 | + ) |
| 64 | + assert isinstance(evaluation, FoundFaces) |
| 65 | + pprint(evaluation.as_dict()) |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + import sys, os.path |
| 69 | + sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) |
| 70 | + from tools import execute_samples |
| 71 | + execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) |
0 commit comments