Ruggero1912 commited on
Commit
3249c76
·
1 Parent(s): 012925a

workaround for imageeditor throwing errors in browser console

Browse files
Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -199,10 +199,16 @@ def process_image_trace_to_coordinates(image_editor_data) -> List[List[Dict[str,
199
  # Skip if layer is not a PIL Image or is empty
200
  if not isinstance(layer, Image.Image):
201
  print(f"[DEBUG] Layer {i} is not a PIL Image")
202
- continue
203
-
204
- # Convert layer to numpy array to find non-transparent pixels
205
- layer_array = np.array(layer)
 
 
 
 
 
 
206
 
207
  # Find non-transparent pixels (alpha > 0)
208
  if layer_array.shape[2] == 4: # RGBA
@@ -841,13 +847,13 @@ def create_gradio_interface(model_config_name : str):
841
  image_editor = gr.ImageEditor(
842
  elem_id="image_editor",
843
  label="Upload image and draw traces",
844
- type="pil",
845
- crop_size=None,
846
  brush=gr.Brush(default_size=10, colors=[colors[color_index]], color_mode="fixed"), # orange with ~60% opacity
847
  visible=True,
848
  eraser=False,
849
  transforms=[],
850
- height=600,
851
  #layers=gr.LayerOptions(allow_additional_layers=True, disabled=True),
852
  )
853
 
@@ -921,7 +927,8 @@ def create_gradio_interface(model_config_name : str):
921
  example_images = get_example_images()
922
  if evt.index < len(example_images):
923
  selected_image_path = example_images[evt.index]
924
- img = Image.open(selected_image_path)
 
925
  # For ImageEditor, return the PIL image directly
926
  # For image_annotator, return dict format as expected by the component
927
  if not USE_BBOX_ANNOTATOR:
@@ -932,6 +939,10 @@ def create_gradio_interface(model_config_name : str):
932
  }
933
  else:
934
  annotated_format = tuple((selected_image_path, []))
 
 
 
 
935
  return img, annotated_format
936
  return None, empty_annotated_format
937
  except Exception as e:
 
199
  # Skip if layer is not a PIL Image or is empty
200
  if not isinstance(layer, Image.Image):
201
  print(f"[DEBUG] Layer {i} is not a PIL Image")
202
+ # try to parse from numpy array if possible
203
+ if isinstance(layer, np.ndarray):
204
+ layer_array = layer
205
+ layer = Image.fromarray(layer)
206
+ print(f"[DEBUG] Layer {i} converted from numpy array to PIL Image")
207
+ else:
208
+ continue
209
+ else:
210
+ # Convert layer to numpy array to find non-transparent pixels
211
+ layer_array = np.array(layer)
212
 
213
  # Find non-transparent pixels (alpha > 0)
214
  if layer_array.shape[2] == 4: # RGBA
 
847
  image_editor = gr.ImageEditor(
848
  elem_id="image_editor",
849
  label="Upload image and draw traces",
850
+ type="numpy",
851
+ #crop_size=None,
852
  brush=gr.Brush(default_size=10, colors=[colors[color_index]], color_mode="fixed"), # orange with ~60% opacity
853
  visible=True,
854
  eraser=False,
855
  transforms=[],
856
+ height=600,
857
  #layers=gr.LayerOptions(allow_additional_layers=True, disabled=True),
858
  )
859
 
 
927
  example_images = get_example_images()
928
  if evt.index < len(example_images):
929
  selected_image_path = example_images[evt.index]
930
+ img = Image.open(selected_image_path).convert('RGB')
931
+
932
  # For ImageEditor, return the PIL image directly
933
  # For image_annotator, return dict format as expected by the component
934
  if not USE_BBOX_ANNOTATOR:
 
939
  }
940
  else:
941
  annotated_format = tuple((selected_image_path, []))
942
+
943
+ # convert to numpy array for ImageEditor
944
+ img = np.array(img)
945
+
946
  return img, annotated_format
947
  return None, empty_annotated_format
948
  except Exception as e: