Felixogunwale commited on
Commit
fdc30cd
·
1 Parent(s): d670fe1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import PIL
2
+ from PIL import ImageDraw
3
+ from PIL import Image
4
+ import streamlit as st
5
+ import os
6
+
7
+
8
+ def load_image(image_file):
9
+ img = PIL.Image.open(image_file)
10
+ return img
11
+
12
+ def init_session_states():
13
+ if 'disp' not in st.session_state:
14
+ st.session_state['disp'] = st.empty()
15
+ st.session_state['disp'].text("Setting up environment with latest build of easyocr. This will take about a minute ")
16
+ if 'init' not in st.session_state:
17
+ st.session_state['init'] = 1
18
+
19
+ # Not required as they are already installed through requirements and also seems to cause errors
20
+ # os.system('pip install git+git://github.com/jaidedai/easyocr.git')
21
+ # os.system('pip install git+https://github.com/huggingface/transformers.git --upgrade')
22
+
23
+
24
+
25
+ init_session_states()
26
+ import easyocr
27
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
28
+
29
+ def text_recognition(image):
30
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
31
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
32
+ #processor = TrOCRProcessor.from_pretrained("microsoft/trocr-large-handwritten")
33
+ #model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-large-handwritten")
34
+
35
+ pixel_values = processor(image, return_tensors="pt").pixel_values
36
+ generated_ids = model.generate(pixel_values)
37
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
38
+ st.write(generated_text)
39
+
40
+ def main():
41
+
42
+ st.session_state['disp'].text("Env setup up Complete")
43
+ uploaded_file = st.file_uploader("Choose image file to detect text",type=['jpeg','jpg'])
44
+ if uploaded_file is not None:
45
+ file_details = {"FileName":uploaded_file.name,"FileType":uploaded_file.type,"FileSize":uploaded_file.size}
46
+ st.write(file_details)
47
+ image = load_image(uploaded_file)
48
+ st.image(image,width=500)
49
+ st.write("Detecting text bounding box and Take 1 recognition...")
50
+ reader = easyocr.Reader(['en'],gpu=True)
51
+ bound = reader.readtext(image)
52
+ st.write("Bounding box Detection complete")
53
+ st.write(str(bound))
54
+ st.write("Recognizing text - Take 2....")
55
+ text_recognition(image)
56
+
57
+
58
+
59
+ if __name__ == "__main__":
60
+ main()
61
+
62
+
63
+
64
+