Since the images are already labeled, better to use these labels to categorize the images. Inside the labels folder, there are JSON files containing image label data.
You can fetch image names and relevant disaster types from JSON files.
"metadata": {
"sensor": "GEOEYE01",
"provider_asset_type": "GEOEYE01",
"gsd": 2.0916247,
"capture_date": "2018-09-20T16:04:41.000Z",
"off_nadir_angle": 28.017313,
"pan_resolution": 0.52282465,
"sun_azimuth": 153.94543,
"sun_elevation": 53.722378,
"target_azimuth": 190.82309,
"disaster": "hurricane-florence",
"disaster_type": "flooding",
"catalog_id": "1050010012411600",
"original_width": 1024,
"original_height": 1024,
"width": 1024,
"height": 1024,
"id": "MjU0Njk0MQ.clApx1C8IcFymibsGi1JLu1eKhU",
"img_name": "hurricane-florence_00000324_post_disaster.png"
}
You can use the following code piece. It's written to copy an image to its relevant category folder (Ex: image with disaster_type 'fire' -> /categorized/fire/). Ultimately all the images will be categorized into separate folders.
from google.colab import drive
import os
import json
import shutil
drive.mount('/content/drive')
# change paths according to yours
main_folder_path = "/content/drive/My Drive/Backup/train"
images_folder_path = main_folder_path+"/images"
labels_folder_path = main_folder_path+"/labels"
categorized_folder_path = "/content/drive/My Drive/Backup/categorized"
os.chdir(main_folder_path)
for json_filename in os.listdir(labels_folder_path):
json_path = os.path.join(main_folder_path, "labels", json_filename)
f = open(json_path, 'r')
data = json.load(f)
disaster_type = data["metadata"]["disaster_type"]
img_name = data["metadata"]["img_name"]
print("disaster:", disaster_type, "image:", img_name)
f.close()
img_filepath = os.path.join(main_folder_path, "images", img_name)
category_folderpath = os.path.join(categorized_folder_path, disaster_type)
if os.path.exists(img_filepath):
if not os.path.exists(category_folderpath):
os.mkdir(category_folderpath)
shutil.copy(img_filepath, category_folderpath)