Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,35 @@
|
|
1 |
-
|
2 |
import numpy as np
|
3 |
import streamlit as st
|
4 |
-
from streamlit_drawable_canvas import st_canvas
|
5 |
import easyocr
|
6 |
-
import
|
|
|
7 |
|
8 |
def rectangle(image, result):
|
9 |
-
"""Draw rectangles on the image based on predicted coordinates
|
10 |
-
|
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 |
-
|
|
|
17 |
|
18 |
-
# Main title
|
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 |
-
#
|
|
|
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,
|
@@ -40,24 +43,34 @@ with col2:
|
|
40 |
key="canvas",
|
41 |
)
|
42 |
|
43 |
-
image = None
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
if image is not None:
|
46 |
-
st.image(image
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
-
result = reader.readtext(np.array(image))
|
52 |
|
|
|
53 |
for idx, res in enumerate(result):
|
54 |
pred_text = res[1]
|
55 |
st.write(pred_text)
|
56 |
|
|
|
57 |
textdic_easyocr = {res[1]: {'pred_confidence': res[2]} for res in result}
|
58 |
-
|
|
|
|
|
59 |
st.table(df)
|
60 |
|
|
|
61 |
rectangle(image, result)
|
62 |
else:
|
63 |
st.write("Please upload an image or use the canvas to draw.")
|
|
|
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]) # top left coordinates as a tuple
|
13 |
+
bottom_right = tuple(res[0][2]) # bottom right coordinates as a tuple
|
14 |
draw.rectangle((top_left, bottom_right), outline="blue", width=2)
|
15 |
+
# Display the image on Streamlit
|
16 |
+
st.image(image)
|
17 |
|
18 |
+
# Main title
|
19 |
st.title("Get text from an image with EasyOCR")
|
20 |
+
|
21 |
+
# Subtitle
|
22 |
st.markdown("## EasyOCR with Streamlit")
|
|
|
23 |
|
24 |
+
# Upload image file or draw
|
25 |
+
st.markdown("## Upload an Image or Draw")
|
26 |
col1, col2 = st.columns(2)
|
27 |
|
28 |
with col1:
|
29 |
file = st.file_uploader("Upload Here", type=['png', 'jpg', 'jpeg'])
|
30 |
|
31 |
with col2:
|
32 |
+
# Drawable canvas
|
33 |
canvas_result = st_canvas(
|
34 |
fill_color="rgba(255, 165, 0, 0.3)",
|
35 |
stroke_width=3,
|
|
|
43 |
key="canvas",
|
44 |
)
|
45 |
|
46 |
+
image = None # Initialize image variable
|
47 |
+
|
48 |
+
# Process uploaded image or drawing
|
49 |
+
if file is not None:
|
50 |
+
image = Image.open(file) # Read image with PIL library
|
51 |
+
elif canvas_result.image_data is not None:
|
52 |
+
image = Image.fromarray(np.array(canvas_result.image_data, dtype=np.uint8)).convert('RGB')
|
53 |
|
54 |
if image is not None:
|
55 |
+
st.image(image) # Display the image
|
56 |
|
57 |
+
# Initialize EasyOCR reader; you can add or remove languages based on your preference
|
58 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
59 |
+
result = reader.readtext(np.array(image)) # Convert image to numpy array and process with EasyOCR
|
|
|
60 |
|
61 |
+
# Print all predicted text
|
62 |
for idx, res in enumerate(result):
|
63 |
pred_text = res[1]
|
64 |
st.write(pred_text)
|
65 |
|
66 |
+
# Collect the results in a dictionary
|
67 |
textdic_easyocr = {res[1]: {'pred_confidence': res[2]} for res in result}
|
68 |
+
|
69 |
+
# Create a DataFrame to show the predicted text and prediction confidence
|
70 |
+
df = pd.DataFrame.from_dict(textdic_easyocr).T
|
71 |
st.table(df)
|
72 |
|
73 |
+
# Draw rectangles around the detected text in the image
|
74 |
rectangle(image, result)
|
75 |
else:
|
76 |
st.write("Please upload an image or use the canvas to draw.")
|