简体   繁体   中英

How to combine 3 operations in 1

I am assembling a dataset using Python. After I run a series of shell commands to download, untar and remove the tar file, I'm using these commands to assemble the dataset:

!rm -rf sample_data # This is just a default directory in colab that includes some starter datasets. I remove it to maintain disk space.
!rm -rf food-101
!wget https://s3.amazonaws.com/fast-ai-imageclas/food-101.tgz 
!tar -xvf food-101.tgz
!rm -rf food-101.tgz food-101/h5/
!mkdir food-101/meta food-101/images/train food-101/images/test
!mv food-101/*.txt food-101/*.json food-101/meta/

# Moves all test images to the food-101/images/test directory and renames them
with open('food-101/meta/test.txt') as test_file:
  for line in test_file:
    name_of_folder = line.split('/')[0]
    name_of_file = line.split('/')[1].rstrip()
    Path('food-101/images/' + name_of_folder + '/' + name_of_file + '.jpg').rename('food-101/images/test/' + name_of_folder + '_' + name_of_file + '.jpg')

# Moves all training images to the food-101/images/train directory and renames them
with open('food-101/meta/train.txt') as train_file:
  for line in train_file:
    name_of_folder = line.split('/')[0]
    name_of_file = line.split('/')[1].rstrip()
    Path('food-101/images/' + name_of_folder + '/' + name_of_file + '.jpg').rename('food-101/images/train/' + name_of_folder + '_' + name_of_file + '.jpg')

# Removes empty directories inside Food-101/images
with open('food-101/meta/train.txt') as train_file:
  for folder in train_file:
    name_of_folder = folder.split('/')[0]
    if os.path.exists('food-101/images/' + name_of_folder)

So basically the dataset comes with every image (train and test) belonging to a folder dedicated to its class. These images are not labeled and their labels are in train.txt and test.txt respectively. These txt files contain the list for which image belongs to which folder (train/test). I'm basically going through each txt file and moving the image to its corresponding file as I am renaming them.

This is basically repeating the same code. I tried combining them into one with open() block but just couldn't figure out how to do it.

Could this be what you're looking for?

with open('food-101/meta/test.txt') as test_file:

  for line in test_file:
    name_of_folder = line.split('/')[0]
    name_of_file = line.split('/')[1].rstrip()
    Path(f'food-101/images/{name_of_folder}/{name_of_file}.jpg').rename(f'food-101/images/test/{name_of_folder}_{name_of_file}.jpg')

  for line in test_file:
    name_of_folder = line.split('/')[0]
    name_of_file = line.split('/')[1].rstrip()
    Path(f'food-101/images/{name_of_folder}/{name_of_file}.jpg').rename(f'food-101/images/train/{name_of_folder}_{name_of_file}.jpg')

  for folder in test_file:
    name_of_folder = folder.split('/')[0]
    if os.path.exists(f'food-101/images/{name_of_folder}')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM