def main(): # Parse arguments parser = argparse.ArgumentParser(description='Classify images in a directory as good or bad.') parser.add_argument('images-path', nargs='?', default='.', help='Path to directory of input images.') args = vars(parser.parse_args()) # Select directory global if_path if_path = args['images-path'] # Parse in features information global feats cwd = os.getcwd() os.chdir('../suitability/') feats = Feats(if_path) print('- Loaded features for %d files' % len(feats)) # Initialise classifier global classifier classifier = Classifier() os.chdir(cwd) # Get list of files global files valid_suffices = ('png', 'jpg', 'jpeg') files = [f for f in os.listdir(if_path) if \ os.path.isfile('%s/%s' % (if_path, f)) and \ f.lower().endswith(valid_suffices)] if len(files) == 0: print('No images to read in: %s' % if_path) cleanup() # Classify each image and keep suitable ones global suitability_scores suitable = [] suitability_scores = [] for fname in files: score = classifier.getScore(feats[fname]) if score > 0: suitable.append(fname) suitability_scores.append(score) print('- Classified images to retain list of suitable images only') print('- %d/%d suitable images left' % (len(suitable), len(files))) files = suitable # Create list of last shown times global last_times global selected last_times = [time.time()]*len(files) selected = [False]*len(files) # Main loop global num num = 0 find_next_image() show_next_image() signal.signal(signal.SIGINT, cleanup) while True: # Wait for key for 50ms k = cv.waitKey(50) # If no more images left to show, exit n_left = len([x for x in selected if not x]) if n_left == 0: break # Ignore if no key captured if k is -1: continue # If Esc, break loop elif k == 27: break # If any other button, show previous image else: next_image() print '%d images left to show' % n_left cleanup()
def get_images(n): # Parse in features information global file_to_feat cwd = os.getcwd() os.chdir('../suitability/') feats = Feats(if_path) print('- Loaded features for %d files' % len(feats)) # Initialise classifier global classifier classifier = Classifier() os.chdir(cwd) # Get list of files global files valid_suffices = ('png', 'jpg', 'jpeg') files = [f for f in os.listdir(if_path) if \ os.path.isfile('%s/%s' % (if_path, f)) and \ f.lower().endswith(valid_suffices)] if len(files) == 0: print('No images to read in: %s' % if_path) cleanup() random.shuffle(files) # Classify each image and keep suitable ones suitable = [False]*len(files) scores = [0]*len(files) for i, fpath in enumerate(files): if classifier.predictFeats(feats[fpath]) == 1: suitable[i] = True scores[i] = classifier.getScore(feats[fpath]) # Show 10 top suitable images N = 7 _, topfs = zip(*sorted(zip(scores, files), reverse=True)[:N]) iH = 150 iW = 200 H = iH W = iW*N out = np.ndarray((H, W, 3), dtype=np.uint8) out.fill(255) for i, fpath in enumerate(topfs): I = imread_rotated('%s/%s' % (if_path, fpath)) h, w, _ = I.shape scale = min(float(iW) / w, float(iH) / h) I = cv.resize(I, None, fx=scale, fy=scale) h, w, _ = I.shape x0 = iW*i + iW/2 - w/2 y0 = iH/2 - h/2 out[y0:y0+h, x0:x0+w,] = I cv.imwrite('top_suitable.jpg', out) # Get up to n entries #pairs = sorted(zip(suitable, files)) #good = [(files[i], suitable[i]) for i in range(len(files)) if suitable[i]] #bad = [(files[i], suitable[i]) for i in range(len(files)) if not suitable[i]] #random.shuffle(good) #random.shuffle(bad) #n_good = int(n * 0.35) #n_bad = n - n_good #pairs = bad[:n_bad] + good[:n_good] #random.shuffle(pairs) #files, suitable = zip(*pairs) files = files[:n] suitable = suitable[:n] files = ['%s/%s' % (if_path, fname) for fname in files] # Load images imgs = [None]*len(files) for i, fpath in enumerate(files): imgs[i] = imread_rotated(fpath) return files, imgs, suitable