示例#1
0
 def test_filename(self):
     predictor = LinePredictor(IamLinesDataset)
     for filename in SUPPORT_DIRNAME.glob('*.png'):
         pred, conf = predictor.predict(str(filename))
         true = filename.stem
         edit_distance = editdistance.eval(pred, true) / len(pred)
         print(f'Pred: "{pred}" | Confidence: {conf} | True: {true} | Edit distance: {edit_distance}')
示例#2
0
    def test_filename(self):
        predictor = LinePredictor()

        for filename in (SUPPORT_DIRNAME / 'emnist_lines').glob('*.png'):
            pred, conf = predictor.predict(str(filename))
            true = str(filename.stem)
            edit_distance = editdistance.eval(pred, true) / len(pred)
            print(f'Pred: "{pred}" | Confidence: {conf} | True: {true} | Edit distance: {edit_distance}')
            self.assertLess(edit_distance, 0.2)
示例#3
0
def predict():
    """Provide main prediction API route. Responds to both GET and POST requests."""
    K.clear_session()
    predictor = LinePredictor()
    image = _load_image()
    pred, conf = predictor.predict(image)
    print("METRIC confidence {}".format(conf))
    print("METRIC mean_intensity {}".format(image.mean()))
    print("INFO pred {}".format(pred))
    return jsonify({"pred": str(pred), "conf": float(conf)})
示例#4
0
    def test_filename(self):  # pylint: disable=R0201
        predictor = LinePredictor(IamLinesDataset)

        for filename in (SUPPORT_DIRNAME / 'iam_lines').glob('*.png'):
            pred, conf = predictor.predict(str(filename))
            true = filename.stem
            if pred:
                edit_distance = editdistance.eval(pred, true) / len(pred)
            else:
                edit_distance = 0
            print(f'Pred: "{pred}" | Confidence: {conf} | True: {true} | Edit distance: {edit_distance}')
    def test_filename(self):
        """Test that LinePredictor correctly predicts on single images, for several test images."""
        predictor = LinePredictor()

        for filename in (SUPPORT_DIRNAME / "emnist_lines").glob("*.png"):
            pred, conf = predictor.predict(str(filename))
            true = str(filename.stem)
            edit_distance = editdistance.eval(pred, true) / len(pred)
            print(
                f'Pred: "{pred}" | Confidence: {conf} | True: {true} | Edit distance: {edit_distance}'
            )
            self.assertLess(edit_distance, 0.2)
示例#6
0
 def test_filename(self):
     predictor = LinePredictor()
     for filename in SUPPORT_DIRNAME.glob('*.png'):
         image = util.read_image(str(filename), grayscale=True)
         print('Saved image shape:', image.shape)
         image = image[:, :-np.random.randint(1, 150)]  # pylint: disable=invalid-unary-operand-type
         print('Randomly cropped image shape:', image.shape)
         pred, conf = predictor.predict(image)
         true = str(filename.stem)
         edit_distance = editdistance.eval(pred, true) / len(pred)
         print(f'Pred: "{pred}" | Confidence: {conf} | True: {true} | Edit distance: {edit_distance}')
         self.assertLess(edit_distance, 0.2)
示例#7
0
    def test_evaluate(self):
        predictor = LinePredictor(IamLinesDataset)
        dataset = IamLinesDataset()

        dataset.load_or_generate_data()

        t = time()
        metric = predictor.evaluate(dataset)
        time_taken = time() - t

        print(f'acc: {metric}, time_taken: {time_taken}')
        self.assertGreater(metric, 0.6)
        self.assertLess(time_taken, 90)
    def test_filename(self):  # pylint: disable=R0201
        """Test that LinePredictor correctly predicts on single images, for several test images."""
        predictor = LinePredictor(IamLinesDataset)

        for filename in (SUPPORT_DIRNAME / "iam_lines").glob("*.png"):
            pred, conf = predictor.predict(str(filename))
            true = filename.stem
            if pred:
                edit_distance = editdistance.eval(pred, true) / len(pred)
            else:
                edit_distance = 0
            print(
                f'Pred: "{pred}" | Confidence: {conf} | True: {true} | Edit distance: {edit_distance}'
            )
示例#9
0
def load_model_to_app():
    """Instantiate tensorflow session and load model."""
    # NOTE: following https://github.com/keras-team/keras/issues/2397#issuecomment-519128406
    app.session = tf.Session(graph=tf.Graph())
    with app.session.graph.as_default():
        backend.set_session(app.session)
        app.predictor = LinePredictor()
示例#10
0
    def test_evaluate(self):
        # predictor = LinePredictor(IamLinesDataset)
        # dataset = IamLinesDataset()
        #instantiates the line predictor
        predictor = LinePredictor(EmnistLinesDataset)
        #load dataset
        dataset = EmnistLinesDataset()

        dataset.load_or_generate_data()

        t = time()
        #evaluate on the dataset
        metric = predictor.evaluate(dataset)
        time_taken = time() - t

        print(f'acc: {metric}, time_taken: {time_taken}')
        #threshold values for expected performance
        self.assertGreater(metric, 0.8)
        self.assertLess(time_taken, 60)
示例#11
0
import tempfile

import flask
from flask import Flask, request, jsonify
import numpy as np
from tensorflow.keras import backend

from text_recognizer.line_predictor import LinePredictor
from text_recognizer.datasets import IamLinesDataset
import text_recognizer.util as util

app = Flask(__name__)

# Tensorflow bug: https://github.com/keras-team/keras/issues/2397
with backend.get_session().graph.as_default() as g:
    predictor = LinePredictor()
    # predictor = LinePredictor(dataset_cls=IamLinesDataset)


@app.route('/')
def index():
    return 'Hello, world!'


@app.route('/v1/predict', methods=['GET', 'POST'])
def predict():
    image = _load_image()
    with backend.get_session().graph.as_default() as g:
        pred, conf = predictor.predict(image)
    # LOG SOME IMAGE STATISTIC
    # LOG CONFIDENCE
    import unzip_requirements  # pylint: disable=unused-import
except ImportError:
    pass

from flask import Flask, request, jsonify
from tensorflow.keras import backend

from text_recognizer.line_predictor import LinePredictor
# from text_recognizer.datasets import IamLinesDataset
import text_recognizer.util as util

app = Flask(__name__)  # pylint: disable=invalid-name

# Tensorflow bug: https://github.com/keras-team/keras/issues/2397
with backend.get_session().graph.as_default() as _:
    predictor = LinePredictor()  # pylint: disable=invalid-name
    # predictor = LinePredictor(dataset_cls=IamLinesDataset)


@app.route('/')
def index():
    return 'Hello, world!'


@app.route('/v1/predict', methods=['GET', 'POST'])
def predict():
    image = _load_image()
    with backend.get_session().graph.as_default() as _:
        pred, conf = predictor.predict(image)
        print("METRIC confidence {}".format(conf))
        print("METRIC mean_intensity {}".format(image.mean()))
示例#13
0
 def test_evaluate_accuracy(self, set_leaderboard_value=None):
     predictor = LinePredictor()
     dataset = EmnistLinesDataset()
     dataset.load_or_generate_data()
     metric = predictor.evaluate(dataset)
     set_leaderboard_value(metric)