TITO Vigninnou Lucien
commited on
Commit
·
28f9e35
1
Parent(s):
eeee0ff
Add application file
Browse files- app.py +36 -0
- segmentation.py +30 -0
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from segmentation import segment_image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
|
| 6 |
+
# Image de test par défaut
|
| 7 |
+
default_image_path = "./image.png"
|
| 8 |
+
|
| 9 |
+
def segment_and_display(image_path=default_image_path):
|
| 10 |
+
# Appeler la fonction de segmentation
|
| 11 |
+
original_image, segmented_image = segment_image(image_path)
|
| 12 |
+
|
| 13 |
+
# Retourner les images pour l'affichage
|
| 14 |
+
return original_image, segmented_image
|
| 15 |
+
|
| 16 |
+
# Charger l'image de test par défaut
|
| 17 |
+
default_original_image, default_segmented_image = segment_image(default_image_path)
|
| 18 |
+
|
| 19 |
+
# Interface Gradio
|
| 20 |
+
iface = gr.Interface(
|
| 21 |
+
fn=segment_and_display,
|
| 22 |
+
inputs=gr.Image(type="filepath", label="Upload Image"),
|
| 23 |
+
outputs=[
|
| 24 |
+
gr.Image(type="numpy", label="Original Image"),
|
| 25 |
+
gr.Image(type="numpy", label="Segmented Image")
|
| 26 |
+
],
|
| 27 |
+
title="Image Segmentation with K-means (k=2)",
|
| 28 |
+
description="Upload an image or use the default test image to see the segmentation result.",
|
| 29 |
+
examples=[
|
| 30 |
+
[default_image_path]
|
| 31 |
+
],
|
| 32 |
+
live=True # Permet de voir les changements en temps réel
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Afficher l'image de test par défaut lorsque l'interface est ouverte
|
| 36 |
+
iface.launch(share=True, inline=True)
|
segmentation.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from sklearn.cluster import KMeans
|
| 4 |
+
|
| 5 |
+
def segment_image(image_path, k=2, resize_shape=(256, 256)):
|
| 6 |
+
# Charger l'image
|
| 7 |
+
img = cv2.imread(image_path)
|
| 8 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convertir en RGB
|
| 9 |
+
img = cv2.resize(img, resize_shape) # Redimensionner l'image
|
| 10 |
+
|
| 11 |
+
# Normaliser l'image
|
| 12 |
+
img_normalized = img / 255.0
|
| 13 |
+
|
| 14 |
+
# Réduire le bruit
|
| 15 |
+
img_denoised = cv2.GaussianBlur(img_normalized, (5, 5), 0)
|
| 16 |
+
|
| 17 |
+
# Aplatir l'image pour l'appliquer à K-means
|
| 18 |
+
img_flat = img_denoised.reshape(-1, 3)
|
| 19 |
+
|
| 20 |
+
# Appliquer K-means
|
| 21 |
+
kmeans = KMeans(n_clusters=k, random_state=0).fit(img_flat)
|
| 22 |
+
labels = kmeans.labels_
|
| 23 |
+
|
| 24 |
+
# Réorganiser les labels en image
|
| 25 |
+
segmented_img = labels.reshape(resize_shape)
|
| 26 |
+
|
| 27 |
+
# Convertir l'image segmentée en niveaux de gris
|
| 28 |
+
segmented_img_gray = (segmented_img * 255).astype(np.uint8)
|
| 29 |
+
|
| 30 |
+
return img, segmented_img_gray
|