Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -202,64 +202,73 @@ def update_meter_value(meter_value):
|
|
| 202 |
annotations[current_index]["meter_value"] = meter_value
|
| 203 |
return meter_value
|
| 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 |
-
fn=update_meter_value,
|
| 261 |
-
inputs=[meter_value_output],
|
| 262 |
-
outputs=[meter_value_output]
|
| 263 |
-
)
|
| 264 |
|
| 265 |
-
demo.launch()
|
|
|
|
| 202 |
annotations[current_index]["meter_value"] = meter_value
|
| 203 |
return meter_value
|
| 204 |
|
| 205 |
+
def process_images(folder_input):
|
| 206 |
+
global annotations, current_index
|
| 207 |
+
|
| 208 |
+
if folder_input is None:
|
| 209 |
+
# Return default values if no folder is selected
|
| 210 |
+
return (
|
| 211 |
+
None, # original_image_output
|
| 212 |
+
None, # cropped_image_output
|
| 213 |
+
None, # meter_value_output
|
| 214 |
+
None, # room_number_output
|
| 215 |
+
[], # room_numbers (state)
|
| 216 |
+
[] # meter_values (state)
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
try:
|
| 220 |
+
folder_path = folder_input['name'] # Extract path from folder input
|
| 221 |
+
annotations.clear()
|
| 222 |
+
current_index = 0
|
| 223 |
+
|
| 224 |
+
image_files = [f for f in os.listdir(folder_path) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 225 |
+
total_images = len(image_files)
|
| 226 |
|
| 227 |
+
for i, image_file in enumerate(tqdm(image_files, desc="Processing Images")):
|
| 228 |
+
image_path = os.path.join(folder_path, image_file)
|
| 229 |
+
original_image = Image.open(image_path)
|
| 230 |
+
detections = detect_objects(image_path)
|
| 231 |
+
|
| 232 |
+
if detections:
|
| 233 |
+
cropped_images = crop_image(image_path, detections)
|
| 234 |
+
for cropped_image in cropped_images:
|
| 235 |
+
annotation = {
|
| 236 |
+
"image_path": image_path,
|
| 237 |
+
"cropped_image": cropped_image,
|
| 238 |
+
"meter_value": perform_ocr(cropped_image),
|
| 239 |
+
"original_image": original_image.copy(),
|
| 240 |
+
"room_number": ""
|
| 241 |
+
}
|
| 242 |
+
annotations.append(annotation)
|
| 243 |
+
else:
|
| 244 |
+
annotation = {
|
| 245 |
+
"image_path": image_path,
|
| 246 |
+
"cropped_image": None,
|
| 247 |
+
"meter_value": "",
|
| 248 |
+
"original_image": original_image.copy(),
|
| 249 |
+
"room_number": ""
|
| 250 |
+
}
|
| 251 |
+
annotations.append(annotation)
|
| 252 |
+
|
| 253 |
+
if annotations:
|
| 254 |
+
current_index = 0
|
| 255 |
+
result = update_gui()
|
| 256 |
+
room_numbers = [a["room_number"] for a in annotations]
|
| 257 |
+
meter_values = [a["meter_value"] for a in annotations]
|
| 258 |
+
return (*result, room_numbers, meter_values)
|
| 259 |
+
|
| 260 |
+
# Default return in case no annotations were created
|
| 261 |
+
return (None, None, "", "", [], [])
|
| 262 |
+
|
| 263 |
+
except Exception as e:
|
| 264 |
+
# Ensure all 6 outputs are returned even on error
|
| 265 |
+
return (
|
| 266 |
+
f"Error: {str(e)}", # original_image_output (textual error display)
|
| 267 |
+
None, # cropped_image_output
|
| 268 |
+
None, # meter_value_output
|
| 269 |
+
None, # room_number_output
|
| 270 |
+
[], # room_numbers (state)
|
| 271 |
+
[] # meter_values (state)
|
| 272 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
+
demo.launch(share=True)
|