Пример #1
0
def load_region_descriptors(trial_file):
    print "Loading descriptors from '{}'...".format(trial_file)
    fname, ext = os.path.splitext(trial_file)

    data = None
    if ext == ".pickle":
        with open(trial_file, "rb") as f:
            import pickle

            data = pickle.load(f)
    elif ext == ".yaml":
        data = fileutils.load_yaml_file(trial_file)
    else:
        raise ValueError("The file '{}' has an unrecognised extension: '{}'.".format(trial_file, ext))

    hog = utils.create_hog_from_info_dict(data["hog_descriptor_info"])
    regions = []
    descriptors = []
    for entry in data["region_descriptor_list"]:
        descriptors.append(np.array(entry["descriptor"], dtype=np.float32))

        rect = gm.PixelRectangle.from_opencv_bbox(entry["region"]["rect"])
        fname = entry["region"]["fname"]
        region = utils.ImageRegion(rect, fname)
        regions.append(region)

    return hog, regions, descriptors
Пример #2
0
def load_region_descriptors(trial_file):
    print 'Loading descriptors from \'{}\'...'.format(trial_file)
    fname, ext = os.path.splitext(trial_file)

    data = None
    if ext == '.pickle':
        with open(trial_file, 'rb') as f:
            import pickle
            data = pickle.load(f)
    elif ext == '.yaml':
        data = fileutils.load_yaml_file(trial_file)
    else:
        raise ValueError(
            'The file \'{}\' has an unrecognised extension: \'{}\'.'.format(
                trial_file, ext))

    hog = utils.create_hog_from_info_dict(data['hog_descriptor_info'])
    regions = []
    descriptors = []
    for entry in data['region_descriptor_list']:
        descriptors.append(np.array(entry['descriptor'], dtype=np.float32))

        rect = gm.PixelRectangle.from_opencv_bbox(entry['region']['rect'])
        fname = entry['region']['fname']
        region = utils.ImageRegion(rect, fname)
        regions.append(region)

    return hog, regions, descriptors
Пример #3
0
    def load_from_directory(cls, checkpoint_dir):
        detector = cls()

        config_yaml_fname = fileutils.find_in_ancestors('template.yaml')
        print 'config_yaml_fname:', os.path.abspath(config_yaml_fname)
        config_yaml = fileutils.load_yaml_file(config_yaml_fname)
        detector.window_dims = tuple(
            map(int, config_yaml['training']['svm']['window_dims']))

        # Clear all existing TensorFlow variables:
        # Note: This is necessary to prevent errors from the Saver when this
        # method is called more than once.
        # TODO: Find a way to avoid the need to do this.
        tf.reset_default_graph()

        # Placeholder for input images:
        # detector.window_dims = [28, 16]
        print 'detector.window_dims:', detector.window_dims
        img_w, img_h = detector.window_dims
        num_col_chnls = 3
        img_pixels = img_w * img_h * num_col_chnls
        detector.x_img = tf.placeholder("float", [img_h, img_w, num_col_chnls],
                                        name='x_img')
        detector.x = tf.placeholder("float", [None, img_pixels],
                                    name='input_images')
        logits, keep_prob, _ = cnnmodel.build_model(
            x=detector.x, window_dims=detector.window_dims)
        detector.logits = logits
        detector.keep_prob = keep_prob

        init = tf.initialize_all_variables()
        detector.sess = tf.Session(
            # config=tf.ConfigProto(
            #     inter_op_parallelism_threads=1,
            #     intra_op_parallelism_threads=1
            # )
        )
        # print 'Session created!'
        print detector.sess
        detector.sess.run(init)
        # print 'Variables initialised!'

        # Set up the checkpoint saver:
        saver = tf.train.Saver(tf.all_variables())

        # Restore the latest checkpoint:
        rel_checkpoint_dir = os.path.relpath(checkpoint_dir)
        latest_ckpt = tf.train.latest_checkpoint(rel_checkpoint_dir)
        print 'rel_checkpoint_dir', rel_checkpoint_dir
        print 'latest_ckpt', latest_ckpt
        print 'get_checkpoint_state', tf.train.get_checkpoint_state(
            rel_checkpoint_dir)
        saver.restore(detector.sess, latest_ckpt)

        # print 'Model restored!'

        return detector
Пример #4
0
    def __init__(self,
                 config_yaml_fname,
                 pos_frac,
                 exclusion_frac,
                 hard_neg_frac,
                 maxqsize=1000):
        self.config_yaml_fname = config_yaml_fname
        self.pos_frac = pos_frac
        self.exclusion_frac = exclusion_frac
        self.hard_neg_frac = hard_neg_frac
        self.maxqsize = maxqsize

        config_yaml = fileutils.load_yaml_file(config_yaml_fname)
        self.window_dims = tuple(
            map(int, config_yaml['training']['svm']['window_dims']))

        # Create the input data queue:
        w, h = self.window_dims
        depth = 3
        num_classes = 2
        feature_shape = [w * h * depth]
        label_shape = [num_classes]
        self.fifoq = tf.FIFOQueue(capacity=maxqsize,
                                  dtypes=[tf.float32, tf.float32],
                                  shapes=[feature_shape, label_shape])

        # self.tf_enqueue_op = self.fifoq.enqueue([self.many_feature_input, self.many_label_input])
        # self.feature_image = tf.placeholder(tf.float32, shape=[img_h, img_w, depth])
        self.many_feature_input = tf.placeholder(
            tf.float32, shape=[None, feature_shape[0]])
        self.many_label_input = tf.placeholder(tf.float32,
                                               shape=[None, label_shape[0]])
        self.tf_enqueue_many_op = self.fifoq.enqueue_many(
            [self.many_feature_input, self.many_label_input])

        self.feature_input = tf.placeholder(tf.float32, shape=feature_shape)
        self.feature_input_mod = random_modifiers_op(self.feature_input,
                                                     self.window_dims)
        self.label_input = tf.placeholder(tf.float32, shape=label_shape)
        self.tf_enqueue_op = self.fifoq.enqueue(
            [self.feature_input_mod, self.label_input])

        self.tf_qsize_op = self.fifoq.size()

        # Use threading or multiprocessing.
        # Threading was found to slow down execution.
        # self.coord = tf.train.Coordinator()
        # self.enqueue_threads = None

        # Process pool and results queue for multiprocessing:
        # self.pool = None
        self.processes = []
        self.mp_qsize = SharedCounter()
        self.mp_queue = multiprocessing.Queue(maxsize=maxqsize)
        self.mp_tf_thread = None
        self.mp_tf_shutdown = False
Пример #5
0
 def __init__(self, config_yaml_fname, pos_frac, exclusion_frac, hard_neg_frac):
     config_yaml = fileutils.load_yaml_file(config_yaml_fname)
     self.pos_reg_gen = generate_samples.load_positive_region_generator(config_yaml)
     self.neg_reg_gen = batch_shuffle(generate_samples.load_negative_region_generator(config_yaml), batch_size=100)
     self.exc_reg_gen = batch_shuffle(generate_samples.load_exclusion_region_generator(config_yaml), batch_size=5000)
     self.hard_neg_reg_gen = generate_samples.load_hard_negative_region_generator(config_yaml)
     self.window_dims = tuple(map(int, config_yaml['training']['svm']['window_dims']))
     self.pos_frac = pos_frac
     self.exc_frac = exclusion_frac
     self.hard_neg_frac = hard_neg_frac
Пример #6
0
    def load_from_directory(cls, checkpoint_dir):
        detector = cls()

        config_yaml_fname = fileutils.find_in_ancestors('template.yaml')
        print 'config_yaml_fname:', os.path.abspath(config_yaml_fname)
        config_yaml = fileutils.load_yaml_file(config_yaml_fname)
        detector.window_dims = tuple(map(int, config_yaml['training']['svm']['window_dims']))

        # Clear all existing TensorFlow variables:
        # Note: This is necessary to prevent errors from the Saver when this
        # method is called more than once.
        # TODO: Find a way to avoid the need to do this.
        tf.reset_default_graph()

        # Placeholder for input images:
        # detector.window_dims = [28, 16]
        print 'detector.window_dims:', detector.window_dims
        img_w, img_h = detector.window_dims
        num_col_chnls = 3
        img_pixels = img_w*img_h*num_col_chnls
        detector.x_img = tf.placeholder("float", [img_h, img_w, num_col_chnls], name='x_img')
        detector.x = tf.placeholder("float", [None, img_pixels], name='input_images')
        logits, keep_prob, _ = cnnmodel.build_model(
            x=detector.x,
            window_dims=detector.window_dims
        )
        detector.logits = logits
        detector.keep_prob = keep_prob

        init = tf.initialize_all_variables()
        detector.sess = tf.Session(
            # config=tf.ConfigProto(
            #     inter_op_parallelism_threads=1,
            #     intra_op_parallelism_threads=1
            # )
        )
        # print 'Session created!'
        print detector.sess
        detector.sess.run(init)
        # print 'Variables initialised!'

        # Set up the checkpoint saver:
        saver = tf.train.Saver(tf.all_variables())

        # Restore the latest checkpoint:
        rel_checkpoint_dir = os.path.relpath(checkpoint_dir)
        latest_ckpt = tf.train.latest_checkpoint(rel_checkpoint_dir)
        print 'rel_checkpoint_dir', rel_checkpoint_dir
        print 'latest_ckpt', latest_ckpt
        print 'get_checkpoint_state', tf.train.get_checkpoint_state(rel_checkpoint_dir)
        saver.restore(detector.sess, latest_ckpt)

        # print 'Model restored!'

        return detector
Пример #7
0
    def __init__(self, config_yaml_fname, pos_frac, exclusion_frac, hard_neg_frac, maxqsize=1000):
        self.config_yaml_fname = config_yaml_fname
        self.pos_frac = pos_frac
        self.exclusion_frac = exclusion_frac
        self.hard_neg_frac = hard_neg_frac
        self.maxqsize = maxqsize

        config_yaml = fileutils.load_yaml_file(config_yaml_fname)
        self.window_dims = tuple(map(int, config_yaml['training']['svm']['window_dims']))

        # Create the input data queue:
        w, h = self.window_dims
        depth = 3
        num_classes = 2
        feature_shape = [w*h*depth]
        label_shape = [num_classes]
        self.fifoq = tf.FIFOQueue(
            capacity=maxqsize,
            dtypes=[tf.float32, tf.float32],
            shapes=[feature_shape, label_shape]
        )

        # self.tf_enqueue_op = self.fifoq.enqueue([self.many_feature_input, self.many_label_input])
        # self.feature_image = tf.placeholder(tf.float32, shape=[img_h, img_w, depth])
        self.many_feature_input = tf.placeholder(tf.float32, shape=[None, feature_shape[0]])
        self.many_label_input = tf.placeholder(tf.float32, shape=[None, label_shape[0]])
        self.tf_enqueue_many_op = self.fifoq.enqueue_many([self.many_feature_input, self.many_label_input])

        self.feature_input = tf.placeholder(tf.float32, shape=feature_shape)
        self.feature_input_mod = random_modifiers_op(self.feature_input, self.window_dims)
        self.label_input = tf.placeholder(tf.float32, shape=label_shape)
        self.tf_enqueue_op = self.fifoq.enqueue([self.feature_input_mod, self.label_input])

        self.tf_qsize_op = self.fifoq.size()

        # Use threading or multiprocessing.
        # Threading was found to slow down execution.
        # self.coord = tf.train.Coordinator()
        # self.enqueue_threads = None

        # Process pool and results queue for multiprocessing:
        # self.pool = None
        self.processes = []
        self.mp_qsize = SharedCounter()
        self.mp_queue = multiprocessing.Queue(maxsize=maxqsize)
        self.mp_tf_thread = None
        self.mp_tf_shutdown = False
Пример #8
0
 def __init__(self, config_yaml_fname, pos_frac, exclusion_frac,
              hard_neg_frac):
     config_yaml = fileutils.load_yaml_file(config_yaml_fname)
     self.pos_reg_gen = generate_samples.load_positive_region_generator(
         config_yaml)
     self.neg_reg_gen = batch_shuffle(
         generate_samples.load_negative_region_generator(config_yaml),
         batch_size=100)
     self.exc_reg_gen = batch_shuffle(
         generate_samples.load_exclusion_region_generator(config_yaml),
         batch_size=5000)
     self.hard_neg_reg_gen = generate_samples.load_hard_negative_region_generator(
         config_yaml)
     self.window_dims = tuple(
         map(int, config_yaml['training']['svm']['window_dims']))
     self.pos_frac = pos_frac
     self.exc_frac = exclusion_frac
     self.hard_neg_frac = hard_neg_frac
Пример #9
0
def main():
    # random.seed(123454321) # Use deterministic samples.

    # Parse arguments:
    parser = argparse.ArgumentParser(description='Train a HOG + Linear SVM classifier.')
    parser.add_argument('classifier_yaml', type=str, nargs='?', default='template.yaml', help='Filename of the YAML file describing the classifier to train.')
    args = parser.parse_args()

    # Read classifier training file:
    classifier_yaml = fileutils.load_yaml_file(args.classifier_yaml)
    output_dir = args.classifier_yaml.split('.yaml')[0]

    window_dims = tuple(map(int, classifier_yaml['training']['svm']['window_dims']))
    print 'window_dims:', window_dims

    print 'Preview negative generation:'
    print '  [ESC]  Stop viewing negatives'
    print '  [ S ]  Save negative regions to disk'
    neg_num = int(classifier_yaml['training']['svm']['neg_num'])
    neg_output_dir = classifier_yaml['dataset']['directory']['generation']['output']['negative']
    def get_neg_reg_gen():
        # return generate_samples.load_negative_region_generator(classifier_yaml)
        # return generate_samples.load_exclusion_region_generator(classifier_yaml)
        return generate_samples.load_hard_negative_region_generator(classifier_yaml)

    # # TODO: REMOVE THIS CODE:
    # save_regions(get_neg_reg_gen(), neg_num, window_dims, neg_output_dir)
    # sys.exit(1)

    # neg_reg_generator = generate_samples.generate_negative_regions_in_image_with_exclusions(all_images[0], exl_info_map, window_dims)
    # print len(list(neg_reg_generator))
    mosaic_gen = utils.mosaic_generator(get_neg_reg_gen(), (5, 5), (100, 100))
    # mosaic_gen = utils.mosaic_generator(get_neg_reg_gen(), (10, 15), (40, 60))
    stop = False
    for mosaic in mosaic_gen:
        print 'mosaic'
        cv2.imshow('mosaic', mosaic)
        while True:
            key = cv2.waitKey(1) & 0xFF
            if key == 27:
                stop = True
                break
            if key == ord('s'):
                save_regions(get_neg_reg_gen(), neg_num, window_dims, neg_output_dir)
                stop = True
                break
            if key != 255:
                break
        if stop:
            break

    print 'Preview positive generation:'
    print '  [ESC]  Stop viewing positives'
    print '  [ S ]  Save positive regions to disk'
    pos_num = int(classifier_yaml['training']['svm']['pos_num'])
    bbinfo_dir = classifier_yaml['dataset']['directory']['bbinfo']
    pos_output_dir = classifier_yaml['dataset']['directory']['generation']['output']['positive']
    def get_pos_reg_gen():
        return generate_samples.load_positive_region_generator(classifier_yaml)
    mosaic_gen = utils.mosaic_generator(get_pos_reg_gen(), (4, 6), (window_dims[1], window_dims[0]))
    # mosaic_gen = utils.mosaic_generator(pos_reg_generator, (20, 30), (40, 60))
    stop = False
    for mosaic in mosaic_gen:
        print 'mosaic'
        cv2.imshow('mosaic', mosaic)
        while True:
            key = cv2.waitKey(1) & 0xFF
            if key == 27:
                stop = True
                break
            if key == ord('s'):
                save_regions(get_pos_reg_gen(), pos_num, window_dims, pos_output_dir)
                save_generated_bbinfo(pos_num, window_dims, pos_output_dir, bbinfo_dir)
                stop = True
                break
            if key != 255:
                break
        if stop:
            break
Пример #10
0
	def doRunning(trial_yaml):
		# Read classifier training file:
		classifier_yaml = fileutils.load_yaml_file(trial_yaml)
		output_dir = trial_yaml.split('.yaml')[0]
		training.runClassifier(classifier_yaml, output_dir)
Пример #11
0
	def doTraining(fname):
		# Read classifier training file:
		classifier_yaml = fileutils.load_yaml_file(fname)
		output_dir = fname.split('.yaml')[0]
		training.trainClassifier(classifier_yaml, output_dir)
Пример #12
0
	# print '===== VIEW SAMPLES ====='
	# print 'Note: Comment out this section if you actually want to train anything.'
	# for trial_yaml in trial_files:
	# 	classifier_yaml = fileutils.load_yaml_file(trial_yaml)
	# 	output_dir = trial_yaml.split('.yaml')[0]
	# 	training.view_positive_samples(classifier_yaml, output_dir)

	print '===== PREPROCESS TRIALS ====='

	preprocessing_was_successful = True
	maxImageCountDiff = (0, 0, 0)

	for trial_yaml in trial_files:
		print '    Preprocessing: {}'.format(trial_yaml)
		# Read classifier training file:
		classifier_yaml = fileutils.load_yaml_file(trial_yaml)
		output_dir = trial_yaml.split('.yaml')[0]

		# Preprocess the trial:
		try:
			training.preprocessTrial(classifier_yaml, output_dir)
		except training.TooFewImagesError as e:
			preprocessing_was_successful = False
			print e
			imgCountDiff = map(lambda (p, r): r - p, zip(e.presentCounts, e.requiredCounts))
			maxImageCountDiff = map(lambda (m, c): max(m, c), zip(maxImageCountDiff, imgCountDiff))

	if not preprocessing_was_successful:
		print '\nNOT ENOUGH IMAGES! TRAINING CANCELLED!'
		print 'maxImageCountDiff: {}'.format(maxImageCountDiff)
		sys.exit(1)
    # print '===== VIEW SAMPLES ====='
    # print 'Note: Comment out this section if you actually want to train anything.'
    # for trial_yaml in trial_files:
    # 	classifier_yaml = fileutils.load_yaml_file(trial_yaml)
    # 	output_dir = trial_yaml.split('.yaml')[0]
    # 	training.view_positive_samples(classifier_yaml, output_dir)

    print '===== PREPROCESS TRIALS ====='

    preprocessing_was_successful = True
    maxImageCountDiff = (0, 0, 0)

    for trial_yaml in trial_files:
        print '    Preprocessing: {}'.format(trial_yaml)
        # Read classifier training file:
        classifier_yaml = fileutils.load_yaml_file(trial_yaml)
        output_dir = trial_yaml.split('.yaml')[0]

        # Preprocess the trial:
        try:
            training.preprocessTrial(classifier_yaml, output_dir)
        except training.TooFewImagesError as e:
            preprocessing_was_successful = False
            print e
            imgCountDiff = map(lambda (p, r): r - p,
                               zip(e.presentCounts, e.requiredCounts))
            maxImageCountDiff = map(lambda (m, c): max(m, c),
                                    zip(maxImageCountDiff, imgCountDiff))

    if not preprocessing_was_successful:
        print '\nNOT ENOUGH IMAGES! TRAINING CANCELLED!'
Пример #14
0
def image_directories():
    """Load the image directories from the config file"""
    config_yaml = fileutils.load_yaml_file(detector_config_fname)
    hashed_config = hash_config_values(config_yaml['image_directories'])
    return jsonify(image_directories=hashed_config)
Пример #15
0
def main():
    # random.seed(123454321) # Use deterministic samples.

    # Parse arguments:
    parser = argparse.ArgumentParser(
        description='Train a HOG + Linear SVM classifier.')
    parser.add_argument(
        'classifier_yaml',
        type=str,
        nargs='?',
        default='template.yaml',
        help='Filename of the YAML file describing the classifier to train.')
    args = parser.parse_args()

    # Read classifier training file:
    classifier_yaml = fileutils.load_yaml_file(args.classifier_yaml)
    output_dir = args.classifier_yaml.split('.yaml')[0]

    window_dims = tuple(
        map(int, classifier_yaml['training']['svm']['window_dims']))
    print 'window_dims:', window_dims

    print 'Preview negative generation:'
    print '  [ESC]  Stop viewing negatives'
    print '  [ S ]  Save negative regions to disk'
    neg_num = int(classifier_yaml['training']['svm']['neg_num'])
    neg_output_dir = classifier_yaml['dataset']['directory']['generation'][
        'output']['negative']

    def get_neg_reg_gen():
        # return generate_samples.load_negative_region_generator(classifier_yaml)
        # return generate_samples.load_exclusion_region_generator(classifier_yaml)
        return generate_samples.load_hard_negative_region_generator(
            classifier_yaml)

    # # TODO: REMOVE THIS CODE:
    # save_regions(get_neg_reg_gen(), neg_num, window_dims, neg_output_dir)
    # sys.exit(1)

    # neg_reg_generator = generate_samples.generate_negative_regions_in_image_with_exclusions(all_images[0], exl_info_map, window_dims)
    # print len(list(neg_reg_generator))
    mosaic_gen = utils.mosaic_generator(get_neg_reg_gen(), (5, 5), (100, 100))
    # mosaic_gen = utils.mosaic_generator(get_neg_reg_gen(), (10, 15), (40, 60))
    stop = False
    for mosaic in mosaic_gen:
        print 'mosaic'
        cv2.imshow('mosaic', mosaic)
        while True:
            key = cv2.waitKey(1) & 0xFF
            if key == 27:
                stop = True
                break
            if key == ord('s'):
                save_regions(get_neg_reg_gen(), neg_num, window_dims,
                             neg_output_dir)
                stop = True
                break
            if key != 255:
                break
        if stop:
            break

    print 'Preview positive generation:'
    print '  [ESC]  Stop viewing positives'
    print '  [ S ]  Save positive regions to disk'
    pos_num = int(classifier_yaml['training']['svm']['pos_num'])
    bbinfo_dir = classifier_yaml['dataset']['directory']['bbinfo']
    pos_output_dir = classifier_yaml['dataset']['directory']['generation'][
        'output']['positive']

    def get_pos_reg_gen():
        return generate_samples.load_positive_region_generator(classifier_yaml)

    mosaic_gen = utils.mosaic_generator(get_pos_reg_gen(), (4, 6),
                                        (window_dims[1], window_dims[0]))
    # mosaic_gen = utils.mosaic_generator(pos_reg_generator, (20, 30), (40, 60))
    stop = False
    for mosaic in mosaic_gen:
        print 'mosaic'
        cv2.imshow('mosaic', mosaic)
        while True:
            key = cv2.waitKey(1) & 0xFF
            if key == 27:
                stop = True
                break
            if key == ord('s'):
                save_regions(get_pos_reg_gen(), pos_num, window_dims,
                             pos_output_dir)
                save_generated_bbinfo(pos_num, window_dims, pos_output_dir,
                                      bbinfo_dir)
                stop = True
                break
            if key != 255:
                break
        if stop:
            break
 def doTraining(fname):
     # Read classifier training file:
     classifier_yaml = fileutils.load_yaml_file(fname)
     output_dir = fname.split('.yaml')[0]
     training.trainClassifier(classifier_yaml, output_dir)
Пример #17
0
    #     bbinfo_map[fname] = rects
    #     cv2.imwrite(fname, img)
    #
    # # Create the bounding box info directory:
    # bbinfo_dir = os.path.join(base_dir, 'bbinfo')
    # if not os.path.isdir(bbinfo_dir):
    #     os.makedirs(bbinfo_dir)
    #
    # bbinfo_file = os.path.join(bbinfo_dir, 'synthetic__bbinfo.dat')
    # utils.save_opencv_bounding_box_info(bbinfo_file, bbinfo_map)


if __name__ == '__main__':
    # random.seed(123454321) # Use deterministic samples.

    # Parse arguments:
    parser = argparse.ArgumentParser(description='Generate a synthetic dataset.')
    parser.add_argument('dataset_yaml', type=str, nargs='?', default='template.yaml', help='Filename of the YAML file containing the dataset parameters.')
    args = parser.parse_args()

    # Read classifier training file:
    classifier_yaml = fileutils.load_yaml_file(args.dataset_yaml)

    base_dir = classifier_yaml['dataset']['directory']['synthetic']
    pos_num = int(classifier_yaml['training']['svm']['pos_num'])
    neg_num = int(classifier_yaml['training']['svm']['neg_num'])

    test_num = 100

    synthesise_dataset(base_dir, pos_num, neg_num, test_num, objects_per_image=3)
 def doRunning(trial_yaml):
     # Read classifier training file:
     classifier_yaml = fileutils.load_yaml_file(trial_yaml)
     output_dir = trial_yaml.split('.yaml')[0]
     training.runClassifier(classifier_yaml, output_dir)
Пример #19
0
def image_directories():
    """Load the image directories from the config file"""
    config_yaml = fileutils.load_yaml_file(detector_config_fname)
    hashed_config = hash_config_values(config_yaml['image_directories'])
    return jsonify(image_directories=hashed_config)
Пример #20
0
def update_preview_state():
    """Return the new state of the UI given the new settings."""
    print 'update_preview_state'

    print 'request data:'
    pprint.pprint(request.json)

    # Get the inputs:
    currentImgIndex = request.json['currentImgIndex']
    hashed_image_dir = request.json['imageDir']
    hashed_detector_dir = request.json['detectorDir']
    performDetection = request.json['performDetection']
    returnImage = request.json['returnImage']

    config_yaml = fileutils.load_yaml_file(detector_config_fname)

    imageDir = validate_image_directory(hashed_image_dir, config_yaml)

    # Get the images:
    image_list = sorted(utils.list_images_in_directory(imageDir))
    num_images = len(image_list)

    if num_images == 0:
        print 'ERROR: The directory \'{}\' contains no images.'.format(
            imageDir)
        # TODO: Display a better error to the client.
        flask.abort(404)  # HTTP status codes: Not Found

    # Find the current image:
    if not currentImgIndex:
        currentImgIndex = 0
    current_img_index = currentImgIndex % num_images
    current_img_path = image_list[current_img_index]
    send_img_path = current_img_path

    # Perform detection:
    detections = []
    if performDetection:
        save_img_dir = os.path.join(app.root_path, 'static', 'cache')
        save_img_fname = '{}.jpg'.format(
            url_safe_hash(current_img_path + hashed_detector_dir))
        save_img_path = os.path.join(save_img_dir, save_img_fname)
        send_img_path = save_img_path
        detection_img_exists = os.path.isfile(save_img_path)
        # if not returnImage and detection_img_exists:
        #     # Delete the current image so that it isn't returned by mistake if
        #     # the detection process fails.
        #     os.remove(save_img_path)
        if not returnImage or not detection_img_exists:
            # Perform the detection.

            detectorDir = validate_detector_directory(hashed_detector_dir,
                                                      config_yaml)

            # detector = ObjectDetector.load_from_directory(detectorDir)
            with ObjectDetector(detectorDir) as detector:
                img = cv2.imread(current_img_path)
                detections, img = detector.detect_objects_in_image(img)

                # cv2.imwrite fails silently if it can't save the image for any
                # reason. We manually check access permissions here, and throw
                # an exception to inform the webmaster if they're incorrect.
                sav_img_dir, _ = os.path.split(save_img_path)
                if not os.path.isdir(sav_img_dir):
                    raise IOError(
                        'The sav_img_dir \'{}\' does not exist.'.format(
                            sav_img_dir))
                if not os.access(sav_img_dir, os.W_OK):
                    raise IOError(
                        'The server user (probably www-data) does not have write permissions for the save_img_path: \'{sav_img_dir}\''
                        .format(sav_img_dir))
                cv2.imwrite(save_img_path, img)

    previewState = jsonify({
        'numImages': num_images,
        'currentImgIndex': current_img_index,
        'currentImgPath': current_img_path,
        # 'currentImgUrl' : None,
        'detections': detections
    })

    if returnImage:
        print 'preview state:', previewState.data
        # Return the image
        return flask.send_file(send_img_path)
    else:
        # Return the new preview state:
        print 'response data:', previewState.data
        return previewState
Пример #21
0
if __name__ == "__main__":
    # random.seed(123454321) # Use deterministic samples.

    # Parse arguments:
    parser = argparse.ArgumentParser(description="Train a HOG + Linear SVM classifier.")
    parser.add_argument(
        "classifier_yaml",
        type=str,
        nargs="?",
        default="template.yaml",
        help="Filename of the YAML file describing the classifier to train.",
    )
    args = parser.parse_args()

    # Read classifier training file:
    classifier_yaml = fileutils.load_yaml_file(args.classifier_yaml)
    output_dir = args.classifier_yaml.split(".yaml")[0]

    window_dims = tuple(map(int, classifier_yaml["training"]["svm"]["window_dims"]))
    print "window_dims:", window_dims

    # test_random_with_same_aspect()
    #
    # print 'Test negative generation:'
    # bak_img_dir = classifier_yaml['dataset']['directory']['background']
    # pos_img_dir = classifier_yaml['dataset']['directory']['positive']
    # bbinfo_dir = classifier_yaml['dataset']['directory']['bbinfo']
    # exl_info_map = utils.load_opencv_bounding_box_info('/Users/mitchell/data/car-detection/bbinfo/shopping__exclusion.dat')
    # pos_reg_generator = generate_positive_regions(pos_img_dir, bbinfo_dir, classifier_yaml['dataset']['modifiers'], window_dims)
    # # neg_reg_generator = generate_negative_regions_with_exclusions(bak_img_dir, exl_info_map, window_dims)
    # # all_images = utils.list_images_in_directory(bak_img_dir)
Пример #22
0
import cardetection.carutils.fileutils as fileutils
import cardetection.carutils.geometry as gm
import cardetection.detection.cascadetraining as training

if __name__ == '__main__':
    if cv2.__version__ != '3.1.0':
        print 'ERROR: This script requires OpenCV 3.1.0, but cv2.__version__ ==', cv2.__version__
        sys.exit(1)

    # Parse arguments:
    parser = argparse.ArgumentParser(description='Cluster positive images and save mosaics and average images for each cluster.')
    parser.add_argument('classifier_yaml', type=str, nargs='?', default='template.yaml', help='Filename of the YAML file describing the classifier to train.')
    args = parser.parse_args()

    # Read classifier training file:
    classifier_yaml = fileutils.load_yaml_file(args.classifier_yaml)
    output_dir = args.classifier_yaml.split('.yaml')[0]

    window_dims = tuple(map(int, classifier_yaml['training']['svm']['window_dims']))
    window_shape = (window_dims[1], window_dims[0])
    print 'window_dims:', window_dims
    print 'window_shape:', window_dims

    pos_img_dir = classifier_yaml['dataset']['directory']['positive']
    bbinfo_dir = classifier_yaml['dataset']['directory']['bbinfo']
    def get_pos_generator():
        reg_gen = trainhog.generate_positive_regions(pos_img_dir, bbinfo_dir, modifiers_config=None, window_dims=window_dims)
        # Limit the number of regions to use:
        return itertools.islice(reg_gen, 0, 10000)

    pos_reg_generator = get_pos_generator()
Пример #23
0
def update_preview_state():
    """Return the new state of the UI given the new settings."""
    print 'update_preview_state'

    print 'request data:'
    pprint.pprint(request.json)

    # Get the inputs:
    currentImgIndex = request.json['currentImgIndex']
    hashed_image_dir = request.json['imageDir']
    hashed_detector_dir = request.json['detectorDir']
    performDetection = request.json['performDetection']
    returnImage = request.json['returnImage']

    config_yaml = fileutils.load_yaml_file(detector_config_fname)

    imageDir = validate_image_directory(hashed_image_dir, config_yaml)

    # Get the images:
    image_list = sorted(utils.list_images_in_directory(imageDir))
    num_images = len(image_list)

    if num_images == 0:
        print 'ERROR: The directory \'{}\' contains no images.'.format(imageDir)
        # TODO: Display a better error to the client.
        flask.abort(404) # HTTP status codes: Not Found

    # Find the current image:
    if not currentImgIndex:
        currentImgIndex = 0
    current_img_index = currentImgIndex % num_images
    current_img_path = image_list[current_img_index]
    send_img_path = current_img_path

    # Perform detection:
    detections = []
    if performDetection:
        save_img_dir = os.path.join(app.root_path, 'static', 'cache')
        save_img_fname = '{}.jpg'.format(url_safe_hash(current_img_path + hashed_detector_dir))
        save_img_path = os.path.join(save_img_dir, save_img_fname)
        send_img_path = save_img_path
        detection_img_exists = os.path.isfile(save_img_path)
        # if not returnImage and detection_img_exists:
        #     # Delete the current image so that it isn't returned by mistake if
        #     # the detection process fails.
        #     os.remove(save_img_path)
        if not returnImage or not detection_img_exists:
            # Perform the detection.

            detectorDir = validate_detector_directory(hashed_detector_dir, config_yaml)

            # detector = ObjectDetector.load_from_directory(detectorDir)
            with ObjectDetector(detectorDir) as detector:
                img = cv2.imread(current_img_path)
                detections, img = detector.detect_objects_in_image(img)

                # cv2.imwrite fails silently if it can't save the image for any
                # reason. We manually check access permissions here, and throw
                # an exception to inform the webmaster if they're incorrect.
                sav_img_dir, _ = os.path.split(save_img_path)
                if not os.path.isdir(sav_img_dir):
                    raise IOError('The sav_img_dir \'{}\' does not exist.'.format(sav_img_dir))
                if not os.access(sav_img_dir, os.W_OK):
                    raise IOError('The server user (probably www-data) does not have write permissions for the save_img_path: \'{sav_img_dir}\''.format(sav_img_dir))
                cv2.imwrite(save_img_path, img)

    previewState = jsonify({
        'numImages' : num_images,
        'currentImgIndex' : current_img_index,
        'currentImgPath' : current_img_path,
        # 'currentImgUrl' : None,
        'detections' : detections
    })

    if returnImage:
        print 'preview state:', previewState.data
        # Return the image
        return flask.send_file(send_img_path)
    else:
        # Return the new preview state:
        print 'response data:', previewState.data
        return previewState