Shuairong commited on
Commit
4f18a5c
·
verified ·
1 Parent(s): bab4e27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -59
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
- # Gradio Interface
206
- def process_images(folder_path):
207
- if isinstance(folder_path, dict):
208
- folder_path = folder_path['name']
209
- result = load_images_with_progress(folder_path)
210
- if isinstance(result, str): # Error message
211
- return result, None, None, None, None
212
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
- with gr.Blocks() as demo:
215
- gr.Markdown("# Image Cropping Tool with Gradio")
216
-
217
- folder_input = gr.File(label="Upload Folder of Images", file_count="directory")
218
- process_button = gr.Button("Process Images")
219
- original_image_output = gr.Image(label="Original Image")
220
- cropped_image_output = gr.Image(label="Cropped Image")
221
- meter_value_output = gr.Textbox(label="Meter Value")
222
- room_number_output = gr.Textbox(label="Room Number")
223
- room_numbers = gr.State([])
224
- meter_values = gr.State([])
225
- status_output = gr.Textbox(label="Status", interactive=False) # Add a status output
226
-
227
- process_button.click(
228
- fn=process_images,
229
- inputs=[folder_input],
230
- outputs=[original_image_output, cropped_image_output, meter_value_output, room_number_output, room_numbers, meter_values]
231
- )
232
-
233
- next_button = gr.Button("Next Image")
234
- prev_button = gr.Button("Previous Image")
235
- export_button = gr.Button("Export to Excel")
236
-
237
- next_button.click(
238
- fn=next_image,
239
- outputs=[original_image_output, cropped_image_output, meter_value_output, room_number_output]
240
- )
241
-
242
- prev_button.click(
243
- fn=prev_image,
244
- outputs=[original_image_output, cropped_image_output, meter_value_output, room_number_output]
245
- )
246
-
247
- export_button.click(
248
- fn=export_to_excel,
249
- inputs=[room_numbers, meter_values],
250
- outputs=[status_output] # Update the output to status_output
251
- )
252
-
253
- room_number_output.change(
254
- fn=update_room_number,
255
- inputs=[room_number_output],
256
- outputs=[room_number_output]
257
- )
258
-
259
- meter_value_output.change(
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)