Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -128,9 +128,10 @@ def prev_image(room_number, meter_value):
|
|
| 128 |
# Function to export to Excel
|
| 129 |
def export_to_excel(room_numbers_state, meter_values_state):
|
| 130 |
try:
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
| 134 |
annotations[current_index]["room_number"] = room_numbers_state.get("room_number", "")
|
| 135 |
annotations[current_index]["meter_value"] = meter_values_state.get("meter_value", "")
|
| 136 |
|
|
@@ -143,6 +144,7 @@ def export_to_excel(room_numbers_state, meter_values_state):
|
|
| 143 |
"room_number": room_number,
|
| 144 |
"meter_value": meter_value
|
| 145 |
})
|
|
|
|
| 146 |
df = pd.DataFrame(data)
|
| 147 |
temp_dir = tempfile.mkdtemp()
|
| 148 |
output_excel = Path(temp_dir) / "results.xlsx"
|
|
@@ -153,32 +155,44 @@ def export_to_excel(room_numbers_state, meter_values_state):
|
|
| 153 |
images_dir = Path(temp_dir) / folder_name
|
| 154 |
images_dir.mkdir(exist_ok=True)
|
| 155 |
|
| 156 |
-
# Save both
|
| 157 |
for idx, annotation in enumerate(annotations):
|
| 158 |
room_number = annotation.get("room_number", "")
|
|
|
|
|
|
|
|
|
|
| 159 |
cropped_image = annotation.get("cropped_image")
|
| 160 |
original_image = annotation.get("original_image")
|
| 161 |
|
| 162 |
-
if
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
cropped_path = images_dir / f"{room_number}_meter.jpg"
|
| 166 |
-
cropped_image.save(cropped_path)
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
original_image.save(original_path)
|
| 172 |
|
| 173 |
zip_path = Path(temp_dir) / f"{folder_name}_images.zip"
|
| 174 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 175 |
for img_file in images_dir.glob("*.jpg"):
|
| 176 |
zipf.write(img_file, arcname=img_file.name)
|
| 177 |
|
|
|
|
| 178 |
return str(output_excel), str(zip_path)
|
|
|
|
| 179 |
except Exception as e:
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
|
|
|
| 182 |
# Function to update room number
|
| 183 |
def update_room_number(room_number):
|
| 184 |
if annotations:
|
|
|
|
| 128 |
# Function to export to Excel
|
| 129 |
def export_to_excel(room_numbers_state, meter_values_state):
|
| 130 |
try:
|
| 131 |
+
global current_index, annotations
|
| 132 |
+
|
| 133 |
+
# Save current room number and meter value before exporting
|
| 134 |
+
if annotations and current_index < len(annotations):
|
| 135 |
annotations[current_index]["room_number"] = room_numbers_state.get("room_number", "")
|
| 136 |
annotations[current_index]["meter_value"] = meter_values_state.get("meter_value", "")
|
| 137 |
|
|
|
|
| 144 |
"room_number": room_number,
|
| 145 |
"meter_value": meter_value
|
| 146 |
})
|
| 147 |
+
|
| 148 |
df = pd.DataFrame(data)
|
| 149 |
temp_dir = tempfile.mkdtemp()
|
| 150 |
output_excel = Path(temp_dir) / "results.xlsx"
|
|
|
|
| 155 |
images_dir = Path(temp_dir) / folder_name
|
| 156 |
images_dir.mkdir(exist_ok=True)
|
| 157 |
|
| 158 |
+
# Save both original and cropped images with room numbers
|
| 159 |
for idx, annotation in enumerate(annotations):
|
| 160 |
room_number = annotation.get("room_number", "")
|
| 161 |
+
if not room_number:
|
| 162 |
+
continue # skip if no room number assigned
|
| 163 |
+
|
| 164 |
cropped_image = annotation.get("cropped_image")
|
| 165 |
original_image = annotation.get("original_image")
|
| 166 |
|
| 167 |
+
if cropped_image:
|
| 168 |
+
cropped_path = images_dir / f"{room_number}_meter.jpg"
|
| 169 |
+
cropped_image.save(cropped_path)
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
if original_image:
|
| 172 |
+
original_path = images_dir / f"{room_number}_original.jpg"
|
| 173 |
+
original_image.save(original_path)
|
|
|
|
| 174 |
|
| 175 |
zip_path = Path(temp_dir) / f"{folder_name}_images.zip"
|
| 176 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 177 |
for img_file in images_dir.glob("*.jpg"):
|
| 178 |
zipf.write(img_file, arcname=img_file.name)
|
| 179 |
|
| 180 |
+
# Return valid file paths
|
| 181 |
return str(output_excel), str(zip_path)
|
| 182 |
+
|
| 183 |
except Exception as e:
|
| 184 |
+
# Create dummy/error files to prevent invalid path errors
|
| 185 |
+
error_dir = tempfile.mkdtemp()
|
| 186 |
+
error_excel = Path(error_dir) / "error.xlsx"
|
| 187 |
+
error_zip = Path(error_dir) / "error.zip"
|
| 188 |
+
|
| 189 |
+
with open(error_excel, 'w') as f:
|
| 190 |
+
f.write(f"Error during export: {str(e)}")
|
| 191 |
+
|
| 192 |
+
with open(error_zip, 'w') as f:
|
| 193 |
+
f.write(f"Error during export: {str(e)}")
|
| 194 |
|
| 195 |
+
return str(error_excel), str(error_zip)
|
| 196 |
# Function to update room number
|
| 197 |
def update_room_number(room_number):
|
| 198 |
if annotations:
|