-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPP.py
More file actions
380 lines (305 loc) · 13.3 KB
/
Copy pathAPP.py
File metadata and controls
380 lines (305 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from flask import Flask, render_template, request, jsonify
import os
from werkzeug.utils import secure_filename
from helpers import find_MCS_Data, update_merged_data, save_mapping_file, get_batch_number, clear_folder, extract_text_from_image, correct_label, base10_to_base36, save_to_json_and_csv, base36_numbers, validate_and_correct, get_images, save_stl_files
from all_parts import save_log_to_json
import csv
from flask import jsonify, Flask, request, session
import numpy as np
import re
from pathlib import Path
from datetime import datetime
import json
import secrets
import shutil
#--------------------------------------------------------
#INITIALIZE COMMMUNICATION WITH HTML, allow file exchange
app = Flask(__name__) # allows communication with html
app.secret_key = secrets.token_hex(16)
UPLOAD_FOLDER = "static/uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
PUBLIC_SCREENSHOT_FOLDER = "static/stl_screenshots"
os.makedirs(PUBLIC_SCREENSHOT_FOLDER, exist_ok=True)
def delete_everything(UPLOAD_FOLDER, PUBLIC_SCREENSHOT_FOLDER):
# Empty the folder
for filename in os.listdir(UPLOAD_FOLDER):
file_path = os.path.join(UPLOAD_FOLDER, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path) # Remove file or symlink
elif os.path.isdir(file_path):
shutil.rmtree(file_path) # Remove subdirectory
except Exception as e:
print(f"Failed to delete {file_path}. Reason: {e}")
for filename in os.listdir(PUBLIC_SCREENSHOT_FOLDER):
file_path = os.path.join(PUBLIC_SCREENSHOT_FOLDER, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path) # Remove file or symlink
elif os.path.isdir(file_path):
shutil.rmtree(file_path) # Remove subdirectory
except Exception as e:
print(f"Failed to delete {file_path}. Reason: {e}")
delete_everything(UPLOAD_FOLDER, PUBLIC_SCREENSHOT_FOLDER)
valid_index = {base10_to_base36(i) for i in range(1, 201)}
latest_uploaded_file = {
"filename": None,
"content": None
}
#--------------------------------------------------------
#DISPLAY
@app.route("/")
def index():
return render_template("index.html")
@app.errorhandler(500)
def internal_server_error(e):
return jsonify({"error": "Internal Server Error", "details": str(e)}), 500
#--------------------------------------------------
#MAIN FUNCTION INITIALIZED FROM HTML USER INTERFACE:
#--------------------------------------------------
@app.route("/upload-largest", methods=["POST"])
def upload_largest():
file = request.files.get("file")
if file.filename == "":
return "Empty filename", 400
content = file.read().decode("utf-8")
# store result globally so other routes can use it
latest_uploaded_file["filename"] = file.filename
print(latest_uploaded_file["filename"])
latest_uploaded_file["content"] = content
return "OK", 200
@app.route("/process", methods=["POST"])
def process():
delete_everything(UPLOAD_FOLDER, PUBLIC_SCREENSHOT_FOLDER)
global stl_list
# ============== DIAGNOSTIC: Check raw uploaded filenames ==============
uploaded_files = request.files.getlist("STLs")
print(f"\n📤 Raw uploaded filenames from browser ({len(uploaded_files)} files):")
for f in uploaded_files[:10]:
print(f" {f.filename}")
# Check for duplicates in uploaded filenames
from collections import Counter
filenames = [f for f in uploaded_files]
# print("Finding dupes")
# for f1 in filenames:
# if " " in f1[41:47]: print(f1)
dupes = [name for name, count in Counter(filenames).items() if count > 1]
if dupes:
print(f"🚨 DUPLICATE FILENAMES IN UPLOAD: {dupes[:5]}")
print()
print(filenames[0])
# ============== END DIAGNOSTIC ==============
print()
print()
print("Received request")
print("📝 Received form data:", request.form)
global extracted_data
if "STLs" not in request.files:
return jsonify({"error": "No STLs uploaded"}), 400
# Get input info
match = re.search(r'(?<=ID)(\d{4})', str(filenames[0]))
if match is None: match = re.search( r'(?:BA|PRO0)0*(\d+)',str(filenames[0]))
if match:
job_ID = match.group(1)
print(f"Job ID: {job_ID}")
if not job_ID or len(job_ID) < 3:
return jsonify({"error": "Invalid Job ID"}), 400
session['job_ID'] = job_ID
# Make storage containers
extracted_data = []
seen_values = {}
# Establish temp folders and clear them before starting new job
output_folder_filled = "filled_images"
os.makedirs(output_folder_filled, exist_ok=True)
clear_folder(output_folder_filled)
# Save STL files and generate images
stl_files_folder = save_stl_files(request.files.getlist("STLs"))
# ============== DIAGNOSTIC: Check saved filenames ==============
saved_files = sorted(os.listdir(stl_files_folder))
print(f"\n📂 Saved {len(saved_files)} files to temp folder")
print(f"📄 First 10 saved filenames:")
for f in saved_files[:10]:
match = re.search(r'_(\d{3})\_', f) if not "ICONS" in f else re.search(r'_(\d+)_', f)
extracted = match.group(1) if match else "NO MATCH"
print(f" {f[:70]}...")
print(f" → extracts: '{extracted}'")
print()
# Check what numbers will be extracted
from collections import Counter
extracted_numbers = []
for f in saved_files:
match = re.search(r'_(\d{3})\_', f) if not "ICONS" in f else re.search(r'_(\d+)_', f)
if match:
extracted_numbers.append(match.group(1))
number_counts = Counter(extracted_numbers)
dupes = {n: c for n, c in number_counts.items() if c > 1}
if dupes:
print(f"🚨 DUPLICATE EXTRACTED NUMBERS: {dupes}")
else:
print(f"✅ All {len(extracted_numbers)} extracted numbers are unique")
# ============== END DIAGNOSTIC ==============
_, stl_list, langs = get_images(stl_files_folder, output_folder_filled)
save_log_to_json(stl_list)
extracted_texts = []
# Process images and extract text
for _, file in enumerate(os.listdir(output_folder_filled)):
filename = secure_filename(file)
image_path = os.path.join(UPLOAD_FOLDER, filename)
# Copy the processed image to static folder
file_path = os.path.join(output_folder_filled, file)
with open(file_path, 'rb') as f:
with open(image_path, 'wb') as out_file:
out_file.write(f.read())
for name in langs:
if file in name: language = langs[name]
else: language = "eng"
# Extract text from image
extracted_text = extract_text_from_image(image_path, lang=language)
extracted_texts.append(extracted_text)
if extracted_text == "NOT RECOGNIZED":
print(f"Warning: No text recognized in {filename}, skipping...")
continue
batch_number = get_batch_number(extracted_texts)
print(f"✅ Determined Batch Number: {batch_number}")
# Validate and correct extracted text
for file, extracted_text in zip(os.listdir(output_folder_filled), extracted_texts):
filename = secure_filename(file)
image_path = os.path.join(output_folder_filled, filename)
corrected_text, color = validate_and_correct(extracted_text, batch_number)
# Extract first two characters
first_two_chars = corrected_text[:2]
# Check for duplicates
if first_two_chars in seen_values:
color = "yellow"
first_index = seen_values[first_two_chars]
extracted_data[first_index]["color"] = "yellow"
else:
seen_values[first_two_chars] = len(extracted_data)
color = "normal"
extracted_data.append({
"filename": filename,
"screenshot_path": f"/static/stl_screenshots/{os.path.splitext(filename)[0]}.png",
"image_path": f"/static/uploads/{filename}",
"text": corrected_text,
"color": color
})
# Save extracted data to JSON & CSV
save_to_json_and_csv(extracted_data)
return jsonify(extracted_data), {"batch_number": batch_number}
#Update values function
@app.route("/update_values", methods=["POST"])
def update_values():
global extracted_data
updated_data = request.json.get("updated_data", [])
if not extracted_data:
return jsonify({"error": "No existing extracted data found."}), 400
# Update the records with the new corrected values and calculate the cavity number
for item in updated_data:
filename = item.get("filename")
corrected_text = item.get("corrected_text")
# Find the corresponding data and update
for data in extracted_data:
if data["filename"] == filename:
data["text"] = corrected_text
# Calculate the cavity number (first two characters of the corrected text)
data["cavity_number"] = corrected_text[:2]
# ✅ Step 2: Recalculate duplicates (FIX ADDED HERE)
seen_values = {}
for idx, data in enumerate(extracted_data):
first_two_chars = data["text"][:2]
if first_two_chars in seen_values:
# Mark current and original as duplicate
data["color"] = "yellow"
first_index = seen_values[first_two_chars]
extracted_data[first_index]["color"] = "yellow"
else:
seen_values[first_two_chars] = idx
data["color"] = "normal"
# ✅ Step 3: Save updated data
save_to_json_and_csv(extracted_data)
return jsonify({
"message": "Updated successfully!",
"data": extracted_data
})
'''STEP 3: Export sorted data'''
@app.route('/export_csv', methods=['POST'])
def export_csv():
job_ID = session.get("job_ID")
try:
with open('data.json', 'r') as file:
data = json.load(file)
except FileNotFoundError:
print("🚨 No data found. Starting fresh.")
# print("🚀 Export triggered. Form keys:", list(request.form.keys()))
# print("📂 Files:", list(request.files.keys()))
with open('processing_log.json', 'r') as f0:
name = json.load(f0)
# Process the data and extract the necessary fields
stored_data = []
for entry in data:
# Extract filename without extension
filename_without_extension = entry['filename'].split('.')[0]
for id in name:
if filename_without_extension in id: stl_name = name[id]["File"]
if "PANDA" in stl_name: ftype = "PANDA"
if "KIWI" in stl_name: ftype = "KIWI"
if "ICONS" in stl_name: ftype = "ICONS"
# Extract cavity number (if it exists)
cavity_number_base36 = entry.get('cavity_number')
text = entry.get('text')
print_code = text[2]
if cavity_number_base36:
try:
# Convert from base 36 to base 10
cavity_number_base10 = int(cavity_number_base36, 36)
# Store the converted value
stored_data.append({
'filename': filename_without_extension,
'cavity_number': cavity_number_base10
})
except ValueError:
# Handle invalid base 36 values gracefully
print(f"Warning: Invalid cavity number '{cavity_number_base36}' in base 36")
# Print the stored data for debugging
# print(f"🚨 Stored Data: {stored_data}")
file_type = ftype
printer = print_code
print(print_code)
# print(f"exportcsv: {printer}")
if not data:
return jsonify({"error": "No valid data received"}), 400
# Get form data lists
filenames = request.form.getlist("filename")
cavity_numbers = request.form.getlist("cavity_number")
if not filenames or not cavity_numbers:
print("❌ Missing filename or cavity number data.")
return jsonify({"error": "Missing filename or cavity number data"}), 400
if len(filenames) != len(cavity_numbers):
print("❌ Mismatched lengths between filenames and cavity numbers.")
return jsonify({"error": "Mismatched data lengths"}), 400
mcs_file = latest_uploaded_file["content"]
# Save MCS file if included
if mcs_file:
mcs_filename = os.path.basename(latest_uploaded_file["filename"])
mcs_file_path = os.path.join(UPLOAD_FOLDER, mcs_filename)
with open(mcs_file_path, "w", encoding="utf-8") as f:
f.write(mcs_file)
print(f"✅ MCS file saved to: {mcs_file_path}")
else:
print("⚠️ No MCS file uploaded.")
# Save the mapping file
saved_file_path = save_mapping_file(file_type, stored_data, job_ID, printer)
if saved_file_path is None:
return jsonify({"error": "Failed to save the mapping file"}), 500
sorted_file_path = update_merged_data(file_type, mcs_file_path, job_ID, printer)
# Check if sorted_merged_data.txt was created
if not os.path.exists(sorted_file_path):
return jsonify({"error": "Failed to generate sorted merged data"}), 500
return jsonify({
"message": "Files saved successfully.",
"mapping_file": saved_file_path,
"sorted_merged_file": sorted_file_path
}), 200
#keeps webapp connected
if __name__ == "__main__":
app.run(debug=True, host="127.0.0.1", port=9000)