frutiemax commited on
Commit
fcbbb8b
·
1 Parent(s): 65cb3ae

Add helper scripts

Browse files
Files changed (3) hide show
  1. convert_images.py +40 -0
  2. create_dataset.py +4 -0
  3. create_json.py +31 -0
convert_images.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import os
3
+ from pathlib import Path
4
+ from PIL import Image
5
+ import numpy as np
6
+
7
+ input_folder = Path("./original_images/")
8
+ output_folder = Path("./data/train/")
9
+ output_folder.mkdir(parents=True, exist_ok=True)
10
+ folders = os.listdir(input_folder)
11
+ for folder in folders:
12
+ folder_path = input_folder.joinpath(folder)
13
+ images = os.listdir(folder_path)
14
+ for image in images:
15
+ output = output_folder.joinpath(f'{folder}')
16
+ output.mkdir(parents=True, exist_ok=True)
17
+
18
+ output = output.joinpath(f'{folder}_{image}')
19
+ output = str(output.absolute())
20
+
21
+ input = folder_path.joinpath(image)
22
+ input = str(input.absolute())
23
+
24
+ if os.path.isfile(input) == False or '.json' in image:
25
+ continue
26
+
27
+ image_input = Image.open(input)
28
+
29
+ background_color = (23, 35, 35, 255)
30
+ new_color = (0, 0, 0, 255)
31
+ data = np.array(image_input)
32
+ data[(data == background_color).all(axis=-1)] = new_color
33
+ image_input = Image.fromarray(data, 'RGBA')
34
+
35
+ new_image = Image.new('RGB', image_input.size)
36
+ new_image.paste(image_input, mask=image_input.split()[3])
37
+
38
+ bbox = new_image.getbbox()
39
+ new_image = new_image.crop(bbox)
40
+ new_image.save(output)
create_dataset.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from datasets import load_dataset
2
+
3
+ dataset = load_dataset('.', data_dir='data', split='train')
4
+ dataset.push_to_hub('frutiemax/rct_dataset')
create_json.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas
3
+ from pathlib import Path
4
+ import string
5
+ import shutil
6
+
7
+ # read all the folders in data/train
8
+ data_folder = Path("./data/train")
9
+ folders = os.listdir(data_folder)
10
+
11
+ entries = []
12
+
13
+ image_index = 0
14
+ for folder in folders:
15
+ images_path = data_folder.joinpath(folder)
16
+ images = os.listdir(images_path)
17
+
18
+ view = 0
19
+ for image in images:
20
+ object_description = folder.replace('_', ' ')
21
+ entry = {'file_name' : image, 'object_type' : 'small_scenery', 'object_description' : object_description, 'view': view, 'color1' : 'none', 'color2' : 'none', 'color3' : 'none'}
22
+ entries.append(entry)
23
+ view = view + 1
24
+
25
+ #put the entries into the metadata.csv
26
+ dataframe = pandas.DataFrame(entries)
27
+
28
+ # dont overwrite the old metadata.csv
29
+ if os.path.exists('metadata.csv'):
30
+ shutil.copyfile('metadata.csv', 'metadata_backup.csv')
31
+ dataframe.to_csv('metadata.csv')