dgith commited on
Commit
2b9d286
·
verified ·
1 Parent(s): 23fc7c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -2
app.py CHANGED
@@ -5,6 +5,7 @@ import tensorflow as tf
5
  import os
6
  import cv2
7
  import pandas as pd
 
8
 
9
  # Define model path
10
  MODEL_DIR = "model"
@@ -17,7 +18,7 @@ print("Checking for existing model...")
17
  if os.path.exists(MODEL_PATH):
18
  print("✅ Loading pre-trained model...")
19
  model = tf.keras.models.load_model(MODEL_PATH)
20
- model.summary() # Print model summary for debugging
21
  else:
22
  print("❌ Model not found. Training new model...")
23
  # Load and preprocess data
@@ -53,13 +54,22 @@ def preprocess_image(image):
53
  print("Warning: Received None as image input.")
54
  return None
55
 
56
- # Handle Gradio ImageEditor/UploadButton output
57
  if isinstance(image, dict):
58
  image = image.get('image', None)
59
  if image is None:
60
  print("Warning: No image found in dictionary.")
61
  return None
62
 
 
 
 
 
 
 
 
 
 
63
  # Convert PIL image to NumPy array if needed
64
  if hasattr(image, 'convert'):
65
  image = np.array(image.convert('L'))
 
5
  import os
6
  import cv2
7
  import pandas as pd
8
+ from PIL import Image
9
 
10
  # Define model path
11
  MODEL_DIR = "model"
 
18
  if os.path.exists(MODEL_PATH):
19
  print("✅ Loading pre-trained model...")
20
  model = tf.keras.models.load_model(MODEL_PATH)
21
+ model.summary()
22
  else:
23
  print("❌ Model not found. Training new model...")
24
  # Load and preprocess data
 
54
  print("Warning: Received None as image input.")
55
  return None
56
 
57
+ # Handle Gradio ImageEditor output (dict with 'image' key)
58
  if isinstance(image, dict):
59
  image = image.get('image', None)
60
  if image is None:
61
  print("Warning: No image found in dictionary.")
62
  return None
63
 
64
+ # Handle Gradio UploadButton output (file path or PIL image)
65
+ if isinstance(image, str):
66
+ try:
67
+ image = Image.open(image)
68
+ image = np.array(image.convert('L'))
69
+ except Exception as e:
70
+ print(f"Error opening uploaded image: {e}")
71
+ return None
72
+
73
  # Convert PIL image to NumPy array if needed
74
  if hasattr(image, 'convert'):
75
  image = np.array(image.convert('L'))