def __init__(self, model_path=None): if model_path == None: return self.sess = tf.Session() self.detect = face.Detection() # make a detector (using triple CNN) with self.sess.as_default(): facenet.load_model(model_path)
if not os.path.exists(flat_dir): os.makedirs(flat_dir) pics = [f for f in listdir(screenshot_dir) if isfile(join(screenshot_dir, f))] for i, pic in enumerate(pics): file = os.path.join(screenshot_dir, pic) try: curr_img = imread(file) except: # failed to read image # sometimes this happens because the correct file type is not returned print('Image Read Failed.') continue detector = face.Detection() detector.minsize = 10 detector.face_crop_margin = 16 try: faces = detector.find_faces(curr_img) except: # Something in detector failed. Move on to next image print('Skipped', pic, ' - Something went wrong finding faces') if len(faces) == 0: print('Skipped', pic, ' - No faces found') else: copy(file, os.path.join(raw_dir, pic)) for j, curr_face in enumerate(faces): imwrite(os.path.join( face_dir, '{}_{}-{}.jpg'.format(pic.split('.')[0], i + 1, j)),
def __init__(self, model_path): self.detect = face.Detection() self.encoder = Encoder(model_path)
import matplotlib.patches as patches test_path = '../test_data/FaceDetect/WiderDataset' img_filenames = ['1', '3', '4', '6', '43', '48'] test_img_vec = list() fig_pre = plt.figure() figlab = 'abcdef' detectors = list() # List of detectors, one per image faces_list = list() # List of facelists from each image for i, filename in enumerate(img_filenames): test_img_vec.append(imageio.imread(test_path + '/' + filename + '.jpg')) fig_pre.add_subplot(2, 3, i + 1) plt.imshow(test_img_vec[i]) plt.xlabel('({})'.format(figlab[i])) # Create face detector detectors.append(face.Detection()) detectors[-1].minsize = 10 detectors[-1].face_crop_margin = 16 faces_list.append(detectors[-1].find_faces(test_img_vec[i])) plt.tight_layout() plt.suptitle('Example Images') plt.subplots_adjust(top=0.90) plt.savefig('example_input_combined.png') plt.show() # Separate figure for face bound box and points fig, ax = plt.subplots(2, 3) plt.suptitle('Example Images with Bounding Boxes and Points') for i, test_img in enumerate(test_img_vec): faces = faces_list[i]
def main(movie_name, val, num_actors, num_images): # Get IMDB instance ia = IMDb() # Get movie search #movie_name = input('Search for the movie the database will be based on. (enter title): ') with NoStdStreams(): results = ia.search_movie(movie_name) # Print the movie title search results print('\nSearch Results:\n') print('Title, Year, Kind') print('-----------------') for i, result in enumerate(results): if i > 9: break try: print( str(i + 1) + '. ' + result.data['title'] + ', (' + str(result.data['year']) + '), ' + result.data['kind']) except: print('Skipped ' + result.data['title'] + ' because of an issue.') #val = input('\nEnter a title number (non-matching characters will exit program): ') # Select a title try: val = int(val) if val in range(1, min(len(results), 10)): movie = ia.get_movie(results[val - 1].movieID) else: print('Please enter a value between 1 and {}'.format( min(len(results), 10))) raise Exception except: quit('Quit signal given. Program Exited') # # Select number of actors from the selected title # print('\nTop 30 Billed Actors:\n') print('Cast, Character') print('---------------') for i, castmember in enumerate(movie['cast']): if i > 29: break print(str(i + 1) + '. ' + castmember['name'] + ', ' + castmember.notes) #num_actors = input('\nHow many characters do you want to include? (Taken from top to bottom): ') try: num_actors = int(num_actors) if num_actors not in range(1, min(len(movie['cast']), 30)): print('Please enter a value between 1 and {}'.format( min(movie['cast'], 30))) raise Exception except: quit('Quit signal given. Program Exited') # Select number of images to download #num_images = input('\nHow many images per actors? (1-100): ') try: num_images = int(num_images) if num_images not in range(1, 100): print('Please enter a value between 1 and 100') raise Exception except: quit('Quit signal given. Program Exited') # # Start Building the database # # Connor adjusted the path for his computer !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! output_root = '/home/connor/Documents/Stanford_Classes/CS230/CS230DeepActor/train_data/FaceID/' movie_dir = output_root + '_'.join(movie['title'].split() + [str(movie['year'])] + [str(movie['kind'])]) + '/' downloads_dir = movie_dir + 'downloaded/' raw_dir = movie_dir + 'raw/' face_dir = movie_dir + 'face/' flat_dir = movie_dir + 'flattened/' for i in range(num_actors): character = movie['cast'][i].notes.split('/')[0] char_actor = '--'.join([ '_'.join(character.split()), '_'.join( movie['cast'][i]['name'].split()) ]) + '/' # Create directories for other outputs if not os.path.exists(raw_dir + char_actor): os.makedirs(raw_dir + char_actor) if not os.path.exists(face_dir + char_actor): os.makedirs(face_dir + char_actor) if not os.path.exists(flat_dir + char_actor): os.makedirs(flat_dir + char_actor) if not os.path.exists(downloads_dir): os.makedirs(downloads_dir) query = ' '.join([ character, movie['title'], movie['cast'][i]['name'], str(movie['year']) ]) query_dir = downloads_dir + '\"{}\"'.format(query) print('\nDownloading {} images for {}'.format( str(num_images), movie['cast'][i]['name'])) print(os.getcwd()) subprocess.check_output([ 'googleimagesdownload', '-k', '\"{}\"'.format(query), '-l', str(num_images), '-o', downloads_dir, '-f', 'jpg', '-s', 'medium' ]) print('Downloads finished') print('Processing images and finding faces') # Get a list of the pics for the current actor noQuote_query_dir = downloads_dir + query if not os.path.exists(query_dir): query_dir = noQuote_query_dir # check if the directory has quotes or not pics = [f for f in listdir(query_dir) if isfile(join(query_dir, f)) ] # THere was an issue with using " " for the file for j, pic in enumerate(pics): file = query_dir + '/' + pic # Changed to noQuote query dir try: curr_img = imread(file) except: # failed to read image # sometimes this happens because the correct file type is not returned print('Image Read Failed.') continue detector = face.Detection() detector.minsize = 10 detector.face_crop_margin = 16 faces = detector.find_faces(curr_img) if len(faces) > 1: print('Skipped', pic, ' - More than one face') elif len(faces) == 0: print('Skipped', pic, ' - No faces found') else: copy(file, raw_dir + char_actor + pic) imwrite(face_dir + char_actor + '_'.join(character.split()) + '{}.jpg'.format(j + 1), faces[0].image, format='jpg') with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=FutureWarning) print('Flattening faces') # Flatten faces pwd = os.getcwd() align_path = '/home/connor/Documents/CS230/CS230DeepActor/facenet/src/align/' try: os.chdir(align_path) subprocess.check_output( ['python', 'align_dataset_mtcnn.py', face_dir, flat_dir]) os.chdir(pwd) except: os.chdir(pwd) print('Database creation complete!')