Spaces:
Sleeping
Sleeping
Update
Browse files
app.py
CHANGED
|
@@ -1,93 +1,111 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from PIL import Image, ImageFilter
|
| 3 |
-
import numpy as np
|
| 4 |
-
import cv2
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
-
|
| 7 |
-
# Fonctions de traitement d'image
|
| 8 |
-
def load_image(image):
|
| 9 |
-
return image
|
| 10 |
-
|
| 11 |
-
def apply_negative(image):
|
| 12 |
-
img_np = np.array(image)
|
| 13 |
-
negative = 255 - img_np
|
| 14 |
-
return Image.fromarray(negative)
|
| 15 |
-
|
| 16 |
-
def binarize_image(image, threshold):
|
| 17 |
-
img_np = np.array(image.convert('L'))
|
| 18 |
-
_, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY)
|
| 19 |
-
return Image.fromarray(binary)
|
| 20 |
-
|
| 21 |
-
def resize_image(image, width, height):
|
| 22 |
-
return image.resize((width, height))
|
| 23 |
-
|
| 24 |
-
def rotate_image(image, angle):
|
| 25 |
-
return image.rotate(angle)
|
| 26 |
-
|
| 27 |
-
def histo_gray(image):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
plt.
|
| 31 |
-
plt.
|
| 32 |
-
plt.
|
| 33 |
-
plt.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return
|
| 41 |
-
|
| 42 |
-
def
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageFilter
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
# Fonctions de traitement d'image
|
| 8 |
+
def load_image(image):
|
| 9 |
+
return image
|
| 10 |
+
|
| 11 |
+
def apply_negative(image):
|
| 12 |
+
img_np = np.array(image)
|
| 13 |
+
negative = 255 - img_np
|
| 14 |
+
return Image.fromarray(negative)
|
| 15 |
+
|
| 16 |
+
def binarize_image(image, threshold):
|
| 17 |
+
img_np = np.array(image.convert('L'))
|
| 18 |
+
_, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY)
|
| 19 |
+
return Image.fromarray(binary)
|
| 20 |
+
|
| 21 |
+
def resize_image(image, width, height):
|
| 22 |
+
return image.resize((width, height))
|
| 23 |
+
|
| 24 |
+
def rotate_image(image, angle):
|
| 25 |
+
return image.rotate(angle)
|
| 26 |
+
|
| 27 |
+
def histo_gray(image):
|
| 28 |
+
img_np = np.array(image.convert('L'))
|
| 29 |
+
hist = cv2.calcHist([img_np], [0], None, [256], [0, 256])
|
| 30 |
+
plt.plot(hist)
|
| 31 |
+
plt.title('Histogramme des niveaux de gris')
|
| 32 |
+
plt.xlabel('Intensité des pixels')
|
| 33 |
+
plt.ylabel('Nombre de pixels')
|
| 34 |
+
plt.show()
|
| 35 |
+
return image
|
| 36 |
+
|
| 37 |
+
def filtre_gauss(image, kernel_width, kernel_height):
|
| 38 |
+
img_np = np.array(image)
|
| 39 |
+
blurred = cv2.GaussianBlur(img_np, (kernel_width, kernel_height), 0)
|
| 40 |
+
return Image.fromarray(blurred)
|
| 41 |
+
|
| 42 |
+
def erosion(image, taille):
|
| 43 |
+
img_np = np.array(image)
|
| 44 |
+
kernel = np.ones((taille, taille), np.uint8)
|
| 45 |
+
eroded = cv2.erode(img_np, kernel, iterations=1)
|
| 46 |
+
return Image.fromarray(eroded)
|
| 47 |
+
|
| 48 |
+
def dilatation(image, taille):
|
| 49 |
+
img_np = np.array(image)
|
| 50 |
+
kernel = np.ones((taille, taille), np.uint8)
|
| 51 |
+
dilated = cv2.dilate(img_np, kernel, iterations=1)
|
| 52 |
+
return Image.fromarray(dilated)
|
| 53 |
+
|
| 54 |
+
def extract_edges(image):
|
| 55 |
+
img_np = np.array(image.convert('L'))
|
| 56 |
+
edges = cv2.Canny(img_np, 100, 200)
|
| 57 |
+
return Image.fromarray(edges)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def image_processing(image, operation, threshold=128, width=100, height=100, angle=0, kernel_width=5, kernel_height=5, taille_e=3, taille_d=3):
|
| 61 |
+
if operation == "Négatif":
|
| 62 |
+
return apply_negative(image)
|
| 63 |
+
elif operation == "Binarisation":
|
| 64 |
+
return binarize_image(image, threshold)
|
| 65 |
+
elif operation == "Redimensionner":
|
| 66 |
+
return resize_image(image, width, height)
|
| 67 |
+
elif operation == "Rotation":
|
| 68 |
+
return rotate_image(image, angle)
|
| 69 |
+
elif operation == "Histogramme des niveaux de gris":
|
| 70 |
+
return histo_gray(image)
|
| 71 |
+
elif operation == "Filtre gaussien":
|
| 72 |
+
return filtre_gauss(image, kernel_width, kernel_height)
|
| 73 |
+
elif operation == "Erosion":
|
| 74 |
+
return erosion(image, taille_e)
|
| 75 |
+
elif operation == "Dilatation":
|
| 76 |
+
return dilatation(image, taille_d)
|
| 77 |
+
elif operation == "Extraction de contours":
|
| 78 |
+
return extract_edges(image)
|
| 79 |
+
|
| 80 |
+
# Interface Gradio
|
| 81 |
+
with gr.Blocks() as demo:
|
| 82 |
+
gr.Markdown("## Projet de Traitement d'Image")
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
image_input = gr.Image(type="pil", label="Charger Image")
|
| 86 |
+
operation = gr.Radio(["Négatif", "Binarisation", "Redimensionner", "Rotation", "Histogramme des niveaux de gris", "Filtre gaussien", "Extraction de contours", "Erosion", "Dilatation"], label="Opération")
|
| 87 |
+
|
| 88 |
+
threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False)
|
| 89 |
+
width = gr.Number(value=100, label="Largeur", visible=False)
|
| 90 |
+
height = gr.Number(value=100, label="Hauteur", visible=False)
|
| 91 |
+
angle = gr.Number(value=0, label="Angle de Rotation", visible=False)
|
| 92 |
+
kernel_width = gr.Number(value=5, label="Largeur du kernel du filtre gaussien", visible=False)
|
| 93 |
+
kernel_height = gr.Number(value=5, label="Hauteur du kernel du filtre gaussien", visible=False)
|
| 94 |
+
taille_e = gr.Number(value=3, label="Taille du filtre pour l'érosion", visible=False)
|
| 95 |
+
taille_d = gr.Number(value=3, label="Taille du filtre pour la dilatation", visible=False)
|
| 96 |
+
|
| 97 |
+
image_output = gr.Image(label="Image Modifiée")
|
| 98 |
+
|
| 99 |
+
def update_visibility(operation):
|
| 100 |
+
if operation == "Binarisation":
|
| 101 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 102 |
+
elif operation == "Redimensionner":
|
| 103 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 104 |
+
elif operation == "Rotation":
|
| 105 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
| 106 |
+
elif operation == "Filtre gaussien":
|
| 107 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
| 108 |
+
elif operation == "Erosion":
|
| 109 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False, visible=True)
|
| 110 |
+
elif operation == "Dilatation":
|
| 111 |
+
return gr.update(visible=False), gr.update(visible(False), False, stored
|