Search code examples
python-3.xgoogle-colaboratory

Filter same tag images and save another folder at google drive using google colaboratory


I have downloaded dataset and it includes images of flood, fire, volcano and etc. Above those I want to filter flood tag images and save them in another folder. My all images include a folder in a google drive. How to do it?

Dataset Link: https://drive.google.com/drive/folders/1lGD1LSnPnyoCOLfPXiZ_Y4zWgyh93ltn?usp=sharing


Solution

  • The dataset you posted has 6 types of natural disasters, hurricane, volcano, earthquake, flooding, tsunami, and wildfire. Each image file name contains one of these words, so you can easily filter images that are relevant to floods.

    from google.colab import drive 
    import os
    
    i_path = "/content/drive/My Drive/images"
    flood_dir = "/content/drive/My Drive/flood_images"
    
    drive.mount('/content/drive')
    os.chdir(i_path)  
    
    for file_name in os.listdir():
      file_path = f"{i_path}/{file_name}"
      if "flooding" in file_name:
        s_p = os.path.join(i_path, file_path)
        d_p = os.path.join(flood_dir)
        !mv "$s_p" "$d_p"