AI-lim commited on
Commit
794769e
·
1 Parent(s): 2b52f5e

Ajout de l'application Gradio

Browse files
Files changed (2) hide show
  1. app.py +165 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import numpy as np
4
+ import cv2
5
+ import matplotlib.pyplot as plt
6
+ from skimage import exposure
7
+ from scipy import signal
8
+ from skimage.color import rgb2gray
9
+
10
+ # Fonctions de traitement d'image
11
+ def load_image(image):
12
+ return image
13
+
14
+ def apply_negative(image):
15
+ img_np = np.array(image)
16
+ negative = 255 - img_np
17
+ return Image.fromarray(negative)
18
+
19
+ def binarize_image(image, threshold):
20
+ img_np = np.array(image.convert('L'))
21
+ _, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY)
22
+ return Image.fromarray(binary)
23
+
24
+ def resize_image(image, width, height):
25
+ return image.resize((width, height))
26
+
27
+ def rotate_image(image, angle):
28
+ return image.rotate(angle)
29
+
30
+ def histogram(image):
31
+ img_np = np.array(image)
32
+ gray = rgb2gray(img_np)
33
+
34
+ hist, _ = exposure.histogram(gray)
35
+ plt.figure(figsize=(6, 4))
36
+ plt.title('Histogramme en nuances de gris')
37
+ plt.xlabel('Intensité des pixels')
38
+ plt.ylabel('Nombre de pixels')
39
+ plt.bar(np.arange(len(hist)), hist, width=0.5, color='gray')
40
+ plt.tight_layout()
41
+ plt.show()
42
+
43
+ def mean_filter(image):
44
+ img_np = np.array(image)
45
+ mean_filtered = cv2.blur(img_np, (5, 5)) # Filtre moyen 5x5
46
+ return Image.fromarray(mean_filtered)
47
+
48
+ def gaussian_filter(image):
49
+ img_np = np.array(image)
50
+ gaussian_filtered = cv2.GaussianBlur(img_np, (5, 5), 0) # Filtre gaussien 5x5
51
+ return Image.fromarray(gaussian_filtered)
52
+
53
+ def contour_extract(image):
54
+ img_np = np.array(image.convert('L'))
55
+ kernel = np.array([[0, 0, 0], [-1, 1, 0], [0, 0, 0]])
56
+ img_contour = signal.convolve2d(img_np, kernel, boundary='symm', mode='same')
57
+ return Image.fromarray(np.uint8(np.absolute(img_contour)))
58
+
59
+ def morph_erosion(image):
60
+ img_np = np.array(image.convert('L')) # Conversion en niveaux de gris
61
+ kernel = np.ones((5, 5), np.uint8) # Noyau 5x5 pour l'érosion
62
+ erosion = cv2.erode(img_np, kernel, iterations=1)
63
+ return Image.fromarray(erosion)
64
+
65
+ def morph_dilation(image):
66
+ img_np = np.array(image.convert('L')) # Conversion en niveaux de gris
67
+ kernel = np.ones((5, 5), np.uint8) # Noyau 5x5 pour la dilatation
68
+ dilation = cv2.dilate(img_np, kernel, iterations=1)
69
+ return Image.fromarray(dilation)
70
+
71
+ # Sauvegarder l'image sur l'ordinateur
72
+ def save_image(image):
73
+ img_np = np.array(image)
74
+ save_path = "image_modifiee.png"
75
+ cv2.imwrite(save_path, cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)) # Sauvegarder en format PNG
76
+ return save_path
77
+
78
+ # Interface Gradio
79
+ def image_processing(image, operation, threshold=128, width=100, height=100, angle=0):
80
+ if operation == "Négatif":
81
+ return apply_negative(image)
82
+ elif operation == "Binarisation":
83
+ return binarize_image(image, threshold)
84
+ elif operation == "Redimensionner":
85
+ return resize_image(image, width, height)
86
+ elif operation == "Rotation":
87
+ return rotate_image(image, angle)
88
+ elif operation == "Histogramme":
89
+ histogram(image) # Affichage seulement
90
+ return image
91
+ elif operation == "Filtre moyen":
92
+ return mean_filter(image)
93
+ elif operation == "Filtre gaussien":
94
+ return gaussian_filter(image)
95
+ elif operation == "Contours":
96
+ return contour_extract(image)
97
+ elif operation == "Erosion":
98
+ return morph_erosion(image)
99
+ elif operation == "Dilatation":
100
+ return morph_dilation(image)
101
+ return image
102
+
103
+ # Mise à jour dynamique de la visibilité des champs en fonction de l'opération
104
+ def update_visibility(operation):
105
+ return {
106
+ "threshold": operation == "Binarisation",
107
+ "width": operation == "Redimensionner",
108
+ "height": operation == "Redimensionner",
109
+ "angle": operation == "Rotation"
110
+ }
111
+
112
+ # Interface Gradio avec style
113
+ custom_css = """
114
+ #main-header {
115
+ color: #4CAF50; /* Vert clair */
116
+ font-family: 'Arial', sans-serif;
117
+ text-align: center;
118
+ }
119
+
120
+ #upload-area {
121
+ border: 2px solid #FF9800; /* Orange */
122
+ background-color: #FFF3E0;
123
+ }
124
+
125
+ #result-area {
126
+ border: 2px solid #4CAF50;
127
+ background-color: #E8F5E9;
128
+ }
129
+
130
+ #process-button {
131
+ background-color: #FF9800; /* Orange */
132
+ color: white;
133
+ font-size: 16px;
134
+ }
135
+ """
136
+
137
+ with gr.Blocks(css=custom_css) as demo:
138
+ gr.Markdown("## 🌈 PixelCrafter: Transformez vos images avec style 🎨")
139
+
140
+ with gr.Row():
141
+ image_input = gr.Image(type="pil", label="Charger Image")
142
+ operation = gr.Radio(
143
+ ["Négatif", "Binarisation", "Redimensionner", "Rotation", "Histogramme", "Filtre moyen", "Filtre gaussien", "Contours", "Erosion", "Dilatation"],
144
+ label="Opération", value="Négatif"
145
+ )
146
+
147
+ threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False)
148
+ width = gr.Number(value=100, label="Largeur", visible=False)
149
+ height = gr.Number(value=100, label="Hauteur", visible=False)
150
+ angle = gr.Number(value=0, label="Angle de Rotation", visible=False)
151
+ image_output = gr.Image(label="Image Modifiée")
152
+
153
+ # Mise à jour de la visibilité des champs
154
+ operation.change(update_visibility, inputs=operation, outputs=[threshold, width, height, angle])
155
+
156
+ # Bouton d'application
157
+ submit_button = gr.Button("Appliquer")
158
+ submit_button.click(image_processing, inputs=[image_input, operation, threshold, width, height, angle], outputs=image_output)
159
+
160
+ # Sauvegarde de l'image
161
+ save_button = gr.Button("Sauvegarder l'image")
162
+ save_button.click(save_image, inputs=image_output, outputs=gr.File(label="Télécharger l'image"))
163
+
164
+ # Lancer l'application Gradio
165
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ numpy
3
+ Pillow
4
+ opencv-python
5
+ matplotlib