Example #1
0
def main(argv):

    # load image
    print('loading image:', argv)
    image = io.imread(argv, as_gray=True)

    # split into letters
    letters = split_letters(image)

    probs = []
    for i, letter in enumerate(letters):
        letter = letter.astype('float32') / 255
        predict_input_fn = tf.estimator.inputs.numpy_input_fn(x={'x': letter},
                                                              y=None,
                                                              num_epochs=1,
                                                              shuffle=False)
        predictions = captcha_classifier.predict(input_fn=predict_input_fn)
        for pred_dict in predictions:
            class_id = pred_dict['classes']
            probs.append(str(class_id))
    print(''.join(probs))
Example #2
0
def main(argv):
    args = parser.parse_args(argv[1:])
    if args.fname == None:
        print('Capture Image is missing!')
        return None

    # load image
    print('loading image:', args.fname)
    image = io.imread(args.fname)

    # split into letters
    letters = split_letters(image)
    if letters == None:
        print('Letters is not valid')
        return None

    # predict
    content = ''
    probs = []
    for i, letter in enumerate(letters):
        letter = letter.astype('float32') / 255
        predict_input_fn = tf.estimator.inputs.numpy_input_fn(x={'x': letter},
                                                              y=None,
                                                              num_epochs=1,
                                                              shuffle=False)
        predictions = captcha_classifier.predict(input_fn=predict_input_fn)
        for pred_dict in predictions:
            class_id = pred_dict['classes']
            prob = pred_dict['probabilities'][class_id]
            class_name = chr(65 + class_id)  # from 65 : 'A'
            print('predict for {} char: `{}` with probability: {:.3f}%'.format(
                i + 1, class_name, prob * 100))
            content += class_name
            probs.append(prob)
    print('Captcha: `{}` with confident: `{:.3f}%`'.format(
        content, 100 * sum(probs) / len(probs)))
    accuracy = str(round(100 * sum(probs) / len(probs), 2))
    print('ACCURACY-' + accuracy)
    result = 'RESULT-' + content
    print(result)
# load image content json file
with open(DATA_MAP) as f:
    image_contents = json.load(f)

# load image and save letters
counter = 0
failCounter = 0
for fname, contents in image_contents.iteritems():
    counter += 1
    image = io.imread(os.path.join(DATA_FULL_DIR, fname))

    # split image
    letters = []

    try:
        letters = split_letters(image, debug=True)
    except:
        print('split_letters failed')
        failCounter += 1
    else:
        if letters != None:
            fname = fname.replace('.jpg', '.png')
            print(counter)
            for i, letter in enumerate(letters):
                content = contents[i]
                # add to dataset
                data_x.append(letter)
                data_y.append(np.uint8(ord(content) - 48))  # 48: '0'

                # save letter into train folder
                fpath = os.path.join(DATA_TRAIN_DIR, content)
# build image contents map
image_contents = build_data_map(DATA_FULL_DIR)

# load image and save letters
counter = 0

for fname, contents in image_contents.iteritems():
    counter += 1
    print(counter, fname, contents)
    original_image = io.imread(os.path.join(DATA_FULL_DIR, fname))
    grayscale_image = rgb2gray(original_image)

    # split image
    letters = split_letters(grayscale_image,
                            num_letters=len(contents),
                            debug=True)
    if letters != None:
        for i, letter in enumerate(letters):
            content = contents[i]
            # add to dataset
            data_x.append(letter)
            data_y.append(np.uint8(ord(content) - 65))  # 65: 'A'

            # save letter into train folder
            fpath = os.path.join(DATA_TRAIN_DIR, content)
            if not os.path.exists(fpath):
                os.makedirs(fpath)
            letter_fname = os.path.join(
                fpath,
                str(i + 1) + '-' + strip_extension(fname) + '.png')