File size: 892 Bytes
7c68c4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- coding: utf-8 -*-
"""Kraken utils for OCR/HTR engine"""

import streamlit as st

from kraken.lib import (vgsl,
                        models)
from kraken import (blla,
                    rpred)
from PIL import Image

@st.cache_data(show_spinner=False)
def load_model_seg(model_path: str) -> vgsl.TorchVGSLModel:
    """Load a segmentation model"""
    return vgsl.TorchVGSLModel.load_model(model_path)

@st.cache_data(show_spinner=False)
def load_model_rec(model_path: str):
    """Load a recognition model"""
    return models.load_any(model_path)


def segment_image(image: Image, model_seg: vgsl.TorchVGSLModel):
    """Segment an image"""
    return blla.segment(image, model=model_seg)


def recognize_text(model, image: Image, baseline_seg):
    """Recognize text in an image"""
    return rpred.rpred(network=model, im=image, bounds=baseline_seg, pad=16, bidi_reordering=True)