File size: 1,732 Bytes
6458bb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
from collections import defaultdict

# Function to process the input JSON into desired structure
# Now groups by video_id with a single caption per video and list of frame entries

def process_ego4d_annotations(json_file_path, output_json_path=None):
    # Load the JSON data from file
    with open(json_file_path, 'r') as f:
        data = json.load(f)

    # Temporary storage for grouping
    grouped = {}

    for entry in data:
        vid = entry['video_id']
        # initialize video entry if not exists
        if vid not in grouped:
            # store caption once per video
            grouped[vid] = {
                'caption': entry['caption'],
                'frames': []
            }
        # replace prefix "ego4d-data" with "ego4d" in paths
        frame_path = entry['image_path'].replace('robotic_videos', 'robotics').replace('videos',"frames")
        mask_path = entry['mask'].replace('robotic_videos', 'robotics')
        # append frame-level info
        grouped[vid]['frames'].append({
            'frame_path': frame_path,
            'mask_path': mask_path,
            'points': entry['clicked_points']
        })

    # Optionally save to output JSON file
    if output_json_path:
        with open(output_json_path, 'w') as outf:
            json.dump(grouped, outf, indent=2)

    return grouped


# Example usage
if __name__ == '__main__':
    input_json = '/share/data/drive_1/heakl/benchmark/annotated/robotics/robotics_annot.json'
    output_json = '/share/data/drive_1/heakl/benchmark/annotated/robotics/annotations.json'

    grouped_dict = process_ego4d_annotations(input_json, output_json)
    # Print nicely for verification
    import pprint
    pprint.pprint(grouped_dict)