def main(): """Main method""" from xbob.db.replay import Database from .. import utils protocols = [k.name for k in Database().protocols()] parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('inputdir', metavar='DIR', type=str, help='Base directory containing the scores to be loaded and merged') parser.add_argument('outputdir', metavar='DIR', type=str, help='Base output directory for every file created by this procedure') parser.add_argument('-p', '--protocol', metavar='PROTOCOL', type=str, default='grandtest', choices=protocols, dest="protocol", help="The protocol type may be specified to subselect a smaller number of files to operate on (one of '%s'; defaults to '%%(default)s')" % '|'.join(sorted(protocols))) supports = ('fixed', 'hand', 'hand+fixed') parser.add_argument('-s', '--support', metavar='SUPPORT', type=str, default='hand+fixed', dest='support', choices=supports, help="If you would like to select a specific support to be used, use this option (one of '%s'; defaults to '%%(default)s')" % '|'.join(sorted(supports))) parser.add_argument('-S', '--skip-frames', metavar='INT', type=int, default=10, dest='skip', help="Number of frames to skip once an eye-blink has been detected (defaults to %(default)s)") parser.add_argument('-T', '--threshold-ratio', metavar='FLOAT', type=float, default=3.0, dest='thres_ratio', help="How many standard deviations to use for counting positive blink picks to %(default)s)") parser.add_argument('-v', '--verbose', action='store_true', dest='verbose', default=False, help='Increases this script verbosity') args = parser.parse_args() if not os.path.exists(args.inputdir): parser.error("input directory `%s' does not exist" % args.inputdir) if args.support == 'hand+fixed': args.support = ('hand', 'fixed') if not os.path.exists(args.outputdir): if args.verbose: print "Creating output directory %s..." % args.outputdir os.makedirs(args.outputdir) db = Database() objs = db.objects(protocol=args.protocol, support=args.support, cls=('real', 'attack', 'enroll')) counter = 0 for obj in objs: counter += 1 arr = obj.load(args.inputdir, '.hdf5') nb = utils.count_blinks(arr, args.thres_ratio, args.skip) if args.verbose: print "Processed file %s [%d/%d]... %d blink(s)" % \ (obj.path, counter, len(objs), nb[-1]) obj.save(nb, args.outputdir, '.hdf5')
def main(): import os, sys from xbob.db.replay import Database, File from .. import utils basedir = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))) ANNOTATIONS = os.path.join(basedir, 'annotations') FEATURES = os.path.join(basedir, 'framediff') parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('annotations', metavar='DIR', type=str, default=ANNOTATIONS, nargs='?', help='Base directory containing the (flandmark) annotations to be treated by this procedure (defaults to "%(default)s")') parser.add_argument('path', metavar='PATH', type=str, help='Base path to the movie file you need plotting') parser.add_argument('output', metavar='FILE', type=str, help='Name of the output file to save the video') parser.add_argument('-M', '--maximum-displacement', metavar='FLOAT', type=float, dest="max_displacement", default=0.2, help="Maximum displacement (w.r.t. to the eye width) between eye-centers to consider the eye for calculating eye-differences") parser.add_argument('-S', '--skip-frames', metavar='INT', type=int, default=10, dest='skip', help="Number of frames to skip once an eye-blink has been detected (defaults to %(default)s)") parser.add_argument('-T', '--threshold-ratio', metavar='FLOAT', type=float, default=3.0, dest='thres_ratio', help="How many standard deviations to use for counting positive blink picks to %(default)s)") args = parser.parse_args() db = Database() # Gets the information concerning the input path or id db.assert_validity() splitted = args.path.split(os.sep) k = [splitted.index(k) for k in splitted if k in \ ('train', 'test', 'devel', 'enroll')][0] splitted[-1] = os.path.splitext(splitted[-1])[0] path_query = os.sep.join(splitted[k:]) obj = db.session.query(File).filter(File.path == path_query).one() video = bob.io.VideoReader(args.path) print "Opened movie file %s (%d frames), id = %d" % \ (args.path, len(video), obj.id) # Choose the printed frames here. start = 0 end = 225 # Loads the input video frames = [bob.ip.rgb_to_gray(k) for k in video[start:end]] # Recalculates the features annotations = utils.flandmark_load_annotations(obj, args.annotations, verbose=True) # Light-normalizes detected faces #utils.light_normalize_tantriggs(frames, annotations, start, end) utils.light_normalize_histogram(frames, annotations, start, end) features = numpy.zeros((len(frames), 2), dtype='float64') sys.stdout.write("Computing features ") sys.stdout.flush() for k in range(start+1, end): curr_annot = annotations[k] if annotations.has_key(k) else None prev_annot = annotations[k-1] if annotations.has_key(k-1) else None use_annotation = (prev_annot, curr_annot) use_frames = (frames[k-1], frames[k]) # maximum of 5 pixel displacement acceptable eye_diff, eye_pixels = utils.eval_eyes_difference(use_frames, use_annotation, max_center_displacement=args.max_displacement) facerem_diff, facerem_pixels = utils.eval_face_remainder_difference( use_frames, use_annotation, eye_diff, eye_pixels) if eye_pixels != 0: features[k][0] = eye_diff/float(eye_pixels) else: features[k][0] = 0. if facerem_pixels != 0: features[k][1] = facerem_diff/float(facerem_pixels) else: features[k][1] = 1. if eye_diff == 0: sys.stdout.write('x') else: sys.stdout.write('.') sys.stdout.flush() scores = utils.score(features) sys.stdout.write('\n') sys.stdout.flush() # plot N sequential images containing the video on the top and the advancing # graph of the features of choice on the bottom fig = mpl.figure() sys.stdout.write("Writing %d frames " % (end-start)) sys.stdout.flush() outv = None #output video place holder orows, ocolumns = None, None #the size of every frame in outv old_blinks = 0 for k in range(start,end): mpl.subplot(211) mpl.title("Frame %05d" % k) use_annotation = annotations[k] if annotations.has_key(k) else None if use_annotation: x, y, width, height = use_annotation['bbox'] bob.ip.draw_box(frames[k], x, y, width, height, 255) x, y, width, height = use_annotation['eyes'][0] bob.ip.draw_box(frames[k], x, y, width, height, 255) x, y, width, height = use_annotation['eyes'][1] bob.ip.draw_box(frames[k], x, y, width, height, 255) x, y, width, height = use_annotation['face_remainder'] bob.ip.draw_box(frames[k], x, y, width, height, 255) mpl.imshow(frames[k], cmap=GrayColorMap) #top plot mpl.subplot(212) score_set = scores[start:k+1] blinks = utils.count_blinks(score_set, args.thres_ratio, args.skip) rmean = utils.rmean(score_set)[-1] rstd = utils.rstd(score_set)[-1] threshold = (args.thres_ratio * rstd) + rmean mpl.plot(numpy.arange(start, k+1), score_set, linewidth=2, label='score') mpl.hlines(rmean, start, end, color='red', linestyles='dashed', alpha=0.8, label='mean') mpl.hlines(threshold, start, end, color='red', linestyles='solid', alpha=0.8, label='threshold') yrange = scores.max() - scores.min() mpl.axis((start, end, scores.min(), (0.2*yrange) + scores.max())) mpl.grid(True) mpl.xlabel("Frames | Blinks = %d" % blinks[-1]) mpl.ylabel("Magnitude") figure = fig2array(fig) if outv is None: orows = 2*(figure.shape[1]/2) ocolumns = 2*(figure.shape[2]/2) outv = bob.io.VideoWriter(args.output, orows, ocolumns, video.frame_rate) outv.append(figure[:,0:orows,0:ocolumns]) mpl.clf() if blinks[-1] != old_blinks: old_blinks = blinks[-1] sys.stdout.write('%d' % old_blinks) else: sys.stdout.write('.') sys.stdout.flush() sys.stdout.write('\n')
def main(): """Main method""" from xbob.db.replay import Database protocols = [k.name for k in Database().protocols()] basedir = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))) INPUTDIR = os.path.join(basedir, "framediff") OUTPUTDIR = os.path.join(basedir, "scores") parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument( "inputdir", metavar="DIR", type=str, default=INPUTDIR, nargs="?", help='Base directory containing the frame differences that will be used to produce the scores (defaults to "%(default)s").', ) parser.add_argument( "outputdir", metavar="DIR", type=str, default=OUTPUTDIR, nargs="?", help='Base directory that will be used to save the results (defaults to "%(default)s").', ) parser.add_argument( "-v", "--verbose", action="store_true", dest="verbose", default=False, help="Increases this script verbosity" ) parser.add_argument( "-p", "--protocol", metavar="PROTOCOL", type=str, default="grandtest", choices=protocols, dest="protocol", help="The protocol type may be specified to subselect a smaller number of files to operate on (one of '%s'; defaults to '%%(default)s')" % "|".join(sorted(protocols)), ) supports = ("fixed", "hand", "hand+fixed") parser.add_argument( "-s", "--support", metavar="SUPPORT", type=str, default="hand+fixed", dest="support", choices=supports, help="If you would like to select a specific support to be used, use this option (one of '%s'; defaults to '%%(default)s')" % "|".join(sorted(supports)), ) args = parser.parse_args() if not os.path.exists(args.inputdir): parser.error("input directory `%s' does not exist" % args.inputdir) if not os.path.exists(args.outputdir): if args.verbose: print "Creating output directory `%s'..." % args.outputdir bob.db.utils.makedirs_safe(args.outputdir) if args.support == "hand+fixed": args.support = ("hand", "fixed") db = Database() process = db.objects(protocol=args.protocol, support=args.support, cls=("real", "attack", "enroll")) counter = 0 for obj in process: counter += 1 if args.verbose: sys.stdout.write("Processing file %s [%d/%d] " % (obj.path, counter, len(process))) input = obj.load(args.inputdir, ".hdf5") obj.save(score(input), directory=args.outputdir, extension=".hdf5") if args.verbose: sys.stdout.write('Saving results to "%s"...\n' % args.outputdir) sys.stdout.flush() if args.verbose: print "All done, bye!"
def main(): import os, sys from xbob.db.replay import Database, File from .. import utils basedir = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))) ANNOTATIONS = os.path.join(basedir, 'annotations') FEATURES = os.path.join(basedir, 'framediff') parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('annotations', metavar='DIR', type=str, default=ANNOTATIONS, nargs='?', help='Base directory containing the (flandmark) annotations to be treated by this procedure (defaults to "%(default)s")') parser.add_argument('path', metavar='PATH', type=str, help='Base path to the movie file you need plotting') parser.add_argument('output', metavar='FILE', type=str, help='Name of the output file to save the video') parser.add_argument('-M', '--maximum-displacement', metavar='FLOAT', type=float, dest="max_displacement", default=0.2, help="Maximum displacement (w.r.t. to the eye width) between eye-centers to consider the eye for calculating eye-differences") parser.add_argument('-S', '--skip-frames', metavar='INT', type=int, default=10, dest='skip', help="Number of frames to skip once an eye-blink has been detected (defaults to %(default)s)") parser.add_argument('-T', '--threshold-ratio', metavar='FLOAT', type=float, default=3.0, dest='thres_ratio', help="How many standard deviations to use for counting positive blink picks to %(default)s)") args = parser.parse_args() db = Database() # Gets the information concerning the input path or id db.assert_validity() splitted = args.path.split(os.sep) k = [splitted.index(k) for k in splitted if k in \ ('train', 'test', 'devel')][0] splitted[-1] = os.path.splitext(splitted[-1])[0] path_query = os.sep.join(splitted[k:]) obj = db.session.query(File).filter(File.path == path_query).one() video = bob.io.VideoReader(args.path) print "Opened movie file %s (%d frames), id = %d" % \ (args.path, len(video), obj.id) # Choose the printed frames here. start = 0 end = 225 # Loads the input video frames = [bob.ip.rgb_to_gray(k) for k in video[start:end]] # Recalculates the features annotations = utils.flandmark_load_annotations(obj, args.annotations, verbose=True) # Light-normalizes detected faces #utils.light_normalize_tantriggs(frames, annotations, start, end) utils.light_normalize_histogram(frames, annotations, start, end) features = numpy.zeros((len(frames), 2), dtype='float64') sys.stdout.write("Computing features ") sys.stdout.flush() for k in range(start+1, end): curr_annot = annotations[k] if annotations.has_key(k) else None prev_annot = annotations[k-1] if annotations.has_key(k-1) else None use_annotation = (prev_annot, curr_annot) use_frames = (frames[k-1], frames[k]) # maximum of 5 pixel displacement acceptable eye_diff, eye_pixels = utils.eval_eyes_difference(use_frames, use_annotation, max_center_displacement=args.max_displacement) facerem_diff, facerem_pixels = utils.eval_face_remainder_difference( use_frames, use_annotation, eye_diff, eye_pixels) if eye_pixels != 0: features[k][0] = eye_diff/float(eye_pixels) else: features[k][0] = 0. if facerem_pixels != 0: features[k][1] = facerem_diff/float(facerem_pixels) else: features[k][1] = 1. if eye_diff == 0: sys.stdout.write('x') else: sys.stdout.write('.') sys.stdout.flush() scores = utils.score(features) sys.stdout.write('\n') sys.stdout.flush() # plot N sequential images containing the video on the top and the advancing # graph of the features of choice on the bottom fig = mpl.figure() sys.stdout.write("Writing %d frames " % (end-start)) sys.stdout.flush() outv = None #output video place holder orows, ocolumns = None, None #the size of every frame in outv old_blinks = 0 for k in range(start,end): mpl.subplot(211) mpl.title("Frame %05d" % k) use_annotation = annotations[k] if annotations.has_key(k) else None if use_annotation: x, y, width, height = use_annotation['bbox'] bob.ip.draw_box(frames[k], x, y, width, height, 255) x, y, width, height = use_annotation['eyes'][0] bob.ip.draw_box(frames[k], x, y, width, height, 255) x, y, width, height = use_annotation['eyes'][1] bob.ip.draw_box(frames[k], x, y, width, height, 255) x, y, width, height = use_annotation['face_remainder'] bob.ip.draw_box(frames[k], x, y, width, height, 255) mpl.imshow(frames[k], cmap=GrayColorMap) #top plot mpl.subplot(212) score_set = scores[start:k+1] blinks = utils.count_blinks(score_set, args.thres_ratio, args.skip) rmean = utils.rmean(score_set)[-1] rstd = utils.rstd(score_set)[-1] threshold = (args.thres_ratio * rstd) + rmean mpl.plot(numpy.arange(start, k+1), score_set, linewidth=2, label='score') mpl.hlines(rmean, start, end, color='red', linestyles='dashed', alpha=0.8, label='mean') mpl.hlines(threshold, start, end, color='red', linestyles='solid', alpha=0.8, label='threshold') yrange = scores.max() - scores.min() mpl.axis((start, end, scores.min(), (0.2*yrange) + scores.max())) mpl.grid(True) mpl.xlabel("Frames | Blinks = %d" % blinks[-1]) mpl.ylabel("Magnitude") figure = fig2array(fig) if outv is None: orows = 2*(figure.shape[1]/2) ocolumns = 2*(figure.shape[2]/2) outv = bob.io.VideoWriter(args.output, orows, ocolumns, video.frame_rate) outv.append(figure[:,0:orows,0:ocolumns]) mpl.clf() if blinks[-1] != old_blinks: old_blinks = blinks[-1] sys.stdout.write('%d' % old_blinks) else: sys.stdout.write('.') sys.stdout.flush() sys.stdout.write('\n')
def main(): import bob import numpy from xbob.db.replay import Database protocols = [k.name for k in Database().protocols()] basedir = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))) INPUTDIR = os.path.join(basedir, 'database') ANNOTATIONS = os.path.join(basedir, 'annotations') OUTPUTDIR = os.path.join(basedir, 'framediff') parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('inputdir', metavar='DIR', type=str, default=INPUTDIR, nargs='?', help='Base directory containing the videos to be treated by this procedure (defaults to "%(default)s")') parser.add_argument('annotations', metavar='DIR', type=str, default=ANNOTATIONS, nargs='?', help='Base directory containing the (flandmark) annotations to be treated by this procedure (defaults to "%(default)s")') parser.add_argument('outputdir', metavar='DIR', type=str, default=OUTPUTDIR, nargs='?', help='Base output directory for every file created by this procedure defaults to "%(default)s")') parser.add_argument('-p', '--protocol', metavar='PROTOCOL', type=str, default='grandtest', choices=protocols, dest="protocol", help='The protocol type may be specified instead of the the id switch to subselect a smaller number of files to operate on (one of "%s"; defaults to "%%(default)s")' % '|'.join(sorted(protocols))) parser.add_argument('-M', '--maximum-displacement', metavar='FLOAT', type=float, dest="max_displacement", default=0.2, help="Maximum displacement (w.r.t. to the eye width) between eye-centers to consider the eye for calculating eye-differences (defaults to %(default)s)") supports = ('fixed', 'hand', 'hand+fixed') parser.add_argument('-s', '--support', metavar='SUPPORT', type=str, default='hand+fixed', dest='support', choices=supports, help="If you would like to select a specific support to be used, use this option (one of '%s'; defaults to '%%(default)s')" % '|'.join(sorted(supports))) # The next option just returns the total number of cases we will be running # It can be used to set jman --array option. parser.add_argument('--grid-count', dest='grid_count', action='store_true', default=False, help=argparse.SUPPRESS) args = parser.parse_args() if args.support == 'hand+fixed': args.support = ('hand', 'fixed') from .. import utils db = Database() process = db.objects(protocol=args.protocol, support=args.support, cls=('real', 'attack', 'enroll')) if args.grid_count: print len(process) sys.exit(0) # if we are on a grid environment, just find what I have to process. if os.environ.has_key('SGE_TASK_ID'): key = int(os.environ['SGE_TASK_ID']) - 1 if key >= len(process): raise RuntimeError, "Grid request for job %d on a setup with %d jobs" % \ (key, len(process)) process = [process[key]] for counter, obj in enumerate(process): filename = str(obj.videofile(args.inputdir)) input = bob.io.VideoReader(filename) annotations = utils.flandmark_load_annotations(obj, args.annotations, verbose=True) sys.stdout.write("Processing file %s (%d frames) [%d/%d]..." % (filename, input.number_of_frames, counter+1, len(process))) # start the work here... frames = [bob.ip.rgb_to_gray(k) for k in input] #utils.light_normalize_tantriggs(frames, annotations, 0, len(frames)) utils.light_normalize_histogram(frames, annotations, 0, len(frames)) features = numpy.ndarray((input.number_of_frames, 2), dtype='float64') features[:] = numpy.NaN for k in range(1, len(frames)): curr_annot = annotations[k] if annotations.has_key(k) else None prev_annot = annotations[k-1] if annotations.has_key(k-1) else None use_annotation = (prev_annot, curr_annot) use_frames = (frames[k-1], frames[k]) # maximum of 5 pixel displacement acceptable eye_diff, eye_pixels = utils.eval_eyes_difference(use_frames, use_annotation, args.max_displacement) facerem_diff, facerem_pixels = utils.eval_face_remainder_difference( use_frames, use_annotation, eye_diff, eye_pixels) if eye_pixels != 0: features[k][0] = eye_diff/float(eye_pixels) else: features[k][0] = 0. if facerem_pixels != 0: features[k][1] = facerem_diff/float(facerem_pixels) else: features[k][1] = 1. if eye_diff == 0: sys.stdout.write('x') else: sys.stdout.write('.') sys.stdout.flush() obj.save(features, directory=args.outputdir, extension='.hdf5') sys.stdout.write('\n') sys.stdout.flush() return 0