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
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
fnames = all_files 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" 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)
if (idx % 1500 == 0 or idx == 36707): if (idx > 34500): all_features = extractSift(all_files) #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, all_files, all_word_histgrams, dataset_path + 'img_features' + str((idx - 1) / 1500 + 1) + '.txt') all_files = [] all_files_labels = {} all_features = {} #print "---------------------"
HISTOGRAMS_FILE = 'visual_words_for_test_data' CODEBOOK_FILE = 'codebook_b.file' IMAGE_INDEX_FILE = 'image_indexe_test' def parse_arguments(): parser = argparse.ArgumentParser(description='generate visual words histogram for test image') parser.add_argument('-c', help='path to the codebook file', required=False, default=CODEBOOK_FILE) parser.add_argument('input_image', help='path to input image', nargs='+') args = parser.parse_args() return args #print "## extract Sift features" all_files = [] all_features = {} args = parse_arguments() codebook_file = args.c fnames = args.input_image all_features = extractSift(fnames) with open(codebook_file, 'rb') as f: codebook = load(f) word_histgram = computeHistograms(codebook, all_features[fnames[0]]) nclusters = codebook.shape[0] result = [x for x in word_histgram] print result