Exemplo n.º 1
0
def test_se_tardata_sudokus(file_path_base):
    try:
        image_file_name = file_path_base + '.jpg'
        image = load_image(image_file_name)
    except:
        image_file_name = file_path_base + '.png'
        image = load_image(image_file_name)
    with open(file_path_base + '.txt', 'rt') as f:
        correct_parsing = f.read().strip()

    predictions, sudoku, subimage = extract_sudoku(image,
                                                   classifier(),
                                                   force=True)
    parsed_sudoku = predictions_to_suduko_string(predictions, oneliner=False)
    if parsed_sudoku != correct_parsing:
        predictions, sudoku, subimage = extract_sudoku(image.rotate(-90),
                                                       classifier(),
                                                       force=True)
        parsed_sudoku = predictions_to_suduko_string(predictions,
                                                     oneliner=False)
    assert parsed_sudoku == correct_parsing

    try:
        os.remove(image_file_name)
        os.remove(file_path_base + '.txt')
        print("Deleted temporary test dir.")
        os.removedirs(os.path.dirname(file_path_base))
    except:
        pass
Exemplo n.º 2
0
def test_se_tardata_sudokus(file_path_base):
    try:
        image_file_name = file_path_base + '.jpg'
        image = load_image(image_file_name)
    except:
        image_file_name = file_path_base + '.png'
        image = load_image(image_file_name)
    with open(file_path_base + '.txt', 'rt') as f:
        correct_parsing = f.read().strip()

    predictions, sudoku, subimage = extract_sudoku(image, classifier(), force=True)
    parsed_sudoku = predictions_to_suduko_string(predictions, oneliner=False)
    if parsed_sudoku != correct_parsing:
        predictions, sudoku, subimage = extract_sudoku(image.rotate(-90), classifier(), force=True)
        parsed_sudoku = predictions_to_suduko_string(predictions, oneliner=False)
    assert parsed_sudoku == correct_parsing


    try:
        os.remove(image_file_name)
        os.remove(file_path_base + '.txt')
        print("Deleted temporary test dir.")
        os.removedirs(os.path.dirname(file_path_base))
    except:
        pass
Exemplo n.º 3
0
def test_xanadoku_sudokus(sudoku_doc):
    if not sudoku_doc.get('verified'):
        assert True
    else:
        print(sudoku_doc.get('_id'))
        image = download_image(sudoku_doc.get('raw_image_url'))
        predictions, sudoku, subimage = extract_sudoku(image, classifier(), force=True)
        parsed_sudoku = predictions_to_suduko_string(predictions, oneliner=True)
        if parsed_sudoku != sudoku_doc.get('parsed_sudoku'):
            predictions, sudoku, subimage = extract_sudoku(image.rotate(-90), classifier(), force=True)
            parsed_sudoku = predictions_to_suduko_string(predictions, oneliner=True)
        assert parsed_sudoku == sudoku_doc.get('parsed_sudoku')
Exemplo n.º 4
0
def test_image_1_with_trained_classifier():
    image = _get_img(1)
    c = fit.KNeighborsClassifier(n_neighbors=5)
    c = fit.fit_sudokuextract_classifier(c)
    predictions, sudoku, subimage = extract_sudoku(image, c)
    parsed_sudoku = predictions_to_suduko_string(predictions)
    assert parsed_sudoku == _get_parsed_img(1)
Exemplo n.º 5
0
def test_image_2_with_trained_classifier():
    image = _get_img(2)
    c = fit.KNeighborsClassifier(n_neighbors=10)
    c = fit.fit_combined_classifier(c)
    predictions, sudoku, subimage = extract_sudoku(image, c)
    parsed_sudoku = predictions_to_suduko_string(predictions)
    assert parsed_sudoku == _get_parsed_img(2)
Exemplo n.º 6
0
def extraction_method_map(image,
                          classifier,
                          use_local_thresholding=False,
                          apply_gaussian=False,
                          n=5,
                          force=False):
    for sudoku, subimage in _extraction_iterator_map(image,
                                                     use_local_thresholding,
                                                     apply_gaussian,
                                                     n=n):
        try:
            pred_n_imgs = classify_sudoku(sudoku, classifier)
            preds = np.array([[pred_n_imgs[k][kk][0] for kk in range(9)]
                              for k in range(9)])
            imgs = [[pred_n_imgs[k][kk][1] for kk in range(9)]
                    for k in range(9)]

            if np.sum(preds > 0) >= 17 or force:
                try:
                    s = Sudoku(predictions_to_suduko_string(preds))
                    s.solve()
                except SudokuException:
                    if force:
                        return preds, imgs, subimage
                except Exception:
                    pass
                else:
                    return preds, imgs, subimage
        except Exception as e:
            pass
    raise SudokuExtractError(
        "Corner Method could not extract any Sudoku from this image.")
Exemplo n.º 7
0
def test_xanadoku_sudokus(sudoku_doc):
    if not sudoku_doc.get('verified'):
        assert True
    else:
        print(sudoku_doc.get('_id'))
        image = download_image(sudoku_doc.get('raw_image_url'))
        predictions, sudoku, subimage = extract_sudoku(image,
                                                       classifier(),
                                                       force=True)
        parsed_sudoku = predictions_to_suduko_string(predictions,
                                                     oneliner=True)
        if parsed_sudoku != sudoku_doc.get('parsed_sudoku'):
            predictions, sudoku, subimage = extract_sudoku(image.rotate(-90),
                                                           classifier(),
                                                           force=True)
            parsed_sudoku = predictions_to_suduko_string(predictions,
                                                         oneliner=True)
        assert parsed_sudoku == sudoku_doc.get('parsed_sudoku')
Exemplo n.º 8
0
def main():
    import argparse
    parser = argparse.ArgumentParser(description="Running SudokuExtract as a command line tool")
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-p', '--path', dest='path', type=str, default=None, help="Path to an image to parse.")
    group.add_argument('-u', '--url', dest='url', type=str, default=None, help="Url to an image to parse.")
    parser.add_argument('--oneliner', action='store_true', help="Print oneliner solution.")

    args = parser.parse_args()

    if args.path is not None:
        image = load_image(args.path)
    else:
        image = download_image(args.url)

    preds, images, subimage = extract_sudoku(image)
    sudoku_string = predictions_to_suduko_string(preds, args.oneliner)
    print(sudoku_string)
Exemplo n.º 9
0
def extraction_method_map(image, classifier, use_local_thresholding=False, apply_gaussian=False, n=5, force=False):
    for sudoku, subimage in _extraction_iterator_map(image, use_local_thresholding, apply_gaussian, n=n):
        try:
            pred_n_imgs = classify_sudoku(sudoku, classifier)
            preds = np.array([[pred_n_imgs[k][kk][0] for kk in range(9)] for k in range(9)])
            imgs = [[pred_n_imgs[k][kk][1] for kk in range(9)] for k in range(9)]

            if np.sum(preds > 0) >= 17 or force:
                try:
                    s = Sudoku(predictions_to_suduko_string(preds))
                    s.solve()
                except SudokuException:
                    if force:
                        return preds, imgs, subimage
                except Exception:
                    pass
                else:
                    return preds, imgs, subimage
        except Exception as e:
            pass
    raise SudokuExtractError("Corner Method could not extract any Sudoku from this image.")
Exemplo n.º 10
0
def _process_an_image(image, correct_sudoku):
    predictions, sudoku, subimage = extract_sudoku(image, classifier())
    parsed_sudoku = predictions_to_suduko_string(predictions)
    assert parsed_sudoku == correct_sudoku
Exemplo n.º 11
0
def _process_an_image(image, correct_sudoku):
    predictions, sudoku, subimage = extract_sudoku(image, classifier())
    parsed_sudoku = predictions_to_suduko_string(predictions)
    assert parsed_sudoku == correct_sudoku