File size: 2,183 Bytes
0c830e5
 
50cf66b
0c830e5
3d64db0
fcc8fb8
f65d837
5137c86
 
 
 
fcc8fb8
bf2871b
fcc8fb8
a7c28ef
f437a4a
 
559d627
ea4455d
0c830e5
78ea23b
3d64db0
0cecbda
 
ea4455d
 
f1eee64
 
 
 
 
9c2f222
f1eee64
 
 
 
fcc8fb8
 
0cecbda
44b3ae9
fcc8fb8
 
 
 
 
b3481c4
c5ff8d8
fcc8fb8
 
f1eee64
fcc8fb8
c5ff8d8
f1eee64
fcc8fb8
3d64db0
66265cc
9c48477
 
50cf66b
4352add
27e3c48
0c830e5
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import PIL 
from PIL import ImageDraw
from PIL import Image
import streamlit as st
import os


def load_image(image_file):
	img = PIL.Image.open(image_file)
	return img

def init_session_states():
  if 'disp' not in st.session_state:
    st.session_state['disp'] = st.empty()
    st.session_state['disp'].text("Setting up environment with latest build of easyocr. This will take about a minute ")
  if 'init' not in st.session_state:
    st.session_state['init'] = 1
    os.system('pip install git+git://github.com/jaidedai/easyocr.git')
    os.system('pip install git+https://github.com/huggingface/transformers.git --upgrade')



init_session_states()
import easyocr
from transformers import TrOCRProcessor, VisionEncoderDecoderModel

def text_recognition(image):
    processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten") 
    model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
    #processor = TrOCRProcessor.from_pretrained("microsoft/trocr-large-handwritten") 
    #model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-large-handwritten")

    pixel_values = processor(image, return_tensors="pt").pixel_values 
    generated_ids = model.generate(pixel_values)
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] 
    st.write(generated_text)

def main():
   
    st.session_state['disp'].text("Env setup up Complete")
    uploaded_file = st.file_uploader("Choose image file to detect text",type=['jpeg','jpg'])
    if uploaded_file is not None:
        file_details = {"FileName":uploaded_file.name,"FileType":uploaded_file.type,"FileSize":uploaded_file.size}
        st.write(file_details)
        image = load_image(uploaded_file)
        st.image(image,width=500)
        st.write("Detecting text bounding box and Take 1 recognition...")
        reader = easyocr.Reader(['en'],gpu=True)
        bound = reader.readtext(image)
        st.write("Bounding box Detection complete")
        st.write(str(bound))
        st.write("Recognizing text - Take 2....")
        text_recognition(image)
   
   
 
if __name__ == "__main__":
    main()