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
print "---------------------" print "## extract Sift features" all_files = [] all_files_labels = {} all_features = {} for cat_path in files.keys(): cat_files = [cat_path + '/' + name for name in files[cat_path][set][1]] all_files.extend(cat_files) datasetpath = DATASETPATH model_file = datasetpath + str(set) + '.' + MODEL_FILE codebook_file = datasetpath + str(set) + '.' + CODEBOOK_FILE 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
dataset_path = dataset_path[2:len(dataset_path) - 2] codebook_file = dataset_path + 'codebook.file' f = open(dataset_path + 'filename.txt', 'r') idx = 0 for line in f.readlines(): imgfilename = line.split()[0] label = line.split()[1] all_files.append(imgfilename) all_files_labels[imgfilename] = int(label) idx = idx + 1 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
all_files = [] all_files_labels = {} all_features = {} args = parse_arguments() model_file = args.m codebook_file = args.c farr=[] val=[] fol = args.input_images fl=os.listdir(fol[0]) fl.sort() for f in fl: fnames=[fol[0]+'/'+f] farr.append(f[0]) 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
with open(SVM_MODEL_PATH, 'rb') as fid: crf = cPickle.load(fid) print "---------------------" print "## extract Sift features" all_files = [] all_files_labels = {} all_features = {} model_file = SVM_MODEL_PATH codebook_file = CODEBOOK_FILE files = learn.get_imgfiles(DATASETPATH) all_features = extractSift(files) for i in files: 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