Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,61 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from streamlit_drawable_canvas import st_canvas
|
3 |
-
import numpy as np
|
4 |
-
from PIL import Image
|
5 |
import easyocr
|
6 |
import pandas as pd
|
7 |
|
8 |
-
def
|
9 |
-
"""
|
10 |
-
#
|
11 |
-
image_np = np.array(image)
|
12 |
-
|
13 |
-
# Initialize EasyOCR reader
|
14 |
-
reader = easyocr.Reader(['en'], gpu=False) # Add more languages or adjust as needed
|
15 |
-
result = reader.readtext(image_np)
|
16 |
-
|
17 |
-
# Draw rectangles on a copy of the image
|
18 |
-
draw_image = image.copy()
|
19 |
draw = ImageDraw.Draw(draw_image)
|
20 |
for res in result:
|
21 |
top_left = tuple(res[0][0])
|
22 |
bottom_right = tuple(res[0][2])
|
23 |
-
draw.rectangle(
|
24 |
-
|
25 |
-
return draw_image, result
|
26 |
-
|
27 |
-
def display_text_results(result):
|
28 |
-
"""Displays text results in Streamlit."""
|
29 |
-
textdic_easyocr = {idx: {'Text': res[1], 'Confidence': res[2]} for idx, res in enumerate(result)}
|
30 |
-
df = pd.DataFrame.from_dict(textdic_easyocr, orient='index')
|
31 |
-
st.table(df)
|
32 |
|
33 |
-
|
34 |
-
st.
|
|
|
|
|
35 |
|
36 |
# Column layout for uploader and canvas
|
37 |
col1, col2 = st.columns(2)
|
38 |
|
39 |
with col1:
|
40 |
-
|
41 |
|
42 |
with col2:
|
43 |
-
# Use the background_image parameter if you want to draw on an uploaded image.
|
44 |
canvas_result = st_canvas(
|
45 |
-
fill_color="rgba(255, 165, 0, 0.3)",
|
46 |
stroke_width=3,
|
47 |
-
stroke_color="#
|
48 |
background_color="#000000",
|
|
|
|
|
49 |
width=400,
|
50 |
height=400,
|
51 |
drawing_mode="freedraw",
|
52 |
key="canvas",
|
53 |
)
|
54 |
|
55 |
-
image
|
56 |
-
|
57 |
-
if uploaded_file is not None:
|
58 |
-
image = Image.open(uploaded_file).convert('RGB')
|
59 |
-
|
60 |
-
elif canvas_result.image_data is not None:
|
61 |
-
# Convert the canvas data to an Image
|
62 |
-
canvas_image = Image.fromarray((canvas_result.image_data).astype('uint8'), mode='RGBA')
|
63 |
-
image = canvas_image.convert('RGB')
|
64 |
-
|
65 |
-
if image:
|
66 |
st.image(image, caption="Uploaded/Drawn Image")
|
67 |
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
|
71 |
-
|
|
|
72 |
|
|
|
73 |
else:
|
74 |
st.write("Please upload an image or use the canvas to draw.")
|
|
|
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 |
+
# Main title and markdowns
|
19 |
+
st.title("Get text from an image with EasyOCR")
|
20 |
+
st.markdown("## EasyOCR with Streamlit")
|
21 |
+
st.markdown("## Upload an Image or Draw")
|
22 |
|
23 |
# Column layout for uploader and canvas
|
24 |
col1, col2 = st.columns(2)
|
25 |
|
26 |
with col1:
|
27 |
+
file = st.file_uploader("Upload Here", type=['png', 'jpg', 'jpeg'])
|
28 |
|
29 |
with col2:
|
|
|
30 |
canvas_result = st_canvas(
|
31 |
+
fill_color="rgba(255, 165, 0, 0.3)",
|
32 |
stroke_width=3,
|
33 |
+
stroke_color="#ffffff",
|
34 |
background_color="#000000",
|
35 |
+
background_image=None if file else st.session_state.get("background", None),
|
36 |
+
update_streamlit=True,
|
37 |
width=400,
|
38 |
height=400,
|
39 |
drawing_mode="freedraw",
|
40 |
key="canvas",
|
41 |
)
|
42 |
|
43 |
+
if image is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
st.image(image, caption="Uploaded/Drawn Image")
|
45 |
|
46 |
+
# Optional: Indicate that processing is happening
|
47 |
+
with st.spinner('Processing...'):
|
48 |
+
reader = easyocr.Reader(['en', 'ja'], gpu=False) # Consider moving this outside the loop if performance is a concern
|
49 |
+
result = reader.readtext(np.array(image))
|
50 |
+
|
51 |
+
for idx, res in enumerate(result):
|
52 |
+
pred_text = res[1]
|
53 |
+
st.write(pred_text)
|
54 |
|
55 |
+
textdic_easyocr = {res[1]: {'pred_confidence': res[2]} for res in result}
|
56 |
+
df = pd.DataFrame.from_dict(textdic_easyocr, orient='index', columns=['pred_confidence'])
|
57 |
+
st.table(df)
|
58 |
|
59 |
+
rectangle(image, result)
|
60 |
else:
|
61 |
st.write("Please upload an image or use the canvas to draw.")
|