Skip to content

Commit 79f334f

Browse files
committed
Content Moderator Video Review
1 parent 7643db7 commit 79f334f

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import os.path
2+
from pprint import pprint
3+
from random import random
4+
import uuid
5+
6+
from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient
7+
from azure.cognitiveservices.vision.contentmoderator.models import Content, Review, Frames
8+
from msrest.authentication import CognitiveServicesCredentials
9+
10+
SUBSCRIPTION_KEY_ENV_NAME = "CONTENTMODERATOR_SUBSCRIPTION_KEY"
11+
CONTENTMODERATOR_LOCATION = os.environ.get("CONTENTMODERATOR_LOCATION", "westcentralus")
12+
13+
def video_review(subscription_key):
14+
"""VideoReview.
15+
16+
This will create and publish a review for video
17+
"""
18+
19+
# The name of the team to assign the job to.
20+
# This must be the team name you used to create your Content Moderator account. You can
21+
# retrieve your team name from the Content Moderator web site. Your team name is the Id
22+
# associated with your subscription.
23+
team_name = "pysdktesting"
24+
25+
# Create a review with the content pointing to a streaming endpoint (manifest)
26+
streamingcontent = "https://amssamples.streaming.mediaservices.windows.net/91492735-c523-432b-ba01-faba6c2206a2/AzureMediaServicesPromo.ism/manifest"
27+
28+
frame1_url = "https://blobthebuilder.blob.core.windows.net/sampleframes/ams-video-frame1-00-17.PNG"
29+
frame2_url = "https://blobthebuilder.blob.core.windows.net/sampleframes/ams-video-frame-2-01-04.PNG"
30+
frame3_url = "https://blobthebuilder.blob.core.windows.net/sampleframes/ams-video-frame-3-02-24.PNG"
31+
32+
33+
client = ContentModeratorClient(
34+
CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
35+
CognitiveServicesCredentials(subscription_key)
36+
)
37+
38+
#
39+
# Create a video review
40+
#
41+
print("Create review for {}.\n".format(streamingcontent))
42+
review_item = {
43+
"content": streamingcontent, # How to download the image
44+
"content_id": uuid.uuid4(), # Random id
45+
# Note: to create a published review, set the Status to "Pending".
46+
# However, you cannot add video frames or a transcript to a published review.
47+
"status": "Unpublished"
48+
}
49+
50+
reviews = client.reviews.create_video_reviews(
51+
"application/json",
52+
team_name,
53+
[review_item] # As many review item as you need
54+
)
55+
review_id = reviews[0] # Ordered list of string of review ID
56+
57+
#
58+
# Add the frames from 17, 64, and 144 seconds.
59+
#
60+
print("\nAdding frames to the review {}".format(review_id))
61+
def create_frames_to_add_to_reviews(timestamp_seconds, url):
62+
return {
63+
'timestamp': timestamp_seconds * 1000,
64+
'frame_image': url,
65+
'reviewer_result_tags': [
66+
# Note: All non str value will be casted using "str()"
67+
{'key': 'reviewRecommended', 'value': True},
68+
{'key': 'adultScore', 'value': random()},
69+
{'key': 'a', 'value': False},
70+
{'key': 'racyScore', 'value': random()},
71+
{'key': 'a', 'value': False},
72+
],
73+
'metadata': [
74+
# Note: All non str value will be casted using "str()"
75+
{'key': 'tag1', 'value': 'tag1'},
76+
]
77+
}
78+
79+
client.reviews.add_video_frame_url(
80+
"application/json",
81+
team_name,
82+
review_id,
83+
[
84+
create_frames_to_add_to_reviews(17, frame1_url),
85+
create_frames_to_add_to_reviews(64, frame2_url),
86+
create_frames_to_add_to_reviews(144, frame3_url)
87+
]
88+
)
89+
90+
#
91+
# Get frames
92+
#
93+
print("\nGetting frames for the review with ID {}".format(review_id))
94+
frames = client.reviews.get_video_frames(team_name, review_id, start_seed=0, no_of_records=1000)
95+
assert isinstance(frames, Frames)
96+
pprint(frames.as_dict())
97+
98+
#
99+
# Get reviews details
100+
#
101+
print("\nGetting review details for the review with ID {}".format(review_id))
102+
review_details = client.reviews.get_review(team_name, review_id)
103+
pprint(review_details.as_dict())
104+
105+
#
106+
# Public review
107+
#
108+
client.reviews.publish_video_review(team_name, review_id)
109+
110+
print("\nOpen your Content Moderator Dashboard and select Review > Video to see the review.")
111+
112+
113+
if __name__ == "__main__":
114+
import sys, os.path
115+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
116+
from tools import execute_samples
117+
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

0 commit comments

Comments
 (0)