robbyzhaox commited on
Commit
a95b973
·
unverified ·
1 Parent(s): 26ab692

update app.py

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. .streamlit/config.toml +4 -0
  3. src/NotoSans.ttf +3 -0
  4. src/app.py +34 -11
.gitattributes CHANGED
@@ -1,4 +1,5 @@
1
  *.7z filter=lfs diff=lfs merge=lfs -text
 
2
  *.arrow filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
4
  *.bz2 filter=lfs diff=lfs merge=lfs -text
 
1
  *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.ttf filter=lfs diff=lfs merge=lfs -text
3
  *.arrow filter=lfs diff=lfs merge=lfs -text
4
  *.bin filter=lfs diff=lfs merge=lfs -text
5
  *.bz2 filter=lfs diff=lfs merge=lfs -text
.streamlit/config.toml CHANGED
@@ -4,6 +4,10 @@ showErrorDetails = "none"
4
  [logger]
5
  level = "warning"
6
 
 
 
 
 
7
  [server]
8
  maxUploadSize = 2
9
  enableXsrfProtection = false
 
4
  [logger]
5
  level = "warning"
6
 
7
+ [ui]
8
+ hideTopBar = true
9
+ hideSidebarNav = true
10
+
11
  [server]
12
  maxUploadSize = 2
13
  enableXsrfProtection = false
src/NotoSans.ttf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2e68d43ae2c504f4e302a9cf522ecc3f06ef66d724cade58bbe13a3a4af70512
3
+ size 17805476
src/app.py CHANGED
@@ -5,23 +5,47 @@ logging.basicConfig(
5
  format="%(asctime)s [%(levelname)s] %(name)s: %(message)s"
6
  )
7
  import streamlit as st
 
8
  from myocr.pipelines import CommonOCRPipeline
9
- from PIL import Image, ImageDraw
10
  import tempfile
11
  import pandas as pd
 
 
 
 
 
 
12
 
13
  # init pipeline
14
- pipeline = CommonOCRPipeline("cpu")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  def main():
17
- st.set_page_config(layout="wide")
18
- st.title("MyOCR Demo")
19
-
20
  left_col, right_col = st.columns([2, 1.5])
21
-
22
  with left_col:
23
- with st.form("my-form", clear_on_submit=True):
24
- uploaded_file = st.file_uploader("", type=["png", "jpg", "jpeg"])
25
  spinner_container = st.empty()
26
  image_slot = st.empty()
27
  if uploaded_file:
@@ -48,7 +72,7 @@ def main():
48
  top_left = (bbox.left,bbox.top)
49
  bottom_right = (bbox.right, bbox.bottom )
50
  draw.rectangle([top_left, bottom_right], outline="red", width=2)
51
- draw.text(top_left, text, fill="red")
52
  table_data.append((text, item.confidence))
53
  image_slot.image(image_with_boxes, use_container_width=True)
54
 
@@ -58,10 +82,9 @@ def main():
58
  df = pd.DataFrame(table_data, columns=["Text", "Confidence"])
59
  st.table(df)
60
 
61
-
62
  if __name__ == "__main__":
63
  try:
64
  main()
65
  except Exception as e:
66
- logger.error(f"Error: {e}")
67
  st.error(f"Internal Error!")
 
5
  format="%(asctime)s [%(levelname)s] %(name)s: %(message)s"
6
  )
7
  import streamlit as st
8
+ import streamlit.components.v1 as components
9
  from myocr.pipelines import CommonOCRPipeline
10
+ from PIL import Image, ImageDraw, ImageFont
11
  import tempfile
12
  import pandas as pd
13
+ import os
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+ st.set_page_config(layout="wide")
18
+ st.title("MyOCR Demo")
19
 
20
  # init pipeline
21
+ @st.cache_resource
22
+ def load_pipeline():
23
+ pipeline = CommonOCRPipeline("cpu")
24
+ return pipeline
25
+
26
+ pipeline = load_pipeline()
27
+ font = ImageFont.truetype("src/NotoSans.ttf", 12)
28
+
29
+ def google_analytics():
30
+ # Google Analytics
31
+ GA_MEASUREMENT_ID = os.getenv("GOOGLE_ANALYTICS") or "NO_ID"
32
+ google_analytics_code = f"""
33
+ <!-- Google tag (gtag.js) -->
34
+ <script async src="https://www.googletagmanager.com/gtag/js?id={GA_MEASUREMENT_ID}"></script>
35
+ <script>
36
+ window.dataLayer = window.dataLayer || [];
37
+ function gtag(){{dataLayer.push(arguments);}}
38
+ gtag('js', new Date());
39
+ gtag('config', '{GA_MEASUREMENT_ID}');
40
+ </script>
41
+ """
42
+ components.html(google_analytics_code, height=0, width=0)
43
 
44
  def main():
45
+ google_analytics()
 
 
46
  left_col, right_col = st.columns([2, 1.5])
 
47
  with left_col:
48
+ uploaded_file = st.file_uploader("", type=["png", "jpg", "jpeg"])
 
49
  spinner_container = st.empty()
50
  image_slot = st.empty()
51
  if uploaded_file:
 
72
  top_left = (bbox.left,bbox.top)
73
  bottom_right = (bbox.right, bbox.bottom )
74
  draw.rectangle([top_left, bottom_right], outline="red", width=2)
75
+ draw.text((bbox.left, 0 if bbox.top - 18<0 else bbox.top - 18), text, font=font, fill="green")
76
  table_data.append((text, item.confidence))
77
  image_slot.image(image_with_boxes, use_container_width=True)
78
 
 
82
  df = pd.DataFrame(table_data, columns=["Text", "Confidence"])
83
  st.table(df)
84
 
 
85
  if __name__ == "__main__":
86
  try:
87
  main()
88
  except Exception as e:
89
+ logger.error("Error: ", e)
90
  st.error(f"Internal Error!")