Spaces:
Sleeping
Sleeping
snoop2head
commited on
Commit
•
f4e9d9d
1
Parent(s):
664f840
restructure streamlit UI as two-column page
Browse files
app.py
CHANGED
@@ -54,50 +54,59 @@ source_img = None
|
|
54 |
source_img = st.sidebar.file_uploader(
|
55 |
"Choose an image...", type=("jpg", "jpeg", "png", "bmp", "webp")
|
56 |
)
|
57 |
-
|
58 |
|
59 |
# left column of the page body
|
60 |
-
|
61 |
-
if source_img is None:
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
68 |
|
69 |
# right column of the page body
|
70 |
-
|
71 |
-
with st.spinner("Wait for it..."):
|
72 |
-
|
73 |
-
with torch.no_grad():
|
74 |
-
res = model.predict(image, save=True, save_txt=True, exist_ok=True, conf=conf)
|
75 |
-
boxes = res[0].boxes # first image
|
76 |
-
res_plotted = res[0].plot()[:, :, ::-1]
|
77 |
-
|
78 |
-
list_boxes = parse_xywh_and_class(boxes)
|
79 |
-
|
80 |
try:
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
str_left_to_right = ""
|
85 |
-
box_classes = box_line[:, -1]
|
86 |
-
for each_class in box_classes:
|
87 |
-
str_left_to_right += convert_to_braille_unicode(
|
88 |
-
model.names[int(each_class)]
|
89 |
-
)
|
90 |
-
st.subheader(str_left_to_right)
|
91 |
-
|
92 |
-
st.image(res_plotted, caption="Detected Image", use_column_width=True)
|
93 |
-
IMAGE_DOWNLOAD_PATH = f"runs/detect/predict/image0.jpg"
|
94 |
-
with open(IMAGE_DOWNLOAD_PATH, "rb") as fl:
|
95 |
-
st.download_button(
|
96 |
-
"Download object-detected image",
|
97 |
-
data=fl,
|
98 |
-
file_name="image0.jpg",
|
99 |
-
mime="image/jpg",
|
100 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
except Exception as ex:
|
103 |
st.write("Please upload image with types of JPG, JPEG, PNG ...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
list_boxes = parse_xywh_and_class(boxes)
|
84 |
+
|
85 |
+
st.image(res_plotted, caption="Detected Image", use_column_width=True)
|
86 |
+
IMAGE_DOWNLOAD_PATH = f"runs/detect/predict/image0.jpg"
|
87 |
|
88 |
except Exception as ex:
|
89 |
st.write("Please upload image with types of JPG, JPEG, PNG ...")
|
90 |
+
|
91 |
+
|
92 |
+
try:
|
93 |
+
st.success(f"Done! Inference time: {time.time() - start_time:.2f} seconds")
|
94 |
+
st.subheader("Detected Braille Patterns")
|
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 |
+
str_left_to_right += convert_to_braille_unicode(
|
100 |
+
model.names[int(each_class)]
|
101 |
+
)
|
102 |
+
st.write(str_left_to_right)
|
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 |
+
)
|