Spaces:
Sleeping
Sleeping
app
Browse files
app.py
CHANGED
|
@@ -145,48 +145,47 @@ def predict_files(filenames):
|
|
| 145 |
image_paths = eval(filenames) # Convert the filenames string back to a list
|
| 146 |
|
| 147 |
for filename in image_paths:
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
file_to_remove = os.path.join('static', 'temp', 'uploads', image_paths[index])
|
| 186 |
-
if os.path.exists(file_to_remove):
|
| 187 |
-
os.remove(file_to_remove)
|
| 188 |
|
| 189 |
-
|
| 190 |
return render_template('extractor.html', index_url=index_url, image_paths=image_paths, prediction_results = prediction_results, predictions=dict(zip(image_paths, prediction_results_copy)))
|
| 191 |
|
| 192 |
|
|
|
|
| 145 |
image_paths = eval(filenames) # Convert the filenames string back to a list
|
| 146 |
|
| 147 |
for filename in image_paths:
|
| 148 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 149 |
+
folder_path = UPLOAD_FOLDER
|
| 150 |
+
destination_folder = r'static/temp/img_display'
|
| 151 |
+
if not os.path.exists(destination_folder):
|
| 152 |
+
os.makedirs(destination_folder)
|
| 153 |
+
|
| 154 |
+
# Get a list of all files in the source folder
|
| 155 |
+
files = os.listdir(folder_path)
|
| 156 |
+
|
| 157 |
+
# Loop through each file and copy it to the destination folder
|
| 158 |
+
for file in files:
|
| 159 |
+
# Construct the full path of the source file
|
| 160 |
+
source_file_path = os.path.join(folder_path, file)
|
| 161 |
+
|
| 162 |
+
# Construct the full path of the destination file
|
| 163 |
+
destination_file_path = os.path.join(destination_folder, file)
|
| 164 |
+
|
| 165 |
+
# Copy the file to the destination folder
|
| 166 |
+
shutil.copy(source_file_path, destination_file_path)
|
| 167 |
|
| 168 |
+
if os.path.exists(file_path):
|
| 169 |
+
# Call make_predictions automatically
|
| 170 |
+
prediction_result = make_predictions([file_path])
|
| 171 |
+
if isinstance(prediction_result, list) and len(prediction_result) > 0:
|
| 172 |
+
prediction_results.append(prediction_result[0]) # Append only the first prediction result
|
| 173 |
+
else:
|
| 174 |
+
print(f"Error making prediction for {file}: {prediction_result}")
|
| 175 |
+
|
| 176 |
+
prediction_results_copy = copy.deepcopy(prediction_results)
|
| 177 |
+
|
| 178 |
+
non_receipt_indices = []
|
| 179 |
+
for i, prediction in enumerate(prediction_results):
|
| 180 |
+
if prediction == 'non-receipt':
|
| 181 |
+
non_receipt_indices.append(i)
|
| 182 |
+
|
| 183 |
+
# Delete images in reverse order to avoid index shifting
|
| 184 |
+
for index in non_receipt_indices[::-1]:
|
| 185 |
+
file_to_remove = os.path.join('static', 'temp', 'uploads', image_paths[index])
|
| 186 |
+
if os.path.exists(file_to_remove):
|
| 187 |
+
os.remove(file_to_remove)
|
|
|
|
|
|
|
|
|
|
| 188 |
|
|
|
|
| 189 |
return render_template('extractor.html', index_url=index_url, image_paths=image_paths, prediction_results = prediction_results, predictions=dict(zip(image_paths, prediction_results_copy)))
|
| 190 |
|
| 191 |
|