Update app.py
Browse files
app.py
CHANGED
@@ -2,22 +2,21 @@ import pandas as pd
|
|
2 |
import numpy as np
|
3 |
import streamlit as st
|
4 |
import easyocr
|
5 |
-
import PIL
|
6 |
from PIL import Image, ImageDraw
|
7 |
from streamlit_drawable_canvas import st_canvas
|
8 |
|
9 |
def rectangle(image, result):
|
10 |
-
"""Draw rectangles on image based on predicted coordinates."""
|
11 |
draw = ImageDraw.Draw(image)
|
12 |
for res in result:
|
13 |
-
top_left = tuple(res[0][0]) # top left coordinates as tuple
|
14 |
-
bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
|
15 |
draw.rectangle((top_left, bottom_right), outline="blue", width=2)
|
16 |
-
# Display image on
|
17 |
st.image(image)
|
18 |
|
19 |
# Main title
|
20 |
-
st.title("Get text from image with EasyOCR")
|
21 |
|
22 |
# Subtitle
|
23 |
st.markdown("## EasyOCR with Streamlit")
|
@@ -32,7 +31,7 @@ with col1:
|
|
32 |
with col2:
|
33 |
# Drawable canvas
|
34 |
canvas_result = st_canvas(
|
35 |
-
fill_color="rgba(255, 165, 0, 0.3)",
|
36 |
stroke_width=3,
|
37 |
stroke_color="#ffffff",
|
38 |
background_color="#000000",
|
@@ -44,36 +43,34 @@ with col2:
|
|
44 |
key="canvas",
|
45 |
)
|
46 |
|
|
|
|
|
47 |
# Process uploaded image or drawing
|
48 |
if file is not None:
|
49 |
image = Image.open(file) # Read image with PIL library
|
50 |
-
|
51 |
-
|
52 |
-
temp_image.save("temp_canvas_image.jpg")
|
53 |
-
st.image(temp_image) # Display to confirm
|
54 |
-
image = temp_image
|
55 |
-
else:
|
56 |
-
st.write("Please upload an image or use the canvas to draw.")
|
57 |
-
image = None
|
58 |
|
59 |
if image is not None:
|
60 |
-
st.image(image) # Display
|
61 |
|
62 |
-
#
|
63 |
-
reader = easyocr.Reader(['en','ja'], gpu=False)
|
64 |
-
result = reader.readtext(np.array(image)) #
|
65 |
|
66 |
-
# Print all predicted text
|
67 |
for idx, res in enumerate(result):
|
68 |
pred_text = res[1]
|
69 |
st.write(pred_text)
|
70 |
|
71 |
-
# Collect the results in
|
72 |
textdic_easyocr = {res[1]: {'pred_confidence': res[2]} for res in result}
|
73 |
|
74 |
-
# Create a
|
75 |
df = pd.DataFrame.from_dict(textdic_easyocr).T
|
76 |
st.table(df)
|
77 |
|
78 |
-
#
|
79 |
rectangle(image, result)
|
|
|
|
|
|
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")
|
|
|
31 |
with col2:
|
32 |
# Drawable canvas
|
33 |
canvas_result = st_canvas(
|
34 |
+
fill_color="rgba(255, 165, 0, 0.3)",
|
35 |
stroke_width=3,
|
36 |
stroke_color="#ffffff",
|
37 |
background_color="#000000",
|
|
|
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', 'ja'], 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.")
|