return i 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), np.uint8) 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) img = resize_rgb_image(read_rgb_image(args.i), (32, 32)) d = [] for i, details in normed_windows(img, [1.0], details = True): arr = flatten(i) / 255 arr = np.reshape(arr, (1, -1)) t = clf.predict(arr) c = t[0] print (c, labelmap[c]) distances, idx = clf.kneighbors(arr) distances = [d for d, p in zip(distances.flatten(), idx.flatten()) if labels[p] == c] s = sum(distances) / len(distances) d.append([s, c, details]) d = sorted(d) for s, c, details in d[:3]: print (s, labelmap[c], details) img = box(img, details[1], details[2], details[0]) write_rgb_image(args.o, img)
#!/usr/bin/env python3 from tools.image import read_rgb_image, write_rgb_image import argparse import numpy as np from skimage import io p = argparse.ArgumentParser(description = 'Extract an image from a 9x9 reCAPTCHA image.') p.add_argument('-i', help = 'input image', required = True) p.add_argument('-y', help = 'row', required = True, type = int) p.add_argument('-x', help = 'column', required = True, type = int) p.add_argument('-o', help = 'output image', default = None) p.add_argument('-v', help = 'show output image', default = False, action = 'store_true') args = p.parse_args() 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 i = read_image(args.i, args.y, args.x) if args.o != None: write_rgb_image(args.o, i) if args.v: io.imshow(i)
from skimage import io p = argparse.ArgumentParser( description='Extract an image from a 9x9 reCAPTCHA image.') p.add_argument('-i', help='input image', required=True) p.add_argument('-y', help='row', required=True, type=int) p.add_argument('-x', help='column', required=True, type=int) p.add_argument('-o', help='output image', default=None) p.add_argument('-v', help='show output image', default=False, action='store_true') args = p.parse_args() 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 i = read_image(args.i, args.y, args.x) if args.o != None: write_rgb_image(args.o, i) if args.v: io.imshow(i)