def get_test_images(test_file, test_folder): print("Getting test images...") if not isdir(test_folder): os.mkdir(test_folder) pic_to_faces = OrderedDict() # Read in test_file contents with open(test_file, "r") as csvfile: test_items = DictReader( csvfile, fieldnames=['url', 'file_name', 'num_faces', 'md5_hash']) for row in test_items: test_file_name = join(test_folder, row['file_name']) try: # File has already been downloaded if isfile(test_file_name) and row['md5_hash'] == file_digest( test_file_name): pic_to_faces[test_file_name] = int(row['num_faces']) #print("Verified {0}".format(test_file_name)) continue # Download file and test hash else: if py3: urlretrieve(row['url'], test_file_name) else: response = urlopen(row['url']) with open(test_file_name, "wb") as local_file: local_file.write(response.read()) if row['md5_hash'] != file_digest(test_file_name): os.remove(test_file_name) print( "Warning - {0} did not match expected hash".format( test_file_name)) else: pic_to_faces[test_file_name] = int(row['num_faces']) #print("Downloaded {0}".format(test_file_name)) except: print("Error - ", exc_info()) continue print("{0} test images verified\n".format(len(pic_to_faces))) return pic_to_faces
def process_image(image_file, margin, min_size, threshold, scale_factor, reduceby, pnet, rnet, onet, verbose=False): filename = image_file.split('/')[-1] file_content_hash = file_digest(image_file) image = cv2.imread(image_file) frame_number = -1 # Find bounding boxes for face chips in this image face_locations, num_detections = identify_chips(image, frame_number, margin, min_size, threshold, scale_factor, reduceby, pnet, rnet, onet) # Only save pickle if faces were detected if num_detections > 0: results = (filename, file_content_hash, [face_locations]) write_out_pickle(filename, results, "/bboxes", "mtcnn", "bboxes") if verbose: print("{0} face detections in {1}".format(num_detections, filename))
def process_image(image_file, reduceby, prob_thresh, nms_thresh, use_gpu, verbose=False): filename = image_file.split('/')[-1] file_content_hash = file_digest(image_file) image = cv2.imread(image_file) frame_number = -1 # Find bounding boxes for face chips in this image face_locations, num_detections = identify_chips(image, frame_number, reduceby, prob_thresh, nms_thresh, use_gpu) # Only save pickle if faces were detected if num_detections > 0: results = (filename, file_content_hash, [face_locations]) write_out_pickle(filename, results, "/bboxes", "tinyface", "bboxes") if verbose: print("{0} face detections in {1}".format(num_detections, filename))
def process_video(image_file, threshold, nms, reduceby, net, every): frame_number = 0 num_detections = 0 filename = image_file.split('/')[-1] #camera = cv2.VideoCapture(image_file) #capture_length = int(camera.get(cv2.CAP_PROP_FRAME_COUNT)) capture_length = get_movie_length(image_file) progress = tqdm(total=capture_length) file_content_hash = file_digest(image_file) combined_face_locations = list() keep_going = True first = True while keep_going: if not first: if (every + frame_number) > capture_length: keep_going = False progress.close() break frame_number += every #camera.set(1, frame_number) progress.update(every) else: first = False #keep_going, image = camera.read() keep_going, image = get_frame_inefficient(image_file, frame_number) # only face detect every once in a while progress.set_description( 'Processing video: {0} detections: {1}'.format( filename[0:30] + "...", num_detections)) progress.refresh() # verify that there is a video frame to process if image is None: progress.refresh() progress.write('end of capture:IMG') progress.close() break if frame_number > capture_length: progress.refresh() progress.write('end of capture:Length') progress.close() break if not keep_going: progress.refresh() progress.write('end of capture:camera.read') progress.close() break # Find bounding boxes for face chips in this frame face_locations, detections = identify_chips(image, frame_number, threshold, nms, reduceby, net) if detections > 0: combined_face_locations += [face_locations] num_detections += detections # Only save pickle if faces were detected if num_detections > 0: results = (filename, file_content_hash, combined_face_locations) write_out_pickle(filename, results, "/bboxes", "frcnn", "bboxes")
def main(jitters=1, tolerance=0.6, chip_size=160, verbose=False): # Gather file names files = [item for item in glob.glob( '/bboxes/*') if not os.path.isdir(item)] if verbose: durations = list() kickoff = time() for f in files: print('Processing:', f.split('/')[-1]) filename, file_content_hash, bounding_boxes = pickle.load( open(f, "rb")) file_with_path = join('/media', filename) # Verify original file exists on disk and has same content hash if os.path.isfile(file_with_path) and file_digest( file_with_path) == file_content_hash: if len(bounding_boxes) > 0: # only allow verbose flag for images if verbose and bounding_boxes[0][0] == -1: start = time() extract_chips( filename, bounding_boxes, chip_size, file_content_hash, tolerance, jitters, verbose) duration = time() - start durations.append(duration) print("{0} seconds to process {1}\n".format( '%.3f' % duration, f.split('/')[-1])) else: extract_chips( filename, bounding_boxes, chip_size, file_content_hash, tolerance, jitters) else: print("There were no faces detected in {0}".format(f)) else: print( "\tWarning - {0} has bounding box file in /bboxes but was not found in /media.\n".format(filename)) sys.stdout.flush() sys.stderr.flush() if verbose and len(durations) > 0: average = sum(durations) / len(durations) print( "\nAverage elapsed time to vectorize faces in an image = {0}".format( '%.3f' % average)) print( "Total time to vectorize faces in {0} images = {1}".format( len(durations), '%.3f' % (time() - kickoff)))
def get_iou_images(iou_url, test_folder, num_test_images=20, seed=41): print("Downloading list of test images...") if not isdir(test_folder): os.mkdir(test_folder) pic_to_bbox = OrderedDict() # Go get the iou_url file iou_file_name = "../test_data/iou_test_file.txt" try: if py3: urlretrieve(iou_url, iou_file_name) else: response = urlopen(iou_url) with open(iou_file_name, "wb") as local_file: local_file.write(response.read()) except: print("Error retrieving {0} - {1}".format(iou_url, exc_info())) quit() # Read in iou_file_name contents with open(iou_file_name, "r") as csvfile: iou_items = DictReader( csvfile, fieldnames=['person', 'imagenum', 'url', 'rect', 'md5_hash'], delimiter='\t') marker = 0 candidate_list = list() for item in iou_items: # Skip first two lines of header info if item['md5_hash'] is None: continue candidate_list.append(item) # Sample for variety of faces random.seed(seed) candidate_list = random.sample(candidate_list, num_test_images * 20) print("Looking for previously downloaded test images...") pop_list = list() for i in range(len(candidate_list)): test_file_name = join( test_folder, candidate_list[i]['person'].replace(" ", "") + candidate_list[i]['imagenum'] + ".iou") if isfile(test_file_name) and candidate_list[i][ 'md5_hash'] == file_digest(test_file_name): bbox = [ int(coordinate) for coordinate in candidate_list[i]['rect'].split(",") ] pic_to_bbox[test_file_name] = bbox pop_list.append(i) # Remove already downloaded files from candidate_list for index_to_pop in pop_list: candidate_list.pop(index_to_pop) print("Downloading test images (slow)...") for row in candidate_list: test_file_name = join( test_folder, row['person'].replace(" ", "") + row['imagenum'] + ".iou") try: # File has already been downloaded #if isfile(test_file_name) and row['md5_hash'] == file_digest(test_file_name): # bbox = [int(coordinate) for coordinate in row['rect'].split(",")] # pic_to_bbox[test_file_name] = bbox #print("Verified {0}".format(test_file_name)) # continue # Download file and test hash #else: if py3: urlretrieve(row['url'], test_file_name) else: response = urlopen(row['url']) with open(test_file_name, "wb") as local_file: local_file.write(response.read()) if row['md5_hash'] != file_digest(test_file_name): os.remove(test_file_name) #print("Warning - {0} did not match expected hash".format(test_file_name)) else: bbox = [ int(coordinate) for coordinate in row['rect'].split(",") ] pic_to_bbox[test_file_name] = bbox #print("Downloaded {0}".format(test_file_name)) except: #print("Error - ", exc_info()) continue # Check is enough test images downloaded if len(pic_to_bbox) % 5 == 0 and len(pic_to_bbox) > marker: print("{0} of {1} test images downloaded and verified".format( len(pic_to_bbox), num_test_images)) marker = len(pic_to_bbox) if len(pic_to_bbox) == num_test_images: break #print("{0} test images verified\n".format(len(pic_to_bbox))) return pic_to_bbox
def main(model, dlibFacePredictor, use_gpu=False, tolerance=0.6, chip_size=96, verbose=False): print("Loading Facenet Model...") sys.stdout.flush() sys.stderr.flush() # Load the Facenet model if use_gpu: net = TorchNeuralNet(model, chip_size, cuda=True) else: net = TorchNeuralNet(model, chip_size, cuda=False) # Load dlib image aligner aligner = AlignDlib(dlibFacePredictor) # Gather file names files = [ item for item in glob.glob('/bboxes/*') if not os.path.isdir(item) ] if verbose: durations = list() kickoff = time() for f in files: print('Processing:', f.split('/')[-1]) filename, file_content_hash, bounding_boxes = pickle.load(open( f, "rb")) file_with_path = join('/media', filename) # Verify original file exists on disk and has same content hash if os.path.isfile(file_with_path) and file_digest( file_with_path) == file_content_hash: if len(bounding_boxes) > 0: # only allow verbose on images if verbose and bounding_boxes[0][0] == -1: start = time() extract_chips(filename, bounding_boxes, chip_size, file_content_hash, tolerance, net, aligner, verbose) duration = time() - start durations.append(duration) print("{0} seconds to process {1}\n".format( '%.3f' % duration, f.split('/')[-1])) else: extract_chips(filename, bounding_boxes, chip_size, file_content_hash, tolerance, net, aligner) else: print("There were no faces detected in {0}".format(f)) else: print( "\tWarning - {0} has bounding box file in /bboxes but was not found in /media.\n" .format(filename)) sys.stdout.flush() sys.stderr.flush() if verbose and len(durations) > 0: average = sum(durations) / len(durations) print("\nAverage elapsed time to vectorize faces in an image = {0}". format('%.3f' % average)) print("Total time to vectorize faces in {0} images = {1}".format( len(durations), '%.3f' % (time() - kickoff)))
def main(model, use_gpu=False, gpu_memory_fraction=0.8, tolerance=0.6, chip_size=160, verbose=False): if use_gpu: gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=gpu_memory_fraction) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) else: sess = tf.Session() # Load the model print("Loading Facenet Model...") sys.stdout.flush() sys.stderr.flush() facenet.load_model(model, sess) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name( "phase_train:0") # Gather files names files = [ item for item in glob.glob('/bboxes/*') if not os.path.isdir(item) ] if verbose: durations = list() kickoff = time() for f in files: print('Processing:', f.split('/')[-1]) filename, file_content_hash, bounding_boxes = pickle.load(open( f, "rb")) file_with_path = join('/media', filename) # Verify original file exists on disk and has same content hash if os.path.isfile(file_with_path) and file_digest( file_with_path) == file_content_hash: if len(bounding_boxes) > 0: if verbose and bounding_boxes[0][0] == -1: start = time() extract_chips(filename, bounding_boxes, chip_size, file_content_hash, tolerance, images_placeholder, phase_train_placeholder, embeddings, sess, verbose) duration = time() - start durations.append(duration) print("{0} seconds to process {1}\n".format( '%.3f' % duration, f.split('/')[-1])) else: extract_chips(filename, bounding_boxes, chip_size, file_content_hash, tolerance, images_placeholder, phase_train_placeholder, embeddings, sess) else: print("There were no faces detected in {0}".format(f)) else: print( "\tWarning - {0} has bounding box file in /bboxes but was not found in /media.\n" .format(filename)) sys.stdout.flush() sys.stderr.flush() sess.close() if verbose and len(durations) > 0: average = sum(durations) / len(durations) print("\nAverage elapsed time to vectorize faces in an image = {0}". format('%.3f' % average)) print("Total time to vectorize faces in {0} images = {1}".format( len(durations), '%.3f' % (time() - kickoff)))