Create annots_json_creation_from_csv.py
Browse files
annots_json_creation_from_csv.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from PIL import Image
|
| 4 |
+
img_root = f'data/images/'
|
| 5 |
+
full_df = pd.read_csv('/content/via_project_7May2024_23h18m_csv.csv')
|
| 6 |
+
for img in full_df['filename'].unique():
|
| 7 |
+
df = full_df[full_df['filename']==img]
|
| 8 |
+
obj_list = []
|
| 9 |
+
for i in range(len(df)):
|
| 10 |
+
folder = 'data'
|
| 11 |
+
filename = df.iloc[i]['filename']
|
| 12 |
+
name = json.loads(df['region_attributes'].iloc[i])['type']
|
| 13 |
+
img = Image.open(f'{img_root}{filename}')
|
| 14 |
+
w, h = (str(img.size[0]), str(img.size[1]))
|
| 15 |
+
d = str(3)
|
| 16 |
+
Xmin = json.loads(df['region_shape_attributes'].iloc[i])['x']
|
| 17 |
+
Ymin = json.loads(df['region_shape_attributes'].iloc[i])['y']
|
| 18 |
+
Xmax = Xmin + json.loads(df['region_shape_attributes'].iloc[i])['width']
|
| 19 |
+
Ymax = Ymin + json.loads(df['region_shape_attributes'].iloc[i])['height']
|
| 20 |
+
obj_list.append({"name": name, "bndbox":{"xmin": Xmin, "ymin":Ymin, "xmax":Xmax, "ymax":Ymax }})
|
| 21 |
+
sampleDict = { "folder": folder, "filename": filename, "size": {"width":w, "height":h, "depth":d}, "object": obj_list,
|
| 22 |
+
}
|
| 23 |
+
with open(f"data/annots/{filename.split('.')[0]}.json", 'w') as fp:
|
| 24 |
+
json.dump(sampleDict, fp)
|