Esempio n. 1
0
from tools.image import read_rgb_image, resize_rgb_image, flatten
import argparse, os
import numpy as np
from sklearn.neighbors import KNeighborsClassifier

p = argparse.ArgumentParser()
p.add_argument('-i', help = 'Input image.', required = True)
p.add_argument('-d', help = 'Dataset path.', required = True)
p.add_argument('--labels', default = "labels.txt")
p.add_argument('--features', default = "features.txt")
p.add_argument('--labelmapping', default = "map.txt")
p.add_argument('-k', default = 7, type = int)
args = p.parse_args()

print ("Loading model ...")
data = np.loadtxt(os.path.join(args.d, args.features), delimiter = ',') / 255
labels = np.loadtxt(os.path.join(args.d, args.labels))
lines = [i.strip().split(" ") for i in open(os.path.join(args.d, args.labelmapping))]
labelmap = dict([(int(n), l) for l, n in lines])

clf = KNeighborsClassifier(n_neighbors = 7, algorithm = 'brute')
clf.fit(data, labels.ravel())

# load query image, resize to 32x32 and scale to [0, 1]
img = resize_rgb_image(read_rgb_image(args.i), (32, 32))
arr = flatten(img) / 255
arr = np.reshape(arr, (1, -1))
t = clf.predict(arr)
print (labelmap[int(t[0])])
Esempio n. 2
0
args = p.parse_args()


def labels_and_files():
    for i in os.listdir(args.i):
        for j in os.listdir(os.path.join(args.i, i)):
            yield (i, os.path.join(args.i, i, j))


if not os.path.exists(args.o):
    os.makedirs(args.o)

f = open(os.path.join(args.o, args.features), "w")
l = open(os.path.join(args.o, args.labels), "w")

mapping = {}
for lb, fn in labels_and_files():
    if not lb in mapping:
        mapping[lb] = len(mapping)
    c = mapping[lb]
    print(c, lb, fn)
    img = resize_rgb_image(read_rgb_image(fn), (32, 32))
    for i in normed_windows(img, [1.0]):
        d = flatten(i)
        f.write(",".join([str(k) for k in d]) + "\n")
        l.write(str(c) + "\n")

m = open(os.path.join(args.o, args.labelmapping), "w")
for k, v in mapping.items():
    m.write(k + " " + str(v) + "\n")
Esempio n. 3
0
def read_image(fname, row, col):
    i = read_rgb_image(fname)
    assert (i.shape[0] == 300)
    assert (i.shape[1] == 300)
    i = i[row * 100:row * 100 + 100, col * 100:col * 100 + 100]
    return i
Esempio n. 4
0
def read_image(fname, row, col):
	i = read_rgb_image(fname)
	assert(i.shape[0] == 300)
	assert(i.shape[1] == 300)
	i = i[row * 100:row * 100 + 100, col * 100:col * 100 + 100]
	return i
Esempio n. 5
0
p.add_argument('--features', default = "features.txt")
p.add_argument('--labelmapping', default = "map.txt")
args = p.parse_args()

def labels_and_files():
	for i in os.listdir(args.i):
		for j in os.listdir(os.path.join(args.i, i)):
			yield (i, os.path.join(args.i, i, j))

if not os.path.exists(args.o):
	os.makedirs(args.o)

f = open(os.path.join(args.o, args.features), "w")
l = open(os.path.join(args.o, args.labels), "w")

mapping = {}
for lb, fn in labels_and_files():
	if not lb in mapping:
		mapping[lb] = len(mapping)
	c = mapping[lb]
	print (c, lb, fn)
	img = resize_rgb_image(read_rgb_image(fn), (32, 32))
	for i in normed_windows(img, [1.0]):
		d = flatten(i)
		f.write(",".join([str(k) for k in d]) + "\n")
		l.write(str(c) + "\n")

m = open(os.path.join(args.o, args.labelmapping), "w")
for k, v in mapping.items():
	m.write(k + " " + str(v) + "\n")