Matan Kriel commited on
Commit
72e2859
·
1 Parent(s): 7b4d8e5

updated app

Browse files
Files changed (1) hide show
  1. app.py +56 -7
app.py CHANGED
@@ -39,7 +39,53 @@ else:
39
  DB_VECTORS = None
40
  MODEL_NAME = "Unknown"
41
 
42
- # --- 2. Define the Search Logic ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  def find_best_matches(user_image):
44
  # Error handling for empty inputs
45
  if user_image is None:
@@ -75,14 +121,17 @@ def find_best_matches(user_image):
75
  display_name = f"{row['name']} (Match: {int(score*100)}%)"
76
  result_text += f"### #{i}: {display_name}\n\n"
77
 
78
- # Load Image - only add to gallery if image exists and loads successfully
79
  try:
80
- if os.path.exists(row['image_path']):
81
- img = Image.open(row['image_path'])
82
- gallery_images.append((img, display_name))
83
- else:
84
- result_text += f"⚠️ Image file not found: {row['image_path']}\n\n"
85
  except Exception as img_error:
 
 
 
86
  result_text += f"⚠️ Could not load image: {str(img_error)}\n\n"
87
 
88
  # Don't pad with None - Gallery can't handle None images
 
39
  DB_VECTORS = None
40
  MODEL_NAME = "Unknown"
41
 
42
+ # --- 2. Helper function to load images with fallbacks ---
43
+ def load_image_with_fallback(image_path, name):
44
+ """Try to load image from various possible locations, or create placeholder"""
45
+ # Try original path
46
+ if os.path.exists(image_path):
47
+ try:
48
+ return Image.open(image_path)
49
+ except:
50
+ pass
51
+
52
+ # Try just the filename in current directory
53
+ filename = os.path.basename(image_path)
54
+ if os.path.exists(filename):
55
+ try:
56
+ return Image.open(filename)
57
+ except:
58
+ pass
59
+
60
+ # Try in my_dataset directory
61
+ dataset_path = os.path.join("my_dataset", filename)
62
+ if os.path.exists(dataset_path):
63
+ try:
64
+ return Image.open(dataset_path)
65
+ except:
66
+ pass
67
+
68
+ # Create a placeholder image if nothing found
69
+ placeholder = Image.new('RGB', (200, 200), color=(220, 220, 220))
70
+ from PIL import ImageDraw, ImageFont
71
+ draw = ImageDraw.Draw(placeholder)
72
+ # Try to use default font, fallback to basic if not available
73
+ try:
74
+ font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
75
+ except:
76
+ font = ImageFont.load_default()
77
+
78
+ # Draw text in center
79
+ text = f"Image\nNot Found"
80
+ bbox = draw.textbbox((0, 0), text, font=font)
81
+ text_width = bbox[2] - bbox[0]
82
+ text_height = bbox[3] - bbox[1]
83
+ position = ((200 - text_width) // 2, (200 - text_height) // 2)
84
+ draw.text(position, text, fill=(100, 100, 100), font=font)
85
+
86
+ return placeholder
87
+
88
+ # --- 3. Define the Search Logic ---
89
  def find_best_matches(user_image):
90
  # Error handling for empty inputs
91
  if user_image is None:
 
121
  display_name = f"{row['name']} (Match: {int(score*100)}%)"
122
  result_text += f"### #{i}: {display_name}\n\n"
123
 
124
+ # Load Image with fallback - always add to gallery (with placeholder if needed)
125
  try:
126
+ img = load_image_with_fallback(row['image_path'], row['name'])
127
+ gallery_images.append((img, display_name))
128
+ # Check if we used a placeholder
129
+ if not os.path.exists(row['image_path']) and not os.path.exists(os.path.basename(row['image_path'])):
130
+ result_text += f"⚠️ Image not found at: {row['image_path']} (showing placeholder)\n\n"
131
  except Exception as img_error:
132
+ # Even if loading fails, create placeholder
133
+ placeholder = Image.new('RGB', (200, 200), color=(220, 220, 220))
134
+ gallery_images.append((placeholder, display_name))
135
  result_text += f"⚠️ Could not load image: {str(img_error)}\n\n"
136
 
137
  # Don't pad with None - Gallery can't handle None images