Commit
·
03af360
1
Parent(s):
3ccf12e
update
Browse files
app.py
CHANGED
@@ -1,102 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
from os.path import join as osp
|
4 |
-
import subprocess
|
5 |
-
import zipfile
|
6 |
-
import io
|
7 |
-
import shutil
|
8 |
-
import time
|
9 |
-
|
10 |
-
def run_bash_script(input_image_path, output_path, progress_placeholder, status_text):
|
11 |
-
bash_command = f"bash config/text_detection.sh -s {input_image_path} -t {output_path}"
|
12 |
-
process = subprocess.Popen(bash_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
13 |
-
|
14 |
-
progress = 0
|
15 |
-
for line in process.stdout:
|
16 |
-
st.text(line.strip())
|
17 |
-
progress += 0.1
|
18 |
-
progress_placeholder.progress(min(progress, 1.0))
|
19 |
-
|
20 |
-
# Capture and display stderr
|
21 |
-
stderr_output = process.stderr.read()
|
22 |
-
if stderr_output:
|
23 |
-
status_text.error("Error output:")
|
24 |
-
st.code(stderr_output, language="bash")
|
25 |
-
|
26 |
-
rc = process.wait()
|
27 |
-
return rc, stderr_output
|
28 |
-
|
29 |
-
def zip_result_files(result_folder):
|
30 |
-
zip_buffer = io.BytesIO()
|
31 |
-
|
32 |
-
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
|
33 |
-
for root, _, files in os.walk(result_folder):
|
34 |
-
for file in files:
|
35 |
-
if file.endswith(".png"):
|
36 |
-
file_path = os.path.join(root, file)
|
37 |
-
arcname = os.path.relpath(file_path, result_folder)
|
38 |
-
zip_file.write(file_path, arcname)
|
39 |
-
|
40 |
-
return zip_buffer
|
41 |
-
|
42 |
-
st.title("Text Detection App")
|
43 |
-
|
44 |
-
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
45 |
-
|
46 |
-
if uploaded_file is not None:
|
47 |
-
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
48 |
-
|
49 |
-
# Create a temporary directory for processing
|
50 |
-
|
51 |
-
# Save the uploaded file temporarily
|
52 |
-
input_path = "test_folder"
|
53 |
-
output_path = "target_test"
|
54 |
-
os.makedirs(input_path, exist_ok=True)
|
55 |
-
os.makedirs(osp(output_path, "result"), exist_ok=True)
|
56 |
-
os.makedirs(osp(output_path, "mask"), exist_ok=True)
|
57 |
-
|
58 |
-
input_file_path = os.path.join(input_path, uploaded_file.name)
|
59 |
-
with open(input_file_path, "wb") as f:
|
60 |
-
f.write(uploaded_file.getbuffer())
|
61 |
-
|
62 |
-
if st.button("Run Text Detection"):
|
63 |
-
progress_placeholder = st.empty()
|
64 |
-
status_text = st.empty()
|
65 |
-
|
66 |
-
try:
|
67 |
-
status_text.text("Running text detection...")
|
68 |
-
os.makedirs(input_path, exist_ok=True)
|
69 |
-
os.makedirs(osp(output_path, "result"), exist_ok=True)
|
70 |
-
os.makedirs(osp(output_path, "mask"), exist_ok=True)
|
71 |
-
rc, stderr_output = run_bash_script(input_path, output_path, progress_placeholder, status_text)
|
72 |
-
|
73 |
-
if rc == 0:
|
74 |
-
status_text.text("Text detection completed successfully!")
|
75 |
-
result_folder = os.path.join(output_path, "result")
|
76 |
-
if os.path.exists(result_folder):
|
77 |
-
st.write("You can now download the results.")
|
78 |
-
|
79 |
-
# Add download button
|
80 |
-
zip_buffer = zip_result_files(result_folder)
|
81 |
-
st.download_button(
|
82 |
-
label="Download Results",
|
83 |
-
data=zip_buffer.getvalue(),
|
84 |
-
file_name="text_detection_results.zip",
|
85 |
-
mime="application/zip"
|
86 |
-
)
|
87 |
-
else:
|
88 |
-
st.error("Result folder not found. The text detection might have failed.")
|
89 |
-
else:
|
90 |
-
st.error(f"Text detection failed with return code {rc}")
|
91 |
-
if stderr_output:
|
92 |
-
st.error("Error details:")
|
93 |
-
st.code(stderr_output, language="bash")
|
94 |
-
except Exception as e:
|
95 |
-
st.error(f"An error occurred: {str(e)}")
|
96 |
-
finally:
|
97 |
-
# Clean up temporary files
|
98 |
-
shutil.rmtree(osp(output_path, "mask"), ignore_errors=True)
|
99 |
-
progress_placeholder.empty()
|
100 |
-
status_text.empty()
|
101 |
-
|
102 |
-
st.write("Note: The download button will appear after running text detection.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|