Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo 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
codebook_file = args.c
fnames = args.input_images
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"
print(libsvm.test(HISTOGRAMS_FILE, model_file))
Exemplo n.º 5
0
if HISTOGRAMS_FILE == "hinditestdata.svm":
  dest1 = 'hindiwords/'



def copyFile(src, dest):
    try:
        shutil.copy(src, dest)

    except shutil.Error as e:
        parserrint('Error: %s' % e)

    except IOError as e:
        print('Error: %s' % e.strerror)

kanmisclass = []
engmisclass = []

count = 0


for index,i in enumerate(libsvm.test(HISTOGRAMS_FILE,model_file)):
  #print 'File:',filenames[index]
  count += 1
  print count
  if i == 0 :
    copyFile(filenames[index],dest1)
    os.remove(filenames[index])
  else:
    continue
Exemplo n.º 6
0
    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
# dct={'a' : 0,'b':1,'c':2,'d':3,'e':4,'f':5,'g':6,'h':7,'i':8,'j':9,'k':10,'l':11,'m':12,'n':13,'o':14,'p':15,'q':16,'r':17,'s':18,'t':19,'u':20,'v':21,'w':22,'x':23,'y':24,'z':25}
dct=pickle.load(open("dict1.p","rb"))
for item in farr:
  # print item[0]
  c=ord(item[0])-97
  col=ord(dct.keys()[dct.values().index(val[ct])])-97
Exemplo n.º 7
0
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"
resultLabel = libsvm.test(HISTOGRAMS_FILE, model_file)
print ('THIS IS THE LABEL')
ingList = open(r"C:\Users\Hannah Chen\Documents\Visual Studio 2013\Projects\ColorBasics-WPF\bin\Debug\query.txt", "a")
#url = "http://www.recipepuppy.com/?i="
res = resultLabel
print res
l = ['0','1','2','3','4','5','6','7','8','9']
st_res = 0
for ch in res:
 if ch not in l:
  st_res+=ch
with open('nameList.txt', 'r') as f:
    count = 0;
    for item in f:
        print "COUNT IS: "
        print count
Exemplo n.º 8
0
args = parse_arguments()
model_file = args.m
codebook_file = args.c
fnames = args.input_images
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"
print libsvm.test(HISTOGRAMS_FILE, model_file)
Exemplo n.º 9
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"
results = libsvm.test(HISTOGRAMS_FILE, model_file)
print results

print "Length of filenames : ", len(fnames)

f = open("filenames.txt", "w")
f.write("\n".join(fnames)+"\n")
f.close()