|
| 1 | +from io import BytesIO |
| 2 | +import os.path |
| 3 | +from pprint import pprint |
| 4 | +from random import random |
| 5 | +import uuid |
| 6 | + |
| 7 | +from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient |
| 8 | +from azure.cognitiveservices.vision.contentmoderator.models import Content, Review, Frames, Screen |
| 9 | +from msrest.authentication import CognitiveServicesCredentials |
| 10 | + |
| 11 | +SUBSCRIPTION_KEY_ENV_NAME = "CONTENTMODERATOR_SUBSCRIPTION_KEY" |
| 12 | +CONTENTMODERATOR_LOCATION = os.environ.get("CONTENTMODERATOR_LOCATION", "westcentralus") |
| 13 | + |
| 14 | + |
| 15 | +def video_transcript_review(subscription_key): |
| 16 | + """VideoTranscriptReview. |
| 17 | +
|
| 18 | + This will create and publish a transcript review for video |
| 19 | + """ |
| 20 | + |
| 21 | + # The name of the team to assign the job to. |
| 22 | + # This must be the team name you used to create your Content Moderator account. You can |
| 23 | + # retrieve your team name from the Content Moderator web site. Your team name is the Id |
| 24 | + # associated with your subscription. |
| 25 | + team_name = "pysdktesting" |
| 26 | + |
| 27 | + # Create a review with the content pointing to a streaming endpoint (manifest) |
| 28 | + streamingcontent = "https://amssamples.streaming.mediaservices.windows.net/91492735-c523-432b-ba01-faba6c2206a2/AzureMediaServicesPromo.ism/manifest" |
| 29 | + |
| 30 | + transcript = b"""WEBVTT |
| 31 | +
|
| 32 | + 01:01.000 --> 02:02.000 |
| 33 | + First line with a crap word in a transcript. |
| 34 | +
|
| 35 | + 02:03.000 --> 02:25.000 |
| 36 | + This is another line in the transcript. |
| 37 | + """ |
| 38 | + |
| 39 | + client = ContentModeratorClient( |
| 40 | + CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com', |
| 41 | + CognitiveServicesCredentials(subscription_key) |
| 42 | + ) |
| 43 | + |
| 44 | + # |
| 45 | + # Create a video review |
| 46 | + # |
| 47 | + print("Create review for {}.\n".format(streamingcontent)) |
| 48 | + review_item = { |
| 49 | + "content": streamingcontent, # How to download the image |
| 50 | + "content_id": uuid.uuid4(), # Random id |
| 51 | + # Note: to create a published review, set the Status to "Pending". |
| 52 | + # However, you cannot add video frames or a transcript to a published review. |
| 53 | + "status": "Unpublished" |
| 54 | + } |
| 55 | + |
| 56 | + reviews = client.reviews.create_video_reviews( |
| 57 | + "application/json", |
| 58 | + team_name, |
| 59 | + [review_item] # As many review item as you need |
| 60 | + ) |
| 61 | + review_id = reviews[0] # Ordered list of string of review ID |
| 62 | + |
| 63 | + # |
| 64 | + # Add transcript |
| 65 | + # |
| 66 | + print("\nAdding transcript to the review {}".format(review_id)) |
| 67 | + client.reviews.add_video_transcript( |
| 68 | + team_name, |
| 69 | + review_id, |
| 70 | + BytesIO(transcript), # Can be a file descriptor, as long as its stream type |
| 71 | + ) |
| 72 | + |
| 73 | + # |
| 74 | + # Add transcript moderation result |
| 75 | + # |
| 76 | + print("\nAdding a transcript moderation result to the review with ID {}".format(review_id)) |
| 77 | + screen = client.text_moderation.screen_text( |
| 78 | + "eng", |
| 79 | + "text/plain", |
| 80 | + transcript, |
| 81 | + ) |
| 82 | + assert isinstance(screen, Screen) |
| 83 | + pprint(screen.as_dict()) |
| 84 | + |
| 85 | + # Build a terms list with index |
| 86 | + terms = [] |
| 87 | + for term in screen.terms: |
| 88 | + terms.append({"index": term.index, "term": term.term}) |
| 89 | + |
| 90 | + client.reviews.add_video_transcript_moderation_result( |
| 91 | + "application/json", |
| 92 | + team_name, |
| 93 | + review_id, |
| 94 | + [{ |
| 95 | + "timestamp": 0, |
| 96 | + "terms": terms |
| 97 | + }] |
| 98 | + ) |
| 99 | + |
| 100 | + # |
| 101 | + # Public review |
| 102 | + # |
| 103 | + client.reviews.publish_video_review(team_name, review_id) |
| 104 | + |
| 105 | + print("\nOpen your Content Moderator Dashboard and select Review > Video to see the review.") |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + import sys, os.path |
| 110 | + sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) |
| 111 | + from tools import execute_samples |
| 112 | + execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) |
0 commit comments