Пример #1
0
def test_filename():  # pylint: disable=R0201
    predictor = ParagraphTextRecognizer()
    num_text_lines_by_name = {"a01-000u-cropped": 7}
    for filename in (SUPPORT_DIRNAME).glob("*.jpg"):
        full_image = util.read_image(str(filename), grayscale=True)
        predicted_text, line_region_crops = predictor.predict(full_image)
        print(filename, ':', predicted_text)
 def test_filename(self):
     predictor = ParagraphTextRecognizer()
     num_text_lines_by_name = {'a01-000u-cropped': 7}
     for filename in (SUPPORT_DIRNAME).glob('*.jpg'):
         full_image = util.read_image(str(filename), grayscale=True)
         predicted_text, line_region_crops = predictor.predict(full_image)
         print(f"ouptut text {predicted_text}")
         assert len(line_region_crops) == num_text_lines_by_name[
             filename.stem]
Пример #3
0
def predict():
    """Provide main prediction API route. Responds to both GET and POST requests."""
    K.clear_session()
    predictor = ParagraphTextRecognizer()
    image = _load_image()
    pred, line_region_crops = predictor.predict(image)
    print("INFO line_region_crops {}".format(line_region_crops))
    print("METRIC mean_intensity {}".format(image.mean()))
    print("INFO pred {}".format(pred))
    return jsonify({"pred": str(pred)})
Пример #4
0
def test_paragraph_text_recognizer():
    """Test ParagraphTextRecognizer."""
    support_filenames = list(_SUPPORT_DIRNAME.glob("*.png"))
    with open(_SUPPORT_DIRNAME / "data_by_file_id.json", "r") as f:
        support_data_by_file_id = json.load(f)

    start_time = time.time()
    text_recognizer = ParagraphTextRecognizer()
    end_time = time.time()
    print(
        f"Time taken to initialize ParagraphTextRecognizer: {round(end_time - start_time, 2)}s"
    )

    for i, support_filename in enumerate(support_filenames):
        if i >= _NUM_MAX_SAMPLES:
            break
        expected_text = support_data_by_file_id[
            support_filename.stem]["predicted_text"]
        start_time = time.time()
        predicted_text = _test_paragraph_text_recognizer(
            support_filename, expected_text, text_recognizer)
        end_time = time.time()
        time_taken = round(end_time - start_time, 2)

        cer = _character_error_rate(
            support_data_by_file_id[support_filename.stem]
            ["ground_truth_text"], predicted_text)
        print(
            f"Character error rate is {round(cer, 3)} for file {support_filename.name} (time taken: {time_taken}s)"
        )
    def test_evaluate(self):
        dataset = IAMParagraphs(
            argparse.Namespace(batch_size=16, num_workers=10))
        dataset.prepare_data()
        dataset.setup()

        text_recog = ParagraphTextRecognizer()
        trainer = pl.Trainer(gpus=1)

        start_time = time.time()
        metrics = trainer.test(text_recog.lit_model, datamodule=dataset)
        end_time = time.time()

        test_cer = round(metrics[0]["test_cer"], 2)
        time_taken = round((end_time - start_time) / 60, 2)

        print(f"Character error rate: {test_cer}, time_taken: {time_taken} m")
        self.assertEqual(test_cer, _TEST_CHARACTER_ERROR_RATE)
        self.assertLess(time_taken, 45)
def _test_paragraph_text_recognizer(image_filename: Path, expected_text: str):
    """Test ParagraphTextRecognizer on 1 image."""
    text_recognizer = ParagraphTextRecognizer()
    predicted_text = text_recognizer.predict(image_filename)
    assert predicted_text == expected_text, f"predicted text does not match expected for {image_filename.name}"
    return predicted_text
Пример #7
0
"""AWS Lambda function serving text_recognizer predictions."""
from PIL import ImageStat

from text_recognizer.paragraph_text_recognizer import ParagraphTextRecognizer
import text_recognizer.util as util

model = ParagraphTextRecognizer()


def handler(event, _context):
    """Provide main prediction API"""
    image = _load_image(event)
    pred = model.predict(image)
    image_stat = ImageStat.Stat(image)
    print("METRIC image_mean_intensity {}".format(image_stat.mean[0]))
    print("METRIC image_area {}".format(image.size[0] * image.size[1]))
    print("METRIC pred_length {}".format(len(pred)))
    print("INFO pred {}".format(pred))
    return {"pred": str(pred)}


def _load_image(event):
    image_url = event.get("image_url")
    if image_url is None:
        return "no image_url provided in event"
    print("INFO url {}".format(image_url))
    return util.read_image_pil(image_url, grayscale=True)
Пример #8
0
try:
    import unzip_requirements  # pylint: disable=unused-import
except ImportError:
    pass

from flask import Flask, request, jsonify

from text_recognizer.line_predictor import LinePredictor
from text_recognizer.paragraph_text_recognizer import ParagraphTextRecognizer
import text_recognizer.util as util

app = Flask(__name__)

# init predictor
predictor = LinePredictor()
text_ocr = ParagraphTextRecognizer()


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


@app.route('/v1/predict', methods=['GET', 'POST'])
def predict():
    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)})