Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,14 +38,17 @@ def allowed_file(filename):
|
|
| 38 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
|
| 39 |
|
| 40 |
def preprocess_image(image_path):
|
| 41 |
-
"""Preprocess image for U-Net model (256x256
|
| 42 |
try:
|
| 43 |
-
# Read image
|
| 44 |
-
img = cv2.imread(image_path
|
| 45 |
|
| 46 |
if img is None:
|
| 47 |
raise ValueError("Could not read image")
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
print(f"Original image shape: {img.shape}")
|
| 50 |
|
| 51 |
# Resize to 256x256
|
|
@@ -54,9 +57,8 @@ def preprocess_image(image_path):
|
|
| 54 |
# Normalize to [0, 1]
|
| 55 |
img_normalized = img_resized / 255.0
|
| 56 |
|
| 57 |
-
# Add
|
| 58 |
img_input = np.expand_dims(img_normalized, axis=0)
|
| 59 |
-
img_input = np.expand_dims(img_input, axis=-1)
|
| 60 |
|
| 61 |
print(f"Model input shape: {img_input.shape}")
|
| 62 |
|
|
@@ -80,23 +82,16 @@ def create_colored_mask(prediction, original_shape):
|
|
| 80 |
|
| 81 |
def create_overlay(original, mask, alpha=0.5):
|
| 82 |
"""Create overlay of original image and segmentation mask"""
|
| 83 |
-
#
|
| 84 |
-
if len(original.shape) == 2:
|
| 85 |
-
original_rgb = cv2.cvtColor(original, cv2.COLOR_GRAY2RGB)
|
| 86 |
-
else:
|
| 87 |
-
original_rgb = original
|
| 88 |
-
|
| 89 |
# Blend
|
| 90 |
-
overlay = cv2.addWeighted(
|
| 91 |
|
| 92 |
return overlay
|
| 93 |
|
| 94 |
def img_to_base64(img):
|
| 95 |
"""Convert numpy image to base64 string"""
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
else:
|
| 99 |
-
img_pil = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
| 100 |
|
| 101 |
buf = BytesIO()
|
| 102 |
img_pil.save(buf, format='PNG')
|
|
|
|
| 38 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
|
| 39 |
|
| 40 |
def preprocess_image(image_path):
|
| 41 |
+
"""Preprocess image for U-Net model (256x256 RGB)"""
|
| 42 |
try:
|
| 43 |
+
# Read image as RGB
|
| 44 |
+
img = cv2.imread(image_path)
|
| 45 |
|
| 46 |
if img is None:
|
| 47 |
raise ValueError("Could not read image")
|
| 48 |
|
| 49 |
+
# Convert BGR to RGB
|
| 50 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 51 |
+
|
| 52 |
print(f"Original image shape: {img.shape}")
|
| 53 |
|
| 54 |
# Resize to 256x256
|
|
|
|
| 57 |
# Normalize to [0, 1]
|
| 58 |
img_normalized = img_resized / 255.0
|
| 59 |
|
| 60 |
+
# Add batch dimension: (1, 256, 256, 3)
|
| 61 |
img_input = np.expand_dims(img_normalized, axis=0)
|
|
|
|
| 62 |
|
| 63 |
print(f"Model input shape: {img_input.shape}")
|
| 64 |
|
|
|
|
| 82 |
|
| 83 |
def create_overlay(original, mask, alpha=0.5):
|
| 84 |
"""Create overlay of original image and segmentation mask"""
|
| 85 |
+
# Original is already RGB
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# Blend
|
| 87 |
+
overlay = cv2.addWeighted(original, 1 - alpha, mask, alpha, 0)
|
| 88 |
|
| 89 |
return overlay
|
| 90 |
|
| 91 |
def img_to_base64(img):
|
| 92 |
"""Convert numpy image to base64 string"""
|
| 93 |
+
# Images are already in RGB format
|
| 94 |
+
img_pil = Image.fromarray(img.astype('uint8'))
|
|
|
|
|
|
|
| 95 |
|
| 96 |
buf = BytesIO()
|
| 97 |
img_pil.save(buf, format='PNG')
|