Update app.py
Browse files
app.py
CHANGED
@@ -9,27 +9,23 @@ 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 |
-
# 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,34 +39,29 @@ with col2:
|
|
43 |
key="canvas",
|
44 |
)
|
45 |
|
46 |
-
image = None
|
47 |
|
48 |
-
# Process uploaded image or drawing
|
49 |
if file is not None:
|
50 |
-
image = Image.open(file)
|
51 |
elif canvas_result.image_data is not None:
|
52 |
-
image
|
|
|
|
|
53 |
|
54 |
if image is not None:
|
55 |
-
st.image(image)
|
56 |
|
57 |
-
# Initialize EasyOCR reader; you can add or remove languages based on your preference
|
58 |
reader = easyocr.Reader(['en', 'ja'], gpu=False)
|
59 |
-
result = reader.readtext(np.array(image))
|
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.")
|
|
|
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,
|
|
|
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)
|
66 |
else:
|
67 |
st.write("Please upload an image or use the canvas to draw.")
|