| from sentence_transformers import SentenceTransformer, util |
|
|
| import json |
| from datetime import datetime |
| |
| def find_next_step(data, current_context): |
| try: |
| |
| |
| |
| |
| |
| total_videos = len(data) |
| total_scenes = 0 |
| max_score=0.0 |
| step={ |
| "path" :"", |
| "start":"", |
| "end": "", |
| } |
| |
| print(f"Processing {total_videos} videos...\n") |
| for index, video in enumerate(data, 1): |
| video_type = video.get('type', 'Unknown') |
| video_path = video.get('path', 'No path provided') |
| scenes = video.get('scenes', []) |
| scene_count = len(scenes) |
| total_scenes += scene_count |
| |
|
|
| |
| for scene_index, scene in enumerate(scenes, 1): |
| timestamp = scene.get('timestamp', 'No timestamp') |
| description = scene.get('description', 'No description') |
|
|
| time_obj = datetime.strptime(timestamp, "%M:%S.%f") |
| start = time_obj.minute * 60 + time_obj.second + time_obj.microsecond / 1_000_000 |
| score=compare_phrases(description,current_context) |
|
|
| if score > max_score: |
| if scene_index+1<len(scenes): |
| next_scene=scenes[scene_index+1] |
| next_timestamp=next_scene.get('timestamp', 'No timestamp') |
| time_obj = datetime.strptime(next_timestamp, "%M:%S.%f") |
|
|
| stop = time_obj.minute * 60 + time_obj.second + time_obj.microsecond / 1_000_000 |
| step={ |
| "path" :video_path, |
| "start":start, |
| "end": stop, |
| "description":description |
| } |
| else: |
| step={ |
| "path" :video_path, |
| "start":start, |
| "end": "", |
| "description":description |
| } |
|
|
| max_score=score |
|
|
| if "Error" in description: |
| print("[Note: This scene contains an error]") |
| |
| |
| print(f"Summary:") |
| print(f" Total Videos: {total_videos}") |
| print(f" Total Scenes: {total_scenes}") |
| |
| except FileNotFoundError: |
| print(f"Error: File '{file_path}' not found.") |
| except json.JSONDecodeError: |
| print("Error: Invalid JSON format.") |
| except Exception as e: |
| print(f"Error: An unexpected error occurred: {str(e)}") |
|
|
| return step |
|
|
|
|
|
|
| def compare_phrases(phrase1,phrase2="Passing around a dj mixing a song"): |
| model = SentenceTransformer('all-MiniLM-L6-v2',local_files_only=True) |
| |
| embedding1 = model.encode(phrase1, convert_to_tensor=True) |
| embedding2 = model.encode(phrase2, convert_to_tensor=True) |
|
|
| |
| similarity_score = util.cos_sim(embedding1, embedding2)[0][0] |
|
|
|
|
| |
| |
| return similarity_score |
|
|
|
|
|
|
| def find_all_steps(data, context): |
|
|
| context = context.replace("\n", "").lower() |
| all_actions=context.split(".") |
| steps=[] |
| for action in all_actions: |
| step=find_next_step(data,action) |
| steps.append(step) |
| return steps |
|
|
|
|
| |
|
|
|
|
| if __name__ == "__main__": |
| file_path = '/Users/georgia.bucea/products/ShortsAI/all_files_metadata.json' |
|
|
| context = """I am walking around San Francisco and passing around the big white building. I am walking around a music band and DJ mixing a song. I am showing a demo on the phone, I am explaining the demo on the mobile phone""" |
| |
| with open(file_path, 'r') as file: |
| data = json.load(file) |
|
|
| steps=find_all_steps(data,context) |
| print(steps) |
|
|
| |
|
|
|
|