Spaces:
Runtime error
Runtime error
mrcryptsie commited on
Commit ·
2ffb4a2
1
Parent(s): 5255198
New updates
Browse files
app.py
CHANGED
|
@@ -2,27 +2,27 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
|
|
|
| 5 |
from skimage.color import rgb2gray
|
| 6 |
import PIL.ImageFilter
|
| 7 |
from scipy.ndimage import convolve
|
| 8 |
from skimage import morphology
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
#==========================================================================================
|
| 12 |
# 1. Charger l'image
|
| 13 |
def load_image(image):
|
| 14 |
return image
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
# Transformer l'image en niveau de gris
|
| 19 |
def gray(image):
|
| 20 |
image = np.array(image)
|
| 21 |
image_gris = rgb2gray(image)
|
| 22 |
return image_gris
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
# Transformer en blanc noir
|
| 27 |
def blanc_noir(image):
|
| 28 |
image = np.array(image)
|
|
@@ -30,133 +30,148 @@ def blanc_noir(image):
|
|
| 30 |
image_blanc_noir = np.where(image_gris > 0.5, 0, 1)
|
| 31 |
image = (image_blanc_noir * 255).astype(np.uint8)
|
| 32 |
return Image.fromarray(image)
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
# 2. Application d'un négatif à l'image
|
| 37 |
def apply_negative(image):
|
| 38 |
img_np = np.array(image)
|
| 39 |
negative = 255 - img_np
|
| 40 |
return Image.fromarray(negative)
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
# 3. Transformation en Rotation
|
| 45 |
def rotate_image(image, angle):
|
| 46 |
return image.rotate(angle, expand=True)
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
# 4. Application des filtres
|
| 51 |
def filtrage_image(image, filter_name):
|
| 52 |
-
# Récupérer le filtre en fonction du nom
|
| 53 |
filtre_mapping = {
|
| 54 |
'Floutage': PIL.ImageFilter.BLUR,
|
| 55 |
'Détails': PIL.ImageFilter.DETAIL,
|
| 56 |
'Netteté': PIL.ImageFilter.SHARPEN,
|
| 57 |
'Effet 3D': PIL.ImageFilter.EMBOSS,
|
| 58 |
-
'Contour': PIL.ImageFilter.FIND_EDGES,
|
| 59 |
-
'Floutage Moyen': PIL.ImageFilter.BoxBlur(5),
|
| 60 |
-
'Floutage Gaussien': PIL.ImageFilter.GaussianBlur(5)
|
| 61 |
}
|
| 62 |
|
| 63 |
if filter_name in filtre_mapping:
|
| 64 |
filtre = filtre_mapping[filter_name]
|
| 65 |
-
# Appliquer le filtre à l'image
|
| 66 |
return image.filter(filtre)
|
| 67 |
else:
|
| 68 |
raise ValueError(f"Le filtre '{filter_name}' n'existe pas dans les filtres définis.")
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
# 5. Binarisation de l'image
|
| 72 |
def binarize_image(image, threshold):
|
| 73 |
img_np = np.array(image.convert('L'))
|
| 74 |
_, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY)
|
| 75 |
return Image.fromarray(binary)
|
| 76 |
|
| 77 |
-
|
| 78 |
# 6. Redimensionnement de l'image
|
| 79 |
def resize_image(image, width, height):
|
| 80 |
return image.resize((width, height))
|
| 81 |
|
| 82 |
-
|
| 83 |
# 7. Détecter les contours avec canny:
|
| 84 |
def detect_contour(image):
|
| 85 |
-
# Transformer l'image en niveau de gris
|
| 86 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 87 |
image = cv2.GaussianBlur(image, (5, 5), 0)
|
| 88 |
edges = cv2.Canny(image, threshold1=50, threshold2=150)
|
| 89 |
return Image.fromarray(edges)
|
| 90 |
|
| 91 |
-
|
| 92 |
# 8. Détecter les contours avec Sobel:
|
| 93 |
def detect_contour_sobel(image):
|
| 94 |
-
sobel_x = np.array([[-1, 0, 1],
|
| 95 |
-
|
| 96 |
-
[-1, 0, 1]])
|
| 97 |
-
|
| 98 |
-
sobel_y = np.array([[-1,-2,-1],
|
| 99 |
-
[0, 0, 0],
|
| 100 |
-
[1, 2, 1]])
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
# Convertir en niveaux de gris
|
| 104 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 105 |
-
# Appliquer les filtres sobel
|
| 106 |
sobel_x_img = convolve(image, sobel_x)
|
| 107 |
sobel_y_img = convolve(image, sobel_y)
|
| 108 |
-
|
| 109 |
-
# Combiner les deux pour obtenir les contours
|
| 110 |
sobel_combined = np.hypot(sobel_x_img, sobel_y_img)
|
| 111 |
-
sobel_combined = (sobel_combined / sobel_combined.max()) * 255
|
| 112 |
-
|
| 113 |
return Image.fromarray(sobel_combined.astype(np.uint8))
|
| 114 |
|
| 115 |
-
|
| 116 |
# 9. Transformation morphologique : erosion
|
| 117 |
def morphologies_erosion(image):
|
| 118 |
-
# Convertir en niveaux de gris
|
| 119 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 120 |
-
erosion = morphology.binary_erosion(image
|
| 121 |
return Image.fromarray(erosion.astype(np.uint8))
|
| 122 |
|
| 123 |
-
|
| 124 |
# 10. Transformation morphologique : dilatation
|
| 125 |
def morphologies_dilatation(image):
|
| 126 |
-
# Convertir en niveaux de gris
|
| 127 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 128 |
-
dilation = morphology.binary_dilation(image=image,
|
| 129 |
return Image.fromarray(dilation.astype(np.uint8))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
#==========================================================================================
|
| 132 |
-
# Interface Gradio
|
| 133 |
-
def image_processing(image, operation, filter_name, threshold=128, width=100, height=100, angle=0):
|
| 134 |
if operation == "Négatif":
|
| 135 |
-
|
| 136 |
elif operation == 'Niveau de Gris':
|
| 137 |
-
|
| 138 |
elif operation == "Blanc Noir":
|
| 139 |
-
|
| 140 |
elif operation == "Binarisation":
|
| 141 |
-
|
| 142 |
elif operation == "Redimensionner":
|
| 143 |
-
|
| 144 |
elif operation == "Rotation":
|
| 145 |
-
|
| 146 |
elif operation == "Filtrage":
|
| 147 |
-
|
| 148 |
elif operation == "Contour Pro (Canny)":
|
| 149 |
-
|
| 150 |
elif operation == "Contour Pro (Sobel)":
|
| 151 |
-
|
| 152 |
elif operation == "Erosion":
|
| 153 |
-
|
| 154 |
elif operation == "Dilatation":
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
|
| 159 |
-
# Interface Gradio
|
| 160 |
with gr.Blocks() as demo:
|
| 161 |
gr.Markdown("## APPLICATION DE TRAITEMENT DES IMAGES")
|
| 162 |
|
|
@@ -174,19 +189,22 @@ with gr.Blocks() as demo:
|
|
| 174 |
'Contour': 'Contour',
|
| 175 |
'Floutage Moyen': 'Floutage Moyen',
|
| 176 |
'Floutage Gaussien': 'Floutage Gaussien',
|
| 177 |
-
|
| 178 |
}
|
| 179 |
options = gr.Dropdown(choices=list(dict_options.keys()), label="Choisissez votre filtre", visible=True)
|
| 180 |
threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False)
|
| 181 |
width = gr.Number(value=100, label="Largeur", visible=False)
|
| 182 |
height = gr.Number(value=100, label="Hauteur", visible=False)
|
| 183 |
angle = gr.Number(value=360, label="Angle de Rotation", visible=True)
|
|
|
|
| 184 |
|
| 185 |
image_output = gr.Image(label="Image Modifiée")
|
|
|
|
| 186 |
|
| 187 |
submit_button = gr.Button("Appliquer")
|
| 188 |
-
submit_button.click(image_processing,
|
|
|
|
|
|
|
| 189 |
|
| 190 |
-
|
| 191 |
# Lancer l'application Gradio
|
| 192 |
demo.launch()
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
from skimage.color import rgb2gray
|
| 7 |
import PIL.ImageFilter
|
| 8 |
from scipy.ndimage import convolve
|
| 9 |
from skimage import morphology
|
| 10 |
|
| 11 |
+
# ==========================================================================================
|
|
|
|
| 12 |
# 1. Charger l'image
|
| 13 |
def load_image(image):
|
| 14 |
return image
|
| 15 |
+
# ==========================================================================================
|
| 16 |
|
| 17 |
+
# ==========================================================================================
|
| 18 |
# Transformer l'image en niveau de gris
|
| 19 |
def gray(image):
|
| 20 |
image = np.array(image)
|
| 21 |
image_gris = rgb2gray(image)
|
| 22 |
return image_gris
|
| 23 |
+
# ==========================================================================================
|
| 24 |
|
| 25 |
+
# ==========================================================================================
|
| 26 |
# Transformer en blanc noir
|
| 27 |
def blanc_noir(image):
|
| 28 |
image = np.array(image)
|
|
|
|
| 30 |
image_blanc_noir = np.where(image_gris > 0.5, 0, 1)
|
| 31 |
image = (image_blanc_noir * 255).astype(np.uint8)
|
| 32 |
return Image.fromarray(image)
|
| 33 |
+
# ==========================================================================================
|
| 34 |
|
| 35 |
+
# ==========================================================================================
|
| 36 |
# 2. Application d'un négatif à l'image
|
| 37 |
def apply_negative(image):
|
| 38 |
img_np = np.array(image)
|
| 39 |
negative = 255 - img_np
|
| 40 |
return Image.fromarray(negative)
|
| 41 |
+
# ==========================================================================================
|
| 42 |
|
| 43 |
+
# ==========================================================================================
|
| 44 |
# 3. Transformation en Rotation
|
| 45 |
def rotate_image(image, angle):
|
| 46 |
return image.rotate(angle, expand=True)
|
| 47 |
+
# ==========================================================================================
|
| 48 |
|
| 49 |
+
# ==========================================================================================
|
| 50 |
# 4. Application des filtres
|
| 51 |
def filtrage_image(image, filter_name):
|
|
|
|
| 52 |
filtre_mapping = {
|
| 53 |
'Floutage': PIL.ImageFilter.BLUR,
|
| 54 |
'Détails': PIL.ImageFilter.DETAIL,
|
| 55 |
'Netteté': PIL.ImageFilter.SHARPEN,
|
| 56 |
'Effet 3D': PIL.ImageFilter.EMBOSS,
|
| 57 |
+
'Contour': PIL.ImageFilter.FIND_EDGES,
|
| 58 |
+
'Floutage Moyen': PIL.ImageFilter.BoxBlur(5),
|
| 59 |
+
'Floutage Gaussien': PIL.ImageFilter.GaussianBlur(5)
|
| 60 |
}
|
| 61 |
|
| 62 |
if filter_name in filtre_mapping:
|
| 63 |
filtre = filtre_mapping[filter_name]
|
|
|
|
| 64 |
return image.filter(filtre)
|
| 65 |
else:
|
| 66 |
raise ValueError(f"Le filtre '{filter_name}' n'existe pas dans les filtres définis.")
|
| 67 |
+
# ==========================================================================================
|
| 68 |
|
| 69 |
+
# ==========================================================================================
|
| 70 |
# 5. Binarisation de l'image
|
| 71 |
def binarize_image(image, threshold):
|
| 72 |
img_np = np.array(image.convert('L'))
|
| 73 |
_, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY)
|
| 74 |
return Image.fromarray(binary)
|
| 75 |
|
| 76 |
+
# ==========================================================================================
|
| 77 |
# 6. Redimensionnement de l'image
|
| 78 |
def resize_image(image, width, height):
|
| 79 |
return image.resize((width, height))
|
| 80 |
|
| 81 |
+
# ==========================================================================================
|
| 82 |
# 7. Détecter les contours avec canny:
|
| 83 |
def detect_contour(image):
|
|
|
|
| 84 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 85 |
image = cv2.GaussianBlur(image, (5, 5), 0)
|
| 86 |
edges = cv2.Canny(image, threshold1=50, threshold2=150)
|
| 87 |
return Image.fromarray(edges)
|
| 88 |
|
| 89 |
+
# ==========================================================================================
|
| 90 |
# 8. Détecter les contours avec Sobel:
|
| 91 |
def detect_contour_sobel(image):
|
| 92 |
+
sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
|
| 93 |
+
sobel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
|
|
|
| 95 |
sobel_x_img = convolve(image, sobel_x)
|
| 96 |
sobel_y_img = convolve(image, sobel_y)
|
|
|
|
|
|
|
| 97 |
sobel_combined = np.hypot(sobel_x_img, sobel_y_img)
|
| 98 |
+
sobel_combined = (sobel_combined / sobel_combined.max()) * 255
|
|
|
|
| 99 |
return Image.fromarray(sobel_combined.astype(np.uint8))
|
| 100 |
|
| 101 |
+
# ==========================================================================================
|
| 102 |
# 9. Transformation morphologique : erosion
|
| 103 |
def morphologies_erosion(image):
|
|
|
|
| 104 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 105 |
+
erosion = morphology.binary_erosion(image=image, footprint=morphology.disk(1))
|
| 106 |
return Image.fromarray(erosion.astype(np.uint8))
|
| 107 |
|
| 108 |
+
# ==========================================================================================
|
| 109 |
# 10. Transformation morphologique : dilatation
|
| 110 |
def morphologies_dilatation(image):
|
|
|
|
| 111 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 112 |
+
dilation = morphology.binary_dilation(image=image, footprint=morphology.disk(1))
|
| 113 |
return Image.fromarray(dilation.astype(np.uint8))
|
| 114 |
+
|
| 115 |
+
# ==========================================================================================
|
| 116 |
+
# 11. Afficher l'histogramme de l'image dans Gradio
|
| 117 |
+
def display_histogram(image):
|
| 118 |
+
img_np = np.array(image.convert('L'))
|
| 119 |
+
plt.figure()
|
| 120 |
+
plt.hist(img_np.ravel(), bins=256, range=[0, 256], color='black', alpha=0.7)
|
| 121 |
+
plt.title('Histogramme de l\'image')
|
| 122 |
+
plt.xlabel('Intensité des pixels')
|
| 123 |
+
plt.ylabel('Fréquence')
|
| 124 |
+
plt.grid(False)
|
| 125 |
+
|
| 126 |
+
# Sauvegarder l'histogramme dans un buffer
|
| 127 |
+
import io
|
| 128 |
+
buf = io.BytesIO()
|
| 129 |
+
plt.savefig(buf, format='png')
|
| 130 |
+
buf.seek(0)
|
| 131 |
+
plt.close()
|
| 132 |
+
|
| 133 |
+
# Charger l'image du buffer
|
| 134 |
+
hist_image = Image.open(buf)
|
| 135 |
+
return hist_image
|
| 136 |
+
|
| 137 |
+
# ==========================================================================================
|
| 138 |
+
# Interface Gradio mise à jour pour inclure l'affichage de l'histogramme
|
| 139 |
+
def image_processing(image, operation, filter_name, threshold=128, width=100, height=100, angle=0, display_hist=False):
|
| 140 |
+
processed_image = image
|
| 141 |
+
hist_image = None # Ajout d'une variable pour l'histogramme
|
| 142 |
|
|
|
|
|
|
|
|
|
|
| 143 |
if operation == "Négatif":
|
| 144 |
+
processed_image = apply_negative(image)
|
| 145 |
elif operation == 'Niveau de Gris':
|
| 146 |
+
processed_image = gray(image)
|
| 147 |
elif operation == "Blanc Noir":
|
| 148 |
+
processed_image = blanc_noir(image)
|
| 149 |
elif operation == "Binarisation":
|
| 150 |
+
processed_image = binarize_image(image, threshold)
|
| 151 |
elif operation == "Redimensionner":
|
| 152 |
+
processed_image = resize_image(image, width, height)
|
| 153 |
elif operation == "Rotation":
|
| 154 |
+
processed_image = rotate_image(image, angle)
|
| 155 |
elif operation == "Filtrage":
|
| 156 |
+
processed_image = filtrage_image(image, filter_name)
|
| 157 |
elif operation == "Contour Pro (Canny)":
|
| 158 |
+
processed_image = detect_contour(image)
|
| 159 |
elif operation == "Contour Pro (Sobel)":
|
| 160 |
+
processed_image = detect_contour_sobel(image)
|
| 161 |
elif operation == "Erosion":
|
| 162 |
+
processed_image = morphologies_erosion(image)
|
| 163 |
elif operation == "Dilatation":
|
| 164 |
+
processed_image = morphologies_dilatation(image)
|
| 165 |
+
|
| 166 |
+
# Afficher l'histogramme si l'option est cochée
|
| 167 |
+
if display_hist:
|
| 168 |
+
hist_image = display_histogram(processed_image)
|
| 169 |
+
|
| 170 |
+
# Retourner l'image modifiée et l'histogramme
|
| 171 |
+
return processed_image, hist_image
|
| 172 |
|
| 173 |
+
# ==========================================================================================
|
| 174 |
+
# Interface Gradio mise à jour
|
| 175 |
with gr.Blocks() as demo:
|
| 176 |
gr.Markdown("## APPLICATION DE TRAITEMENT DES IMAGES")
|
| 177 |
|
|
|
|
| 189 |
'Contour': 'Contour',
|
| 190 |
'Floutage Moyen': 'Floutage Moyen',
|
| 191 |
'Floutage Gaussien': 'Floutage Gaussien',
|
|
|
|
| 192 |
}
|
| 193 |
options = gr.Dropdown(choices=list(dict_options.keys()), label="Choisissez votre filtre", visible=True)
|
| 194 |
threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False)
|
| 195 |
width = gr.Number(value=100, label="Largeur", visible=False)
|
| 196 |
height = gr.Number(value=100, label="Hauteur", visible=False)
|
| 197 |
angle = gr.Number(value=360, label="Angle de Rotation", visible=True)
|
| 198 |
+
display_hist = gr.Checkbox(label="Afficher Histogramme", visible=True)
|
| 199 |
|
| 200 |
image_output = gr.Image(label="Image Modifiée")
|
| 201 |
+
hist_output = gr.Image(label="Histogramme", visible=True) # Ajout d'un espace pour l'histogramme
|
| 202 |
|
| 203 |
submit_button = gr.Button("Appliquer")
|
| 204 |
+
submit_button.click(image_processing,
|
| 205 |
+
inputs=[image_input, operation, options, threshold, width, height, angle, display_hist],
|
| 206 |
+
outputs=[image_output, hist_output])
|
| 207 |
|
| 208 |
+
# ==========================================================================================
|
| 209 |
# Lancer l'application Gradio
|
| 210 |
demo.launch()
|