chelleboyer commited on
Commit
b605fb0
Β·
1 Parent(s): 67694da

Fix placeholder text and enhance image validation

Browse files
Files changed (2) hide show
  1. .chainlit/config.toml +1 -0
  2. app.py +37 -9
.chainlit/config.toml CHANGED
@@ -120,3 +120,4 @@ font_family = "Inter"
120
  [UI.chat]
121
  background_image = "assets/PP_backgound.jpg"
122
  background_image_opacity = 0.3
 
 
120
  [UI.chat]
121
  background_image = "assets/PP_backgound.jpg"
122
  background_image_opacity = 0.3
123
+ chat_input_placeholder = "Upload an image to analyze your shelf layout..."
app.py CHANGED
@@ -90,13 +90,27 @@ def compress_image(image, max_size=(400, 400)):
90
 
91
  def validate_image(image, name="image"):
92
  """Validate that an image is properly loaded and has valid dimensions."""
93
- if image is None:
94
- raise ValueError(f"Failed to load {name}")
95
- if image.size == 0:
96
- raise ValueError(f"{name} is empty")
97
- if len(image.shape) != 3:
98
- raise ValueError(f"{name} must be a color image (3 channels)")
99
- return True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  # Load planogram metadata
102
  try:
@@ -278,7 +292,14 @@ async def main(message: cl.Message):
278
  logger.error(f"Failed to read image from path: {img_path}")
279
  await processing_msg.update(content="❌ Error: Could not read the uploaded image. Please try again with a different image.")
280
  return
281
- logger.info(f"Successfully read image with shape: {uploaded_img.shape}")
 
 
 
 
 
 
 
282
 
283
  # Load reference image
284
  logger.info("Loading reference image")
@@ -287,7 +308,14 @@ async def main(message: cl.Message):
287
  logger.error("Failed to load reference image")
288
  await processing_msg.update(content="❌ Error: Reference planogram image not found.")
289
  return
290
- logger.info("Successfully loaded reference image")
 
 
 
 
 
 
 
291
 
292
  # Compare images using CLIP
293
  await processing_msg.update(content="πŸ”„ Comparing with reference planogram...")
 
90
 
91
  def validate_image(image, name="image"):
92
  """Validate that an image is properly loaded and has valid dimensions."""
93
+ try:
94
+ if image is None:
95
+ raise ValueError(f"Failed to load {name}")
96
+
97
+ if isinstance(image, np.ndarray):
98
+ if image.size == 0:
99
+ raise ValueError(f"{name} is empty")
100
+ if len(image.shape) != 3:
101
+ raise ValueError(f"{name} must be a color image (3 channels)")
102
+ logger.info(f"{name} shape: {image.shape}")
103
+ elif isinstance(image, Image.Image):
104
+ if image.size[0] == 0 or image.size[1] == 0:
105
+ raise ValueError(f"{name} has invalid dimensions")
106
+ logger.info(f"{name} size: {image.size}")
107
+ else:
108
+ raise ValueError(f"{name} must be a numpy array or PIL Image")
109
+
110
+ return True
111
+ except Exception as e:
112
+ logger.error(f"Error validating {name}: {str(e)}", exc_info=True)
113
+ raise
114
 
115
  # Load planogram metadata
116
  try:
 
292
  logger.error(f"Failed to read image from path: {img_path}")
293
  await processing_msg.update(content="❌ Error: Could not read the uploaded image. Please try again with a different image.")
294
  return
295
+
296
+ # Validate uploaded image
297
+ try:
298
+ validate_image(uploaded_img, "uploaded image")
299
+ except ValueError as e:
300
+ logger.error(f"Invalid uploaded image: {str(e)}")
301
+ await processing_msg.update(content=f"❌ Error: {str(e)}")
302
+ return
303
 
304
  # Load reference image
305
  logger.info("Loading reference image")
 
308
  logger.error("Failed to load reference image")
309
  await processing_msg.update(content="❌ Error: Reference planogram image not found.")
310
  return
311
+
312
+ # Validate reference image
313
+ try:
314
+ validate_image(ref_img, "reference image")
315
+ except ValueError as e:
316
+ logger.error(f"Invalid reference image: {str(e)}")
317
+ await processing_msg.update(content=f"❌ Error: {str(e)}")
318
+ return
319
 
320
  # Compare images using CLIP
321
  await processing_msg.update(content="πŸ”„ Comparing with reference planogram...")