import os
from perceptron import Client, video
client = Client(api_key=os.environ["PERCEPTRON_API_KEY"])
video_url = (
"https://raw.githubusercontent.com/perceptron-ai-inc/perceptron/"
"main/cookbook/_shared/assets/capabilities/video-clipping/mj_shot_short.mp4"
)
def locate_event(question, include_soundtrack=False):
response = client.chat.completions.create(
model="perceptron-mk1.5",
messages=[{
"role": "user",
"content": [
video(video_url),
{"type": "text", "text": (
question + " Use clip annotations with asset_idx=0 and "
"timestamps in explicit seconds. If the requested event "
"is absent or uncertain, say so instead of inventing a timestamp."
)},
],
}],
reasoning_effort="high",
max_completion_tokens=2048,
vision_config={
"annotation_format": "clip",
"enable_audio_in_video": include_soundtrack,
},
)
choice = response.choices[0]
if choice.finish_reason != "stop":
raise RuntimeError(f"Incomplete clipping answer: {choice.finish_reason}")
return choice.message.content or ""
print("Moment:")
print(locate_event(
"Find the moment the ball passes through the hoop. "
"Return a single timestamp for that event."
))
print("Interval:")
print(locate_event(
"Find the shot-attempt interval, from the player's shooting motion "
"through the ball passing through the hoop. Return a start and end time."
))