Ejemplo n.º 1
0
def test_func(img_file_name):
    print("---------------------")
    print("## extract Sift features")
    all_files = []
    all_files_labels = {}
    all_features = {}
    '''args = parse_arguments()
    model_file = args.m
    codebook_file = args.c
    fnames = args.input_images'''
    model_file = 'traintrainingdata.svm.model'
    codebook_file = 'traincodebook.file'
    fnames = []
    fnames.append(img_file_name)
    all_features = extractSift(fnames)
    for i in fnames:
        all_files_labels[i] = 0  # label is unknown

    print("---------------------")
    print("## loading codebook from " + codebook_file)
    with open(codebook_file, 'rb') as f:
        codebook = load(f)

    print("---------------------")
    print("## computing visual word histograms")
    all_word_histgrams = {}
    for imagefname in all_features:
        word_histgram = computeHistograms(codebook, all_features[imagefname])
        all_word_histgrams[imagefname] = word_histgram

    print("---------------------")
    print("## write the histograms to file to pass it to the svm")
    nclusters = codebook.shape[0]
    writeHistogramsToFile(nclusters, all_files_labels, fnames,
                          all_word_histgrams, HISTOGRAMS_FILE)

    print("---------------------")
    print("## test data with svm")
    result = libsvm.test(HISTOGRAMS_FILE, model_file)
    print(result)
    return result
Ejemplo n.º 2
0
def predict_image(model_file, codebook_file, fnames):
    print str(fnames[0]) + ".sift"
    try:
        os.remove(str(fnames[0]) + ".sift")
    except OSError:
        pass
    # extract Sift features for each individual image"
    all_files = []
    all_files_labels = {}
    all_features = {}

    print fnames
    try:
        all_features = extractSift(fnames)
        for i in fnames:
            all_files_labels[i] = 0  # label is unknown

        # loading codebook from codebook_file
        # default codebookfile: codebook.file
        with open(codebook_file, 'rb') as f:
            codebook = load(f)

        # computing visual word histograms"
        all_word_histgrams = {}
        for imagefname in all_features:
            word_histgram = computeHistograms(codebook,
                                              all_features[imagefname])
            all_word_histgrams[imagefname] = word_histgram

        # write the histograms to file to pass it to the svm
        nclusters = codebook.shape[0]
        writeHistogramsToFile(nclusters, all_files_labels, fnames,
                              all_word_histgrams, HISTOGRAMS_FILE)

        # test data with svm"
        print libsvm.test(HISTOGRAMS_FILE, model_file)
    except Exception as e:
        pass  #Incase of error with sift extraction, just try again on next stroke
Ejemplo n.º 3
0
    for i in fnames:
        all_files_labels[i] = 0  # label is unknown

    print "---------------------"
    print "## loading codebook from " + codebook_file
    with open(codebook_file, 'rb') as f:
        codebook = load(f)

    print "---------------------"
    print "## computing visual word histograms"
    all_word_histgrams = {}
    for imagefname in all_features:
        word_histgram = computeHistograms(codebook, all_features[imagefname])
        all_word_histgrams[imagefname] = word_histgram

    print "---------------------"
    print "## write the histograms to file to pass it to the svm"
    print all_word_histgrams.keys()
    nclusters = codebook.shape[0]
    writeHistogramsToFile(nclusters,
                          all_files_labels,
                          fnames,
                          all_word_histgrams,
                          datasetpath + str(set) + '.' + HISTOGRAMS_FILE)

    print "---------------------"
    print "## test data with svm"
    accuracy = libsvm.test(datasetpath + str(set) + '.' + HISTOGRAMS_FILE, model_file)
    print accuracy
Ejemplo n.º 4
0
with open(codebook_file, 'rb') as f:
    codebook = load(f)

print "---------------------"
print "## computing visual word histograms"
all_word_histgrams = {}
for imagefname in all_features:
    word_histgram = computeHistograms(codebook, all_features[imagefname])
    all_word_histgrams[imagefname] = word_histgram

print "---------------------"
print "## write the histograms to file to pass it to the svm"
nclusters = codebook.shape[0]
writeHistogramsToFile(nclusters,
                      all_files_labels,
                      fnames,
                      all_word_histgrams,
                      HISTOGRAMS_FILE)

print 'loading data file'
data_file = loadtxt(HISTOGRAMS_FILE)
#print data_file, type(data_file)
#classID = data_file[:,0].astype(int)
classID = data_file[0].astype(int)
#Features = np.delete(data_file, 0,1)

features = data_file[1:]
features = features.reshape(1, (features.shape[0]))
#print classID, features
#sys.exit()
Ejemplo n.º 5
0
            #    all_files_labels[i] = 0  # label is unknown

            print "---------------------"
            print "## loading codebook from " + codebook_file
            with open(codebook_file, 'rb') as f:
                codebook = load(f)

            print "---------------------"
            print "## computing visual word histograms"
            all_word_histgrams = {}
            for imagefname in all_features:
                word_histgram = computeHistograms(codebook,
                                                  all_features[imagefname])
                all_word_histgrams[imagefname] = word_histgram

            print "---------------------"
            print "## write the histograms to file to pass it to the svm"
            nclusters = codebook.shape[0]
            writeHistogramsToFile(
                nclusters, all_files_labels, all_files, all_word_histgrams,
                dataset_path + 'img_features' + str((idx - 1) / 1500 + 1) +
                '.txt')

        all_files = []
        all_files_labels = {}
        all_features = {}

#print "---------------------"
#print "## test data with svm"
#print libsvm.test(HISTOGRAMS_FILE, model_file)
Ejemplo n.º 6
0
    with open(codebook_file, 'rb') as f:
        codebook = load(f)

    print "---------------------"
    print "## computing visual word histograms"
    all_word_histgrams = {}
    for imagefname in all_features:
        word_histgram = computeHistograms(codebook, all_features[imagefname])
        all_word_histgrams[imagefname] = word_histgram

    print "---------------------"
    print "## write the histograms to file to pass it to the svm"
    nclusters = codebook.shape[0]
    writeHistogramsToFile(nclusters,
                          all_files_labels,
                          fnames,
                          all_word_histgrams,
                          HISTOGRAMS_FILE)

    print "---------------------"
    print "## test data with svm"
    hold=libsvm.test(HISTOGRAMS_FILE, model_file)
    vl=hold[0]
    val.append(vl)
    print hold

print farr
print val
cm=np.zeros(shape=(26,26))
# confusion matrix
ct=0
Ejemplo n.º 7
0
args = parse_arguments()
codebook_file = args.c
fnames = args.input_image
all_features = extractSift(fnames)

print "---------------------"
print "## loading codebook from " + codebook_file
with open(codebook_file, 'rb') as f:
    codebook = load(f)

print "---------------------"
print "## computing visual word histograms"
all_word_histgrams = {}
for imagefname in all_features:
    word_histgram = computeHistograms(codebook, all_features[imagefname])
    all_word_histgrams[imagefname] = word_histgram

print "---------------------"
print "## write the histograms to file"
nclusters = codebook.shape[0]
writeHistogramsToFile(nclusters,
                      fnames,
                      all_word_histgrams,
                      IMAGE_INDEX_FILE,
                      HISTOGRAMS_FILE)

print "---------------------"
print "finished"

Ejemplo n.º 8
0
    print("---------------------")
    print("## write the histograms to file to pass it to the svm")
    nclusters = codebook.shape[0]
    writeHistogramsToFile(nclusters, all_files_labels, fnames,
                          all_word_histgrams, HISTOGRAMS_FILE)

    print("---------------------")
    print("## test data with svm")
    result = libsvm.test(HISTOGRAMS_FILE, model_file)
    print(result)
    return result


#print (test_func('D:/python/MinimalBOW/test/test.jpg'))
'''
print "---------------------"
print "## extract Sift features"
all_files = []
all_files_labels = {}
all_features = {}

args = parse_arguments()
model_file = args.m
codebook_file = args.c
fnames = args.input_images
print fnames
all_features = extractSift(fnames)
for i in fnames:
    all_files_labels[i] = 0  # label is unknown