예제 #1
0
def main(args):
    image = cv.LoadImage( args.input_image)
    featuremap = FeatureMap()
    featuremap.read_from_file( args.input_features )
    classifier = load_classifier_from_file( args.classifier )
    if not classifier.is_trained():
        print "Warning: using an untrained classifier. This will probably break."
    label_set = LabelSet()
    label_set.read_from_file( args.label_set )

    window = ClickWindow( image, args.zoom_out)

    while(True):
        cont = update_all_windows()
        if not cont:
            break
        if window.update_label:
            click_pt = window.click_pt
            closest_pt = min( featuremap.get_feature_points(),
                              key = lambda pt: l2_dist(pt,click_pt) )
            feature = featuremap.get_feature( closest_pt )
            shape = featuremap.get_shape( closest_pt )
            size = featuremap.get_size( closest_pt )

            label = classifier.predict_label( feature )
            label_name = "None" if label==0 else label_set.get_label_name(label)
            label_color = cv.RGB(0,0,0) if label==0 else label_set.get_label_color(label)
            print "Predicted label: %d (%s)"%(label, label_name);
            window.set_patch( closest_pt, shape, size, label_color )
            window.update_label = False
예제 #2
0
def main(args):

    if not (args.input_classifier or args.classifier_type):
        print "Must specify input classifier or type!"
        return 1

    if args.input_classifier:
        args.classifier_type = get_classifier_name(args.input_classifier)
        output_file = args.input_classifier
    else:
        if args.output_directory:
            directory = args.output_directory
        else:
            directory = os.path.dirname(args.input_features)
            print "No output directory specified. Defaulting to %s" % directory
        if not os.path.exists(directory):
            os.makedirs(directory)
        if args.output_prefix:
            prefix = args.output_prefix
        else:
            prefix, extension = os.path.splitext(
                os.path.basename(args.input_features))
            print "No output prefix selected. Defaulting to %s" % prefix
        output_file = "%s/%s.cls" % (directory, prefix)

    if args.classifier_type in CPP_CLASSIFIERS:
        call_cpp(args)
        return
    #Now onto just the python implementation
    if args.input_classifier:
        classifier = load_classifier_from_file(args.input_classifier)
    else:
        classifier = instantiate_classifier_by_name(
            args.classifier_type, include_unlabeled=args.include_unlabeled)

    fm = FeatureMap()
    if (args.input_features):
        fm.read_from_file(args.input_features)

    classifier.add_featuremap(fm)
    if args.train:
        classifier.train()

    classifier.save_to_file(output_file)
    print "Saved classifier to %s" % output_file
예제 #3
0
def main(args):
    
    if not ( args.input_classifier or args.classifier_type ):
        print "Must specify input classifier or type!"
        return 1

    if args.input_classifier:
        args.classifier_type = get_classifier_name( args.input_classifier )
        output_file = args.input_classifier
    else:
        if args.output_directory:
            directory = args.output_directory
        else:
            directory = os.path.dirname(args.input_features)
            print "No output directory specified. Defaulting to %s"%directory
        if not os.path.exists(directory):
            os.makedirs(directory)
        if args.output_prefix:
            prefix = args.output_prefix
        else:
            prefix, extension = os.path.splitext(os.path.basename(args.input_features))
            print "No output prefix selected. Defaulting to %s"%prefix
        output_file = "%s/%s.cls"%(directory,prefix)

    if args.classifier_type in CPP_CLASSIFIERS:
        call_cpp(args)
        return
    #Now onto just the python implementation
    if args.input_classifier:
        classifier = load_classifier_from_file( args.input_classifier )
    else:
        classifier = instantiate_classifier_by_name( args.classifier_type, include_unlabeled=args.include_unlabeled )

    fm = FeatureMap()
    if( args.input_features):
        fm.read_from_file( args.input_features )

    classifier.add_featuremap( fm )
    if args.train:
        classifier.train()

    classifier.save_to_file( output_file )
    print "Saved classifier to %s"%output_file
예제 #4
0
def main(args):

    if args.output_directory:
        directory = args.output_directory
    else:
        directory = os.path.dirname(args.input_image)
        print "No output directory specified. Defaulting to %s" % directory
    if not os.path.exists(directory):
        os.makedirs(directory)
    if args.output_prefix:
        prefix = args.output_prefix
    else:
        prefix = os.path.splitext(os.path.basename(args.input_image))[0]
        print "No output prefix selected. Defaulting to %s" % prefix
    output_file = "%s/%s.fm" % (directory, prefix)
    if not args.patch_step:
        args.patch_step = args.patch_size
    if args.use_mask and not args.mask_file:
        pre, ext = os.path.splitext(args.input_image)
        possible_mask_path = "%s.mask.png" % (prefix)
        if os.path.exists(possible_mask_path):
            args.mask_file = possible_mask_path
            print "Using mask"
    if args.feature_type in CPP_DESCRIPTORS:
        cmd = "rosrun %s make_featuremap -i %s -o %s -f %s -p %d -s %d -D %s %s %s %s %s" % (
            PACKAGE_NAME, args.input_image, output_file, args.feature_type,
            args.patch_size, args.patch_step, args.detector,
            "-P %s" % args.points_file if args.points_file else "", "-m %s" %
            args.mask_file if args.mask_file else "", "-l %s" % args.label_file
            if args.label_file else "", "-v" if args.verbose else "")
        print "Calling %s" % cmd
        return call(cmd, shell=True)

    if args.feature_type == 'LBP':
        descriptor = LBPDescriptor(args.patch_size)
    elif args.feature_type == 'RAW_BW':
        descriptor = RawBWDescriptor(args.patch_size)
    elif args.feature_type == "RAW_COLOR":
        descriptor = RawColorDescriptor(args.patch_size)
    else:
        raise Exception("Invalid feature!")
    input_image = cv.LoadImage(args.input_image)
    patch_maker = SlidingWindowPatchMaker(args.patch_size, args.patch_step)
    features, patch_centers = descriptor.process_image(input_image,
                                                       patch_maker,
                                                       args.verbose)
    feature_map = FeatureMap()
    feature_map.set_patch_size((args.patch_size, args.patch_size))
    shape = 'SQUARE' if 'SQUARE' in args.detector else 'CIRCLE'
    for i, ctr in enumerate(patch_centers):
        feature_map.add_feature(ctr, shape, (args.patch_size, args.patch_size),
                                features[i])
    feature_map.save_to_file("%s/%s.fm" % (directory, prefix))
예제 #5
0
def main(args):
    
    if args.output_directory:
        directory = args.output_directory
    else:
        directory = os.path.dirname(args.input_image)
        print "No output directory specified. Defaulting to %s"%directory
    if not os.path.exists(directory):
        os.makedirs(directory)
    if args.output_prefix:
        prefix = args.output_prefix
    else:
        prefix = os.path.splitext(os.path.basename(args.input_image))[0]
        print "No output prefix selected. Defaulting to %s"%prefix
    output_file = "%s/%s.fm"%(directory,prefix)
    if not args.patch_step:
        args.patch_step = args.patch_size
    if args.use_mask and not args.mask_file:
        pre, ext = os.path.splitext(args.input_image)
        possible_mask_path = "%s.mask.png"%(prefix)
        if os.path.exists(possible_mask_path):
            args.mask_file = possible_mask_path
            print "Using mask"
    if args.feature_type in CPP_DESCRIPTORS:
        cmd = "rosrun %s make_featuremap -i %s -o %s -f %s -p %d -s %d -D %s %s %s %s %s"%(
                PACKAGE_NAME, args.input_image, output_file, args.feature_type, 
                args.patch_size, args.patch_step, args.detector,
                "-P %s"%args.points_file if args.points_file else "",
                "-m %s"%args.mask_file if args.mask_file else "",
                "-l %s"%args.label_file if args.label_file else "",
                "-v" if args.verbose else "")
        print "Calling %s"%cmd
        return call( cmd, shell=True);
    
    if args.feature_type == 'LBP':
        descriptor = LBPDescriptor(args.patch_size)
    elif args.feature_type == 'RAW_BW':
        descriptor = RawBWDescriptor(args.patch_size)
    elif args.feature_type == "RAW_COLOR":
        descriptor = RawColorDescriptor(args.patch_size)
    else:
        raise Exception("Invalid feature!")
    input_image = cv.LoadImage( args.input_image )
    patch_maker = SlidingWindowPatchMaker( args.patch_size, args.patch_step)
    features, patch_centers = descriptor.process_image( input_image, patch_maker, args.verbose )
    feature_map = FeatureMap()
    feature_map.set_patch_size( (args.patch_size,args.patch_size) )
    shape = 'SQUARE' if 'SQUARE' in args.detector else 'CIRCLE'
    for i, ctr in enumerate(patch_centers):
        feature_map.add_feature( ctr, shape, (args.patch_size, args.patch_size), features[i] )
    feature_map.save_to_file("%s/%s.fm"%(directory,prefix))
예제 #6
0
def main(args):
    n_views = len(args.images)
    assert len(args.features) == len(args.zoom_outs) == n_views
    view_windows = []
    for i in range(n_views):
        image = cv.LoadImage( args.images[i] )
        featuremap = FeatureMap()
        featuremap.read_from_file( args.features[i] )
        zoom_out = args.zoom_outs[i]
        view_window = ViewWindow( image, zoom_out, featuremap, i)
        view_windows.append(view_window)
    #Tile
    x = 0
    y = 0
    for win in view_windows:
        if x + win.size()[0] >= SCREEN_WIDTH:
            x = 0
            y += win.size()[1]
        win.move(x,y)
        x += win.size()[0]
    while(True):
        cont = update_all_windows()
        if not cont:
            break
예제 #7
0
def main(args):
    if args.output_directory:
        directory = args.output_directory
    else:
        directory = os.path.dirname(args.input_features)
        if directory == "":
            directory = "."
        print "No output directory specified. Defaulting to %s" % directory
    if not os.path.exists(directory):
        os.makedirs(directory)
    if args.output_prefix:
        prefix = args.output_prefix
    else:
        prefix, extension = os.path.splitext(
            os.path.basename(args.input_features))
        print "No output prefix selected. Defaulting to %s" % prefix
    output_file = "%s/%s.libsvm" % (directory, prefix)

    fm = FeatureMap()
    fm.read_from_file(args.input_features)

    labeled_features = []
    for pt in fm.get_feature_points():
        feature = fm.get_feature(pt)
        label = fm.get_label(pt)
        if label < 0:
            continue
        if (not args.include_unlabeled) and label == 0:
            continue
        labeled_features.append((label, feature))
    f = open(output_file, 'w')
    print len(labeled_features)
    for (label, feature) in labeled_features:
        f.write("%d\t" % label)
        for i, val in enumerate(feature):
            if val == 0:
                continue
            f.write("%d:%f " % (i, val))
        f.write("\n")
    f.close()
예제 #8
0
def main(args):
    if args.output_directory:
        directory = args.output_directory
    else:
        directory = os.path.dirname(args.input_features)
        if directory == "":
            directory = "."
        print "No output directory specified. Defaulting to %s"%directory
    if not os.path.exists(directory):
        os.makedirs(directory)
    if args.output_prefix:
        prefix = args.output_prefix
    else:
        prefix, extension = os.path.splitext(os.path.basename(args.input_features))
        print "No output prefix selected. Defaulting to %s"%prefix
    output_file = "%s/%s.libsvm"%(directory,prefix)

    fm = FeatureMap()
    fm.read_from_file( args.input_features )
    
    labeled_features = []
    for pt in fm.get_feature_points():
        feature = fm.get_feature(pt)
        label = fm.get_label(pt)
        if label < 0:
            continue
        if (not args.include_unlabeled) and label == 0:
            continue
        labeled_features.append( (label, feature) )
    f = open(output_file,'w')
    print len(labeled_features)
    for (label, feature) in labeled_features:
        f.write("%d\t"%label)
        for i,val in enumerate(feature):
            if val == 0:
                continue
            f.write("%d:%f "%(i,val))
        f.write("\n")
    f.close()
예제 #9
0
def main(args):
    compared_image = cv.LoadImage( args.compared_image)
    reference_image = cv.LoadImage( args.reference_image )
    compared_featuremap = FeatureMap()
    compared_featuremap.read_from_file( args.compared_features )
    reference_featuremap = FeatureMap()
    reference_featuremap.read_from_file( args.reference_features )
    compare_window = ClickWindow( compared_image, args.compare_zoom_out)
    reference_window = ReferenceWindow( reference_image, args.reference_zoom_out)
    reference_window.move( compare_window.size()[0], 0 ) 

    #nn_solver = pyflann.FLANN()
    while(True):
        cont = update_all_windows()
        if not cont:
            break
        if compare_window.update_nn:
            click_pt = compare_window.click_pt
            closest_pt = min( compared_featuremap.get_feature_points(),
                              key = lambda pt: l2_dist(pt,click_pt) )
            compared_feature = compared_featuremap.get_feature( closest_pt )
            distance_map = {}
            shape_map = {}
            size_map = {}
            for pt in reference_featuremap.get_feature_points():
                distance_map[pt] = l2_dist( compared_feature,
                                              reference_featuremap.get_feature(pt) )
                shape_map[pt] = reference_featuremap.get_shape(pt)
                size_map[pt] = reference_featuremap.get_size(pt)
            
            knn = compute_knn( distance_map.keys(), lambda pt: distance_map[pt], 20 )
            reference_window.set_knn( knn  )
            reference_window.set_distance_map( distance_map )
            reference_window.set_shape_map( shape_map )
            reference_window.set_size_map( size_map )
            compare_window.update_nn = False