Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ import streamlit as st
|
|
12 |
import torch
|
13 |
from ultralyticsplus import YOLO, render_result
|
14 |
|
15 |
-
from convert import convert_to_braille_unicode, parse_xywh_and_class
|
16 |
|
17 |
|
18 |
def load_model(model_path):
|
@@ -54,59 +54,63 @@ source_img = None
|
|
54 |
source_img = st.sidebar.file_uploader(
|
55 |
"Choose an image...", type=("jpg", "jpeg", "png", "bmp", "webp")
|
56 |
)
|
57 |
-
col1, col2 = st.columns(2)
|
58 |
-
|
59 |
-
# left column of the page body
|
60 |
-
with col1:
|
61 |
-
if source_img is None:
|
62 |
-
default_image_path = "./images/alpha-numeric.jpeg"
|
63 |
-
image = load_image(default_image_path)
|
64 |
-
st.image(
|
65 |
-
default_image_path, caption="Example Input Image", use_column_width=True
|
66 |
-
)
|
67 |
-
else:
|
68 |
-
image = load_image(source_img)
|
69 |
-
st.image(source_img, caption="Uploaded Image", use_column_width=True)
|
70 |
-
|
71 |
-
# right column of the page body
|
72 |
-
with col2:
|
73 |
-
with st.spinner("Wait for it..."):
|
74 |
-
start_time = time.time()
|
75 |
-
try:
|
76 |
-
with torch.no_grad():
|
77 |
-
res = model.predict(
|
78 |
-
image, save=True, save_txt=True, exist_ok=True, conf=conf
|
79 |
-
)
|
80 |
-
boxes = res[0].boxes # first image
|
81 |
-
res_plotted = res[0].plot()[:, :, ::-1]
|
82 |
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
|
86 |
-
|
|
|
87 |
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
90 |
|
|
|
|
|
91 |
|
|
|
|
|
92 |
try:
|
93 |
st.success(f"Done! Inference time: {time.time() - start_time:.2f} seconds")
|
94 |
-
st.subheader("Detected
|
|
|
95 |
for box_line in list_boxes:
|
96 |
str_left_to_right = ""
|
97 |
box_classes = box_line[:, -1]
|
98 |
for each_class in box_classes:
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
except Exception as ex:
|
104 |
-
st.write("Please try again with images with types of JPG, JPEG, PNG ...")
|
105 |
-
|
106 |
-
with open(IMAGE_DOWNLOAD_PATH, "rb") as fl:
|
107 |
-
st.download_button(
|
108 |
-
"Download object-detected image",
|
109 |
-
data=fl,
|
110 |
-
file_name="image0.jpg",
|
111 |
-
mime="image/jpg",
|
112 |
-
)
|
|
|
12 |
import torch
|
13 |
from ultralyticsplus import YOLO, render_result
|
14 |
|
15 |
+
from convert import convert_to_braille_unicode, parse_xywh_and_class, braille_to_text
|
16 |
|
17 |
|
18 |
def load_model(model_path):
|
|
|
54 |
source_img = st.sidebar.file_uploader(
|
55 |
"Choose an image...", type=("jpg", "jpeg", "png", "bmp", "webp")
|
56 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
# Single column layout - only show uploaded image
|
59 |
+
if source_img is None:
|
60 |
+
default_image_path = "./images/alpha-numeric.jpeg"
|
61 |
+
image = load_image(default_image_path)
|
62 |
+
st.image(
|
63 |
+
default_image_path, caption="Example Input Image", use_column_width=True
|
64 |
+
)
|
65 |
+
else:
|
66 |
+
image = load_image(source_img)
|
67 |
+
st.image(source_img, caption="Uploaded Image", use_column_width=True)
|
68 |
|
69 |
+
# Process the image
|
70 |
+
with st.spinner("Wait for it..."):
|
71 |
+
start_time = time.time()
|
72 |
|
73 |
+
try:
|
74 |
+
with torch.no_grad():
|
75 |
+
res = model.predict(
|
76 |
+
image, save=True, save_txt=True, exist_ok=True, conf=conf
|
77 |
+
)
|
78 |
+
boxes = res[0].boxes # first image
|
79 |
+
list_boxes = parse_xywh_and_class(boxes)
|
80 |
|
81 |
+
except Exception as ex:
|
82 |
+
st.write("Please upload image with types of JPG, JPEG, PNG ...")
|
83 |
|
84 |
+
# Convert braille to text and display results
|
85 |
+
detected_text_lines = []
|
86 |
try:
|
87 |
st.success(f"Done! Inference time: {time.time() - start_time:.2f} seconds")
|
88 |
+
st.subheader("Detected Text")
|
89 |
+
|
90 |
for box_line in list_boxes:
|
91 |
str_left_to_right = ""
|
92 |
box_classes = box_line[:, -1]
|
93 |
for each_class in box_classes:
|
94 |
+
braille_unicode = convert_to_braille_unicode(model.names[int(each_class)])
|
95 |
+
str_left_to_right += braille_unicode
|
96 |
+
|
97 |
+
# Convert braille unicode to actual text
|
98 |
+
text_line = braille_to_text(str_left_to_right)
|
99 |
+
detected_text_lines.append(text_line)
|
100 |
+
st.write(text_line)
|
101 |
+
|
102 |
+
# Combine all detected text
|
103 |
+
full_detected_text = "\n".join(detected_text_lines)
|
104 |
+
|
105 |
+
# Add copy to clipboard functionality
|
106 |
+
if st.button("Copy Text to Clipboard"):
|
107 |
+
# Use streamlit's built-in clipboard functionality
|
108 |
+
st.code(full_detected_text, language=None)
|
109 |
+
st.success("Text displayed above. Use Ctrl+A to select all, then Ctrl+C to copy!")
|
110 |
+
|
111 |
+
# Alternative: Create a text area that users can easily copy from
|
112 |
+
st.subheader("Copy Text Below:")
|
113 |
+
st.text_area("Detected Text (Select All & Copy)", value=full_detected_text, height=150)
|
114 |
+
|
115 |
except Exception as ex:
|
116 |
+
st.write("Please try again with images with types of JPG, JPEG, PNG ...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|