VietCat commited on
Commit
cfe01e5
·
1 Parent(s): 4da1757

Add input validation to prevent None image errors

Browse files

- Check if image is None before processing
- Validate image dimensions (must be 3D array)
- Return None, None gracefully if validation fails
- Fixes cv2.cvtColor() assertion error when no image uploaded

Files changed (1) hide show
  1. app.py +10 -0
app.py CHANGED
@@ -10,10 +10,20 @@ def detect_traffic_signs(image, confidence_threshold):
10
  """
11
  Process the uploaded image and return the image with detected signs.
12
  """
 
 
 
 
 
13
  print(f"Received image type: {type(image)}")
14
  if hasattr(image, 'convert'):
15
  image = np.array(image)
16
  print(f"Converted PIL to numpy array, shape: {image.shape}")
 
 
 
 
 
17
 
18
  # Convert RGB to BGR for OpenCV
19
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
 
10
  """
11
  Process the uploaded image and return the image with detected signs.
12
  """
13
+ # Validate input image
14
+ if image is None:
15
+ print("No image provided")
16
+ return None, None
17
+
18
  print(f"Received image type: {type(image)}")
19
  if hasattr(image, 'convert'):
20
  image = np.array(image)
21
  print(f"Converted PIL to numpy array, shape: {image.shape}")
22
+
23
+ # Check if image is valid
24
+ if image.size == 0 or len(image.shape) != 3:
25
+ print(f"Invalid image: shape={image.shape}")
26
+ return None, None
27
 
28
  # Convert RGB to BGR for OpenCV
29
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)