nguyen-brat commited on
Commit
aa8fb5b
·
1 Parent(s): ac92a10
Files changed (1) hide show
  1. app.py +26 -37
app.py CHANGED
@@ -5,10 +5,10 @@ import subprocess
5
  import zipfile
6
  import io
7
  import shutil
8
- import time
9
  import sys
10
  from PIL import Image
11
  import tempfile
 
12
 
13
  os.environ["HYDRA_FULL_ERROR"] = "1"
14
 
@@ -70,21 +70,24 @@ def clear_folder(folder_path):
70
  except Exception as e:
71
  print(f'Failed to delete {file_path}. Reason: {e}')
72
 
73
- def create_temp_structure():
74
- # Create a temporary directory
75
- temp_dir = tempfile.mkdtemp()
 
 
 
76
 
77
  # Create test_folder
78
- test_folder = os.path.join(temp_dir, "test_folder")
79
  os.makedirs(test_folder, exist_ok=True)
80
 
81
  # Create target_folder with mask and result subdirectories
82
- target_folder = os.path.join(temp_dir, "target_folder")
83
  os.makedirs(os.path.join(target_folder, "mask"), exist_ok=True)
84
  os.makedirs(os.path.join(target_folder, "result"), exist_ok=True)
85
  os.makedirs(os.path.join(target_folder, "bbox"), exist_ok=True)
86
 
87
- return temp_dir, test_folder, target_folder
88
 
89
  st.title("Text Detection App")
90
  # file_name = " || ".join(os.listdir('craft_pytorch'))
@@ -94,31 +97,20 @@ uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "p
94
  if uploaded_file is not None:
95
  st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
96
 
97
- # Create a temporary directory for processing
98
-
99
- # Save the uploaded file temporarily
100
- temp_dir, input_path, output_path = create_temp_structure()
101
- st.write(f"Temp dir: {temp_dir}")
102
- # os.makedirs(input_path, exist_ok=True)
103
- # os.makedirs(osp(output_path, "result"), exist_ok=True)
104
- # os.makedirs(osp(output_path, "mask"), exist_ok=True)
105
 
106
  input_file_path = os.path.join(input_path, uploaded_file.name)
107
  image = Image.open(uploaded_file)
108
- # image.save(os.path.join(PROJECT_ROOT, input_file_path))
109
  image.save(input_file_path)
110
- file_name = " || ".join(os.listdir(f'{temp_dir}/target_folder'))
111
- st.write(file_name)
112
-
113
  if st.button("Run Text Detection"):
114
  progress_placeholder = st.empty()
115
  status_text = st.empty()
116
 
117
  try:
118
  status_text.text("Running text detection...")
119
- #os.makedirs(input_path, exist_ok=True)
120
- #os.makedirs(osp(output_path, "result"), exist_ok=True)
121
- #os.makedirs(osp(output_path, "mask"), exist_ok=True)
122
  rc, stderr_output = run_bash_script(input_path, output_path, progress_placeholder, status_text)
123
  if rc == 0:
124
  status_text.text("Text detection completed successfully!")
@@ -132,14 +124,9 @@ if uploaded_file is not None:
132
  label="Download Results",
133
  data=zip_buffer.getvalue(),
134
  file_name="text_detection_results.zip",
135
- mime="application/zip"
 
136
  )
137
- file_namea = " || ".join(os.listdir(f'{temp_dir}/target_folder/mask'))
138
- file_nameb = " || ".join(os.listdir(f'{temp_dir}/target_folder/bbox'))
139
- file_namec = " || ".join(os.listdir(f'{temp_dir}/target_folder/result'))
140
- st.write(file_namea)
141
- st.write(file_nameb)
142
- st.write(file_namec)
143
  else:
144
  st.error("Result folder not found. The text detection might have failed.")
145
  else:
@@ -150,15 +137,17 @@ if uploaded_file is not None:
150
  except Exception as e:
151
  st.error(f"An error occurred: {str(e)}")
152
  finally:
153
- # Clean up temporary files
154
- clear_folder(osp(output_path, "mask"))
155
  progress_placeholder.empty()
156
  status_text.empty()
157
 
158
- file_namea = " || ".join(os.listdir(f'{temp_dir}/mask'))
159
- file_nameb = " || ".join(os.listdir(f'{temp_dir}/result'))
160
- file_namec = " || ".join(os.listdir(f'{temp_dir}/bbox'))
161
- st.write(file_namea)
162
- st.write(file_nameb)
163
- st.write(file_namec)
 
 
 
 
164
  st.write("Note: The download button will appear after running text detection.")
 
5
  import zipfile
6
  import io
7
  import shutil
 
8
  import sys
9
  from PIL import Image
10
  import tempfile
11
+ import uuid
12
 
13
  os.environ["HYDRA_FULL_ERROR"] = "1"
14
 
 
70
  except Exception as e:
71
  print(f'Failed to delete {file_path}. Reason: {e}')
72
 
73
+ def create_persistent_structure():
74
+ # Create a unique directory name
75
+ unique_id = str(uuid.uuid4())
76
+
77
+ # Create a persistent directory in the current working directory
78
+ persistent_dir = os.path.join(os.getcwd(), f"processing_{unique_id}")
79
 
80
  # Create test_folder
81
+ test_folder = os.path.join(persistent_dir, "test_folder")
82
  os.makedirs(test_folder, exist_ok=True)
83
 
84
  # Create target_folder with mask and result subdirectories
85
+ target_folder = os.path.join(persistent_dir, "target_folder")
86
  os.makedirs(os.path.join(target_folder, "mask"), exist_ok=True)
87
  os.makedirs(os.path.join(target_folder, "result"), exist_ok=True)
88
  os.makedirs(os.path.join(target_folder, "bbox"), exist_ok=True)
89
 
90
+ return persistent_dir, test_folder, target_folder
91
 
92
  st.title("Text Detection App")
93
  # file_name = " || ".join(os.listdir('craft_pytorch'))
 
97
  if uploaded_file is not None:
98
  st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
99
 
100
+ # Create a persistent directory for processing
101
+ persistent_dir, input_path, output_path = create_persistent_structure()
102
+ st.write(f"Processing directory: {persistent_dir}")
 
 
 
 
 
103
 
104
  input_file_path = os.path.join(input_path, uploaded_file.name)
105
  image = Image.open(uploaded_file)
 
106
  image.save(input_file_path)
107
+
 
 
108
  if st.button("Run Text Detection"):
109
  progress_placeholder = st.empty()
110
  status_text = st.empty()
111
 
112
  try:
113
  status_text.text("Running text detection...")
 
 
 
114
  rc, stderr_output = run_bash_script(input_path, output_path, progress_placeholder, status_text)
115
  if rc == 0:
116
  status_text.text("Text detection completed successfully!")
 
124
  label="Download Results",
125
  data=zip_buffer.getvalue(),
126
  file_name="text_detection_results.zip",
127
+ mime="application/zip",
128
+ on_click=lambda: clear_folder(persistent_dir)
129
  )
 
 
 
 
 
 
130
  else:
131
  st.error("Result folder not found. The text detection might have failed.")
132
  else:
 
137
  except Exception as e:
138
  st.error(f"An error occurred: {str(e)}")
139
  finally:
 
 
140
  progress_placeholder.empty()
141
  status_text.empty()
142
 
143
+ # Display directory contents for debugging
144
+ st.write(f"Contents of {persistent_dir}:")
145
+ for root, dirs, files in os.walk(persistent_dir):
146
+ level = root.replace(persistent_dir, '').count(os.sep)
147
+ indent = ' ' * 4 * (level)
148
+ st.write(f"{indent}{os.path.basename(root)}/")
149
+ subindent = ' ' * 4 * (level + 1)
150
+ for f in files:
151
+ st.write(f"{subindent}{f}")
152
+
153
  st.write("Note: The download button will appear after running text detection.")