Skip to content

Commit

Permalink
Merge pull request #19 from YaleDHLab/validate-inputs
Browse files Browse the repository at this point in the history
validate user inputs before processing
  • Loading branch information
duhaime committed Apr 12, 2018
2 parents e370670 + cc74b55 commit af6bec0
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions utils/process_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,37 @@ def __init__(self, image_dir, output_dir):
self.rewrite_image_thumbs = False
self.rewrite_image_vectors = False
self.rewrite_atlas_files = True
self.validate_inputs()
self.create_output_dirs()
self.create_image_thumbs()
self.create_image_vectors()
self.load_image_vectors()
self.write_json()
self.create_atlas_files()

def validate_inputs(self):
'''
Make sure the inputs are valid, and warn users if they're not
'''
# ensure the user provided enough input images
if len(self.image_files) < self.n_clusters:
print('Please provide >= ' + str(self.n_clusters) + ' images')
sys.exit()

# test whether each input image can be processed
invalid_files = []
for i in self.image_files:
try:
cmd = 'identify ' + i
response = subprocess.check_output(cmd, shell=True)
except:
invalid_files.append(i)
if invalid_files:
message = '\n\nThe following files could not be processed:'
message += '\n ! ' + '\n ! '.join(invalid_files) + '\n'
message += 'Please remove these files and reprocess your images.'
print(message)
sys.exit()

def create_output_dirs(self):
'''
Expand Down Expand Up @@ -148,9 +172,9 @@ def progress(count, block_size, total_size):
percent = float(count * block_size) / float(total_size) * 100.0
sys.stdout.write('\r>> Downloading %s %.1f%%' % (filename, percent))
sys.stdout.flush()
filepath, _ = urllib.request.urlretrieve(inception_path, filepath, progress)
filepath, _ = urllib.request.urlretrieve(inception_path, filepath, progress)
tarfile.open(filepath, 'r:gz').extractall(dest_directory)


def create_tf_graph(self):
'''
Expand Down Expand Up @@ -299,13 +323,13 @@ def write_atlas_files(self, thumb_size, image_thumbs):
# build a directory for the atlas files
out_dir = join(self.output_dir, 'atlas_files', str(thumb_size) + 'px')
self.ensure_dir_exists(out_dir)

# specify number of columns in a 2048 x 2048px texture
atlas_cols = 2048/thumb_size

# subdivide the image thumbs into groups
atlas_image_groups = self.subdivide(image_thumbs, atlas_cols**2)

# generate a directory for images at this size if it doesn't exist
for idx, atlas_images in enumerate(atlas_image_groups):
print(' * creating atlas', idx, 'at size', thumb_size)
Expand Down

0 comments on commit af6bec0

Please sign in to comment.