May222 commited on
Commit
baf49b9
1 Parent(s): f73d025

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -12
app.py CHANGED
@@ -1,7 +1,8 @@
1
- import gradio as gr
2
  # from transformers import pipeline
3
  from PIL import Image
4
  import pytesseract
 
5
  import cv2
6
  import os
7
 
@@ -11,17 +12,89 @@ import os
11
  # predictions = pipeline(input_img)
12
  # return input_img, {p["label"]: p["score"] for p in predictions}
13
 
14
- def recognize(input_img):
15
- text = pytesseract.image_to_string(Image.open("./data/" + filename))
16
- return input_img, text
17
 
18
- gradio_app = gr.Interface(
19
- recognize,
20
- inputs=[gr.Image(label="Upload an Image", type="pil")],
21
- outputs=[gr.Textbox(label="Text in the Image")],
22
 
23
- title="Extrate Text From Image",
24
- )
25
 
26
- if __name__ == "__main__":
27
- gradio_app.launch(server_port=8756)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import gradio as gr
2
  # from transformers import pipeline
3
  from PIL import Image
4
  import pytesseract
5
+ import easyocr
6
  import cv2
7
  import os
8
 
 
12
  # predictions = pipeline(input_img)
13
  # return input_img, {p["label"]: p["score"] for p in predictions}
14
 
15
+ # def recognize(input_img):
16
+ # text = pytesseract.image_to_string(Image.open("./data/" + filename))
17
+ # return input_img, text
18
 
19
+ # gradio_app = gr.Interface(
20
+ # recognize,
21
+ # inputs=[gr.Image(label="Upload an Image", type="pil")],
22
+ # outputs=[gr.Textbox(label="Text in the Image")],
23
 
24
+ # title="Extrate Text From Image",
25
+ # )
26
 
27
+ # if __name__ == "__main__":
28
+ # gradio_app.launch(server_port=8756)
29
+
30
+
31
+ import os
32
+
33
+ import matplotlib.pyplot as plt
34
+ import streamlit as st
35
+
36
+ import cv2
37
+ import tensorflow as tf
38
+ from PIL import Image
39
+ import pytesseract
40
+
41
+ DET_ARCHS = ["pytesseract", "easyocr"]
42
+
43
+ def main():
44
+
45
+ # Wide mode
46
+ st.set_page_config(layout="wide")
47
+
48
+ # Designing the interface
49
+ st.title("Image Text Recognition")
50
+ # For newline
51
+ st.write('\n')
52
+
53
+ # Instructions
54
+ st.markdown("*Hint: click on the top-right corner of an image to enlarge it!*")
55
+ # Set the columns
56
+ cols = st.columns((1, 1, 1, 1))
57
+ cols[0].subheader("Input image")
58
+ cols[1].subheader("OCR output")
59
+
60
+ # Sidebar
61
+ # File selection
62
+ st.sidebar.title("Document selection")
63
+ # Disabling warning
64
+ # st.set_option('deprecation.showfileUploaderEncoding', False)
65
+ # Choose your own image
66
+ uploaded_file = st.sidebar.file_uploader("Upload files", type=['png', 'jpeg', 'jpg'])
67
+ if uploaded_file is not None:
68
+ doc = uploaded_file.read()
69
+ cols[0].image(doc)
70
+
71
+ # Model selection
72
+ st.sidebar.title("Model selection")
73
+ det_arch = st.sidebar.selectbox("OCR model", DET_ARCHS)
74
+
75
+ # For newline
76
+ st.sidebar.write('\n')
77
+
78
+ if st.sidebar.button("Analyze image"):
79
+
80
+ if uploaded_file is None:
81
+ st.sidebar.write("Please upload a document")
82
+
83
+ else:
84
+ with st.spinner('Loading model...'):
85
+ if det_arch == 'pytesseract':
86
+ predictor = pytesseract.image_to_string(Image.open(doc))
87
+ else:
88
+ reader = easyocr.Reader(['en'])
89
+ predictor = reader.readtext("./data/" + filename, detail = 0)
90
+ with st.spinner('Analyzing...'):
91
+
92
+ # Plot OCR output
93
+ if det_arch == 'pytesseract':
94
+ cols[1].text(predictor)
95
+ else:
96
+ cols[1].text(''.join(text))
97
+
98
+
99
+ if __name__ == '__main__':
100
+ main()