Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import tensorflow as tf
|
| 5 |
-
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
return intersection / (union + 1e-7)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
# Convert PIL to NumPy array
|
| 28 |
-
img_np = np.array(image)
|
| 29 |
-
img_resized = cv2.resize(img_np, (IMG_WIDTH, IMG_HEIGHT)) / 255.0
|
| 30 |
-
img_input = np.expand_dims(img_resized, axis=0)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
# Create overlay
|
| 37 |
-
overlay =
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
return
|
| 45 |
|
| 46 |
-
# Gradio
|
| 47 |
interface = gr.Interface(
|
| 48 |
fn=predict,
|
| 49 |
-
inputs=gr.Image(type="pil"),
|
| 50 |
-
outputs=
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
],
|
| 54 |
-
title="Face Mask Segmentation",
|
| 55 |
-
description="Upload a face image to get the predicted segmentation mask using U-Net."
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
+
import cv2
|
| 5 |
|
| 6 |
+
# Load your trained Keras model
|
| 7 |
+
model = tf.keras.models.load_model("model.h5")
|
| 8 |
|
| 9 |
+
# Image preprocessing function (same as used during training)
|
| 10 |
+
def preprocess_image(img):
|
| 11 |
+
img_resized = cv2.resize(img, (256, 256))
|
| 12 |
+
img_normalized = img_resized / 255.0 # Normalize to 0-1
|
| 13 |
+
return img_normalized
|
|
|
|
| 14 |
|
| 15 |
+
# Prediction and overlay function
|
| 16 |
+
def predict(input_img):
|
| 17 |
+
# Ensure image is RGB and numpy array
|
| 18 |
+
img = np.array(input_img.convert("RGB"))
|
| 19 |
|
| 20 |
+
# Preprocess
|
| 21 |
+
preprocessed_img = preprocess_image(img)
|
| 22 |
+
input_tensor = np.expand_dims(preprocessed_img, axis=0) # Add batch dimension
|
| 23 |
|
| 24 |
+
# Model prediction
|
| 25 |
+
prediction = model.predict(input_tensor)[0] # Remove batch dim
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Post-processing mask
|
| 28 |
+
mask = (prediction > 0.5).astype(np.uint8) # Binary mask
|
| 29 |
+
mask_resized = cv2.resize(mask, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 30 |
|
| 31 |
# Create overlay
|
| 32 |
+
overlay = img.astype(np.float32) / 255.0 # Normalize input image
|
| 33 |
+
alpha = 0.5 # Transparency of overlay
|
| 34 |
+
|
| 35 |
+
# Create red mask in RGB format
|
| 36 |
+
red_mask = np.zeros_like(overlay)
|
| 37 |
+
red_mask[:, :, 0] = mask_resized # Red channel
|
| 38 |
|
| 39 |
+
# Alpha blend original image with red mask
|
| 40 |
+
blended = (1 - alpha) * overlay + alpha * red_mask
|
| 41 |
+
blended = np.clip(blended * 255, 0, 255).astype(np.uint8)
|
| 42 |
|
| 43 |
+
return blended
|
| 44 |
|
| 45 |
+
# Gradio interface
|
| 46 |
interface = gr.Interface(
|
| 47 |
fn=predict,
|
| 48 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 49 |
+
outputs=gr.Image(type="numpy", label="Segmented Image"),
|
| 50 |
+
title="Image Segmentation App",
|
| 51 |
+
description="Upload an image and get the segmentation mask overlay using your trained model."
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# Launch Gradio app (enable public link for Hugging Face Spaces)
|
| 55 |
+
interface.launch(share=True)
|