Mattral commited on
Commit
3ebf562
·
verified ·
1 Parent(s): d706d88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -43
app.py CHANGED
@@ -1,65 +1,36 @@
1
- import pandas as pd
2
  import numpy as np
3
  import streamlit as st
4
- import easyocr
5
- from PIL import Image, ImageDraw
6
  from streamlit_drawable_canvas import st_canvas
 
 
7
 
8
  def rectangle(image, result):
9
- """Draw rectangles on the image based on predicted coordinates."""
10
- draw = ImageDraw.Draw(image)
 
11
  for res in result:
12
  top_left = tuple(res[0][0])
13
  bottom_right = tuple(res[0][2])
14
  draw.rectangle((top_left, bottom_right), outline="blue", width=2)
15
- st.image(image)
16
-
17
- # Main title and markdowns
18
- st.title("Get text from an image with EasyOCR")
19
- st.markdown("## EasyOCR with Streamlit")
20
- st.markdown("## Upload an Image or Draw")
21
-
22
- # Column layout for uploader and canvas
23
- col1, col2 = st.columns(2)
24
-
25
- with col1:
26
- file = st.file_uploader("Upload Here", type=['png', 'jpg', 'jpeg'])
27
-
28
- with col2:
29
- canvas_result = st_canvas(
30
- fill_color="rgba(255, 165, 0, 0.3)",
31
- stroke_width=3,
32
- stroke_color="#ffffff",
33
- background_color="#000000",
34
- background_image=None if file else st.session_state.get("background", None),
35
- update_streamlit=True,
36
- width=400,
37
- height=400,
38
- drawing_mode="freedraw",
39
- key="canvas",
40
- )
41
-
42
- image = None
43
 
44
- if file is not None:
45
- image = Image.open(file)
46
- elif canvas_result.image_data is not None:
47
- # Convert canvas RGBA image to RGB
48
- canvas_image_data = canvas_result.image_data.astype(np.uint8)
49
- image = Image.fromarray(canvas_image_data, 'RGBA').convert('RGB')
50
 
51
  if image is not None:
52
- st.image(image)
53
 
54
- reader = easyocr.Reader(['en', 'ja'], gpu=False)
55
- result = reader.readtext(np.array(image))
 
 
56
 
57
  for idx, res in enumerate(result):
58
  pred_text = res[1]
59
  st.write(pred_text)
60
 
61
  textdic_easyocr = {res[1]: {'pred_confidence': res[2]} for res in result}
62
- df = pd.DataFrame.from_dict(textdic_easyocr).T
63
  st.table(df)
64
 
65
  rectangle(image, result)
 
1
+ from PIL import Image, ImageDraw
2
  import numpy as np
3
  import streamlit as st
 
 
4
  from streamlit_drawable_canvas import st_canvas
5
+ import easyocr
6
+ import pandas as pd
7
 
8
  def rectangle(image, result):
9
+ """Draw rectangles on the image based on predicted coordinates and display the image."""
10
+ draw_image = image.copy() # Work on a copy of the image
11
+ draw = ImageDraw.Draw(draw_image)
12
  for res in result:
13
  top_left = tuple(res[0][0])
14
  bottom_right = tuple(res[0][2])
15
  draw.rectangle((top_left, bottom_right), outline="blue", width=2)
16
+ st.image(draw_image, caption="Processed Image with Detected Text Highlighted")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # Rest of your script remains unchanged until the final processing:
 
 
 
 
 
19
 
20
  if image is not None:
21
+ st.image(image, caption="Uploaded/Drawn Image")
22
 
23
+ # Optional: Indicate that processing is happening
24
+ with st.spinner('Processing...'):
25
+ reader = easyocr.Reader(['en'], gpu=False) # Consider moving this outside the loop if performance is a concern
26
+ result = reader.readtext(np.array(image))
27
 
28
  for idx, res in enumerate(result):
29
  pred_text = res[1]
30
  st.write(pred_text)
31
 
32
  textdic_easyocr = {res[1]: {'pred_confidence': res[2]} for res in result}
33
+ df = pd.DataFrame.from_dict(textdic_easyocr, orient='index', columns=['pred_confidence'])
34
  st.table(df)
35
 
36
  rectangle(image, result)