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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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 |
+
# Fonctions de traitement d'image
|
| 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)
|
| 29 |
+
image_gris = rgb2gray(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, # Détecter les contours
|
| 59 |
+
'Floutage Moyen': PIL.ImageFilter.BoxBlur(5), # Spécifiez le rayon pour BoxBlur
|
| 60 |
+
'Floutage Gaussien': PIL.ImageFilter.GaussianBlur(5) # Spécifiez le rayon pour GaussianBlur
|
| 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 |
+
[-2, 0, 2],
|
| 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 # Normaliser
|
| 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 = image, footprint=morphology.disk(1))
|
| 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, footprint=morphology.disk(1))
|
| 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 |
+
return apply_negative(image)
|
| 136 |
+
elif operation == 'Niveau de Gris':
|
| 137 |
+
return gray(image)
|
| 138 |
+
elif operation == "Blanc Noir":
|
| 139 |
+
return blanc_noir(image)
|
| 140 |
+
elif operation == "Binarisation":
|
| 141 |
+
return binarize_image(image, threshold)
|
| 142 |
+
elif operation == "Redimensionner":
|
| 143 |
+
return resize_image(image, width, height)
|
| 144 |
+
elif operation == "Rotation":
|
| 145 |
+
return rotate_image(image, angle)
|
| 146 |
+
elif operation == "Filtrage":
|
| 147 |
+
return filtrage_image(image, filter_name)
|
| 148 |
+
elif operation == "Contour Pro (Canny)":
|
| 149 |
+
return detect_contour(image)
|
| 150 |
+
elif operation == "Contour Pro (Sobel)":
|
| 151 |
+
return detect_contour_sobel(image)
|
| 152 |
+
elif operation == "Erosion":
|
| 153 |
+
return morphologies_erosion(image)
|
| 154 |
+
elif operation == "Dilatation":
|
| 155 |
+
return morphologies_dilatation(image)
|
| 156 |
+
return image
|
| 157 |
+
|
| 158 |
+
#==========================================================================================
|
| 159 |
+
# Interface Gradio
|
| 160 |
+
with gr.Blocks() as demo:
|
| 161 |
+
gr.Markdown("## APPLICATION DE TRAITEMENT DES IMAGES")
|
| 162 |
+
|
| 163 |
+
with gr.Row():
|
| 164 |
+
image_input = gr.Image(type="pil", label="Charger Image")
|
| 165 |
+
operation = gr.Radio(["Négatif", "Binarisation", "Redimensionner",
|
| 166 |
+
"Rotation", "Niveau de Gris", "Blanc Noir",
|
| 167 |
+
"Filtrage", "Contour Pro (Canny)", "Contour Pro (Sobel)",
|
| 168 |
+
"Erosion", "Dilatation"], label="Opération")
|
| 169 |
+
dict_options = {
|
| 170 |
+
'Floutage': 'Floutage',
|
| 171 |
+
'Détails': 'Détails',
|
| 172 |
+
'Netteté': 'Netteté',
|
| 173 |
+
'Effet 3D': 'Effet 3D',
|
| 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, inputs=[image_input, operation, options, threshold, width, height, angle], outputs=image_output)
|
| 189 |
+
|
| 190 |
+
#==========================================================================================
|
| 191 |
+
# Lancer l'application Gradio
|
| 192 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1
|
| 2 |
+
annotated-types==0.7.0
|
| 3 |
+
click==8.1.7
|
| 4 |
+
contourpy==1.3.0
|
| 5 |
+
cycler==0.12.1
|
| 6 |
+
fastapi==0.115.0
|
| 7 |
+
ffmpy==0.4.0
|
| 8 |
+
fonttools==4.54.1
|
| 9 |
+
fsspec==2024.9.0
|
| 10 |
+
gradio==4.44.1
|
| 11 |
+
gradio_client==1.3.0
|
| 12 |
+
huggingface-hub==0.25.2
|
| 13 |
+
imageio==2.35.1
|
| 14 |
+
importlib_resources==6.4.5
|
| 15 |
+
kiwisolver==1.4.7
|
| 16 |
+
lazy_loader==0.4
|
| 17 |
+
matplotlib==3.9.2
|
| 18 |
+
networkx==3.3
|
| 19 |
+
numpy==2.1.2
|
| 20 |
+
opencv-python==4.10.0.84
|
| 21 |
+
orjson==3.10.7
|
| 22 |
+
pandas==2.2.3
|
| 23 |
+
pillow==10.4.0
|
| 24 |
+
pydantic==2.9.2
|
| 25 |
+
pydantic_core==2.23.4
|
| 26 |
+
pydub==0.25.1
|
| 27 |
+
pyparsing==3.1.4
|
| 28 |
+
python-multipart==0.0.12
|
| 29 |
+
pytz==2024.2
|
| 30 |
+
ruff==0.6.9
|
| 31 |
+
scikit-image==0.24.0
|
| 32 |
+
scipy==1.14.1
|
| 33 |
+
semantic-version==2.10.0
|
| 34 |
+
setuptools==75.1.0
|
| 35 |
+
shellingham==1.5.4
|
| 36 |
+
starlette==0.38.6
|
| 37 |
+
tifffile==2024.9.20
|
| 38 |
+
tomlkit==0.12.0
|
| 39 |
+
tqdm==4.66.5
|
| 40 |
+
typer==0.12.5
|
| 41 |
+
tzdata==2024.2
|
| 42 |
+
uvicorn==0.31.0
|
| 43 |
+
websockets==12.0
|