nguyen-brat
commited on
Commit
•
3ccf12e
1
Parent(s):
275a405
update
Browse files- Dockerfile +66 -0
- README.md +4 -3
- app.py +102 -0
- requirements.txt +19 -0
Dockerfile
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as a parent image
|
2 |
+
FROM python:3.8
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Install system dependencies
|
8 |
+
RUN apt-get update && apt-get install -y \
|
9 |
+
build-essential \
|
10 |
+
cmake \
|
11 |
+
curl \
|
12 |
+
ca-certificates \
|
13 |
+
gcc \
|
14 |
+
git \
|
15 |
+
git-lfs \
|
16 |
+
wget \
|
17 |
+
libpq-dev \
|
18 |
+
libsndfile1-dev \
|
19 |
+
libgl1 \
|
20 |
+
unzip \
|
21 |
+
libjpeg-dev \
|
22 |
+
libpng-dev \
|
23 |
+
libgomp1 \
|
24 |
+
&& rm -rf /var/lib/apt/lists/* \
|
25 |
+
&& apt-get clean
|
26 |
+
|
27 |
+
WORKDIR /app
|
28 |
+
|
29 |
+
# Create .streamlit/config.toml file
|
30 |
+
RUN mkdir -p /app/.streamlit && \
|
31 |
+
echo "[server]\nenableXsrfProtection = false\nenableCORS = false" > /app/.streamlit/config.toml
|
32 |
+
|
33 |
+
# Clone the repository
|
34 |
+
RUN git clone https://github.com/nguyen-brat/text-remove.git /app/text-remove
|
35 |
+
|
36 |
+
# Set the working directory to the cloned repo
|
37 |
+
WORKDIR /app/text-remove
|
38 |
+
|
39 |
+
RUN mkdir -p /app/text-remove/.streamlit && \
|
40 |
+
echo "[server]\nenableXsrfProtection = false\nenableCORS = false" > /app/text-remove/.streamlit/config.toml
|
41 |
+
|
42 |
+
# Install Python dependencies
|
43 |
+
COPY requirements.txt .
|
44 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
45 |
+
|
46 |
+
# Set up Craft
|
47 |
+
WORKDIR /app/text-remove/CRAFT-pytorch
|
48 |
+
RUN wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=1Jk4eGD7crsqCCg9C9VjCLkMN3ze8kutZ' -O "craft_mlt_25k.pth"
|
49 |
+
RUN pip install gdown
|
50 |
+
RUN gdown 1XSaFwBkOaFOdtk4Ane3DFyJGPRw6v5bO
|
51 |
+
|
52 |
+
# Set up lama
|
53 |
+
WORKDIR /app/text-remove/lama
|
54 |
+
RUN curl -LJO https://huggingface.co/smartywu/big-lama/resolve/main/big-lama.zip
|
55 |
+
RUN unzip big-lama.zip
|
56 |
+
|
57 |
+
# Set the working directory back to the root of the project
|
58 |
+
WORKDIR /app/text-remove
|
59 |
+
|
60 |
+
# Make port 7860 available to the world outside this container
|
61 |
+
EXPOSE 7860
|
62 |
+
|
63 |
+
# Run app.py when the container launches
|
64 |
+
ENTRYPOINT ["streamlit", "run"]
|
65 |
+
CMD ["app.py", "--server.port=7860"]
|
66 |
+
# CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
|
README.md
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: red
|
6 |
sdk: docker
|
7 |
pinned: false
|
|
|
8 |
---
|
9 |
|
10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Docker Remove Text
|
3 |
+
emoji: 👀
|
4 |
+
colorFrom: yellow
|
5 |
colorTo: red
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
+
app_port: 7860
|
9 |
---
|
10 |
|
11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.")
|
requirements.txt
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.24.0
|
2 |
+
torch==2.3.1
|
3 |
+
torchvision==0.2.1
|
4 |
+
opencv-python==4.10.0.82
|
5 |
+
scikit-image==0.19.3
|
6 |
+
pyyaml
|
7 |
+
tqdm
|
8 |
+
easydict
|
9 |
+
scikit-learn==1.3.2
|
10 |
+
pandas==2.0.3
|
11 |
+
pytorch-lightning==2.3.0
|
12 |
+
kornia==0.7.2
|
13 |
+
hydra-core==1.1.0
|
14 |
+
omegaconf==2.1.2
|
15 |
+
albumentations==0.5.2
|
16 |
+
webdataset==0.2.86
|
17 |
+
pillow==6.2.2
|
18 |
+
streamlit==1.24.0
|
19 |
+
pillow==6.2.2
|