| import json |
| import pandas as pd |
| from PIL import Image |
| img_root = f'data/images/' |
| full_df = pd.read_csv('/content/via_project_7May2024_23h18m_csv.csv') |
| for img in full_df['filename'].unique(): |
| df = full_df[full_df['filename']==img] |
| obj_list = [] |
| for i in range(len(df)): |
| folder = 'data' |
| filename = df.iloc[i]['filename'] |
| name = json.loads(df['region_attributes'].iloc[i])['type'] |
| img = Image.open(f'{img_root}{filename}') |
| w, h = (str(img.size[0]), str(img.size[1])) |
| d = str(3) |
| Xmin = json.loads(df['region_shape_attributes'].iloc[i])['x'] |
| Ymin = json.loads(df['region_shape_attributes'].iloc[i])['y'] |
| Xmax = Xmin + json.loads(df['region_shape_attributes'].iloc[i])['width'] |
| Ymax = Ymin + json.loads(df['region_shape_attributes'].iloc[i])['height'] |
| obj_list.append({"name": name, "bndbox":{"xmin": Xmin, "ymin":Ymin, "xmax":Xmax, "ymax":Ymax }}) |
| sampleDict = { "folder": folder, "filename": filename, "size": {"width":w, "height":h, "depth":d}, "object": obj_list, |
| } |
| with open(f"data/annots/{filename.split('.')[0]}.json", 'w') as fp: |
| json.dump(sampleDict, fp) |