StephaneBah commited on
Commit
b6ec593
1 Parent(s): 08aeadf
Files changed (1) hide show
  1. app.py +81 -44
app.py CHANGED
@@ -4,6 +4,7 @@ from PIL import Image
4
  import numpy as np
5
  import cv2
6
  import matplotlib.pyplot as plt
 
7
 
8
  # Fonctions de traitement d'image
9
  def load_image(image):
@@ -22,98 +23,134 @@ def binarize_image(image, threshold):
22
  _, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY)
23
  return Image.fromarray(binary)
24
 
25
- def resize_image(image, width: int, height: int):
 
 
26
  return image.resize((width, height))
27
 
28
  def rotate_image(image, angle):
29
  return image.rotate(angle)
30
 
31
  def show_histogram(image):
32
- grayscale = image.convert("L")
33
- plt.hist(grayscale, bins=120)
34
- #hist_data = grayscale.histogram()
35
- plt.figure()
36
- plt.plot(hist_data)
37
- plt.title("Histogramme des Niveaux de Gris")
38
- plt.show()
39
-
40
- def gaussian_filter(image, shape=(3,3)):
 
 
 
 
 
 
 
 
 
 
41
  image = np.array(image)
42
  filtered = cv2.GaussianBlur(image, shape, 0)
43
  return Image.fromarray(filtered)
44
 
45
- def mean_filter(image, shape=(3,3)):
46
  image = np.array(image)
47
  filtered = cv2.blur(image, shape)
48
  return Image.fromarray(filtered)
49
 
50
  def sobel_edges(image, k=5):
 
51
  image = np.array(image.convert('L'))
52
  sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=k)
53
  sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=k)
54
  sobel_combined = cv2.magnitude(sobel_x, sobel_y)
55
  return Image.fromarray(np.uint8(sobel_combined))
56
 
57
- def erosion(image, noyau=(5,5), iterations=3):
 
58
  image = np.array(image.convert("L"))
59
- kernel = np.ones(noyau, np.uint8)
60
  eroded_image = cv2.erode(image, kernel, iterations=iterations)
61
  return Image.fromarray(eroded_image)
62
 
63
- def dilatation(image, noyau=(5,5), iterations=3):
 
64
  image = np.array(image.convert("L"))
65
- kernel = np.ones(noyau, np.uint8)
66
  dilated_image = cv2.dilate(image, kernel, iterations=iterations)
67
  return Image.fromarray(dilated_image)
68
 
69
 
70
- # Ajoutez d'autres fonctions pour l'histogramme, le filtrage, Sobel, etc.
71
-
72
  # Interface Gradio
73
- def image_processing(image, operation, threshold=128, width=100, height=100, angle=30, shape=(3,3), noyau=(5,5), k=5, iterations=3):
 
74
  if operation == "Négatif":
75
- return apply_negative(image)
76
  elif operation == "Image en Gris":
77
- return grayscale(image)
78
  elif operation == "Binarisation":
79
- return binarize_image(image, threshold)
80
  elif operation == "Redimensionner":
81
- return resize_image(image, width, height)
82
  elif operation == "Rotation":
83
- return rotate_image(image, angle)
84
- elif operation == 'Histogramme de Gris':
85
- return show_histogram(image)
86
  elif operation == 'Filtre Gaussien':
87
- return gaussian_filter(image, shape)
88
  elif operation == 'Filtre Moyen':
89
- return mean_filter(image, shape)
90
  elif operation == 'Sobel Edges Extraction':
91
- return sobel_edges(image, k)
92
  elif operation == 'Erosion':
93
- return erosion(image, noyau, iterations)
94
  elif operation == 'Dilatation':
95
- return dilatation(image)
96
- # Ajouter d'autres conditions pour les autres opérations
97
- return image
98
 
99
  # Interface Gradio
100
  with gr.Blocks() as demo:
101
- gr.Markdown("## Projet de Traitement d'Image")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  with gr.Row():
104
- operation = gr.Radio(["Négatif", "Image en Gris", "Binarisation", "Redimensionner", "Rotation", 'Histogramme de Gris',
105
- 'Filtre Gaussien', 'Filtre Moyen', 'Sobel Edges Extraction', 'Erosion', 'Dilatation'], label="Opération")
106
- threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=True)
107
- width = gr.Number(value=100, label="Largeur", visible=True)
108
- height = gr.Number(value=100, label="Hauteur", visible=True)
109
- angle = gr.Slider(0, 360, 30, label="Angle de Rotation", visible=True)
110
- k = gr.Number(value=5, label="k de Sobel", visible=True)
111
- iterations = gr.Number(value=3, label="Nombre d'iteration pour les transformations morphologiques", visible=True)
112
  with gr.Row():
113
- image_input = gr.Image(type="pil", label="Charger Image")
114
- image_output = gr.Image(type="pil", label="Image Modifiée")
 
 
 
 
 
 
115
  submit_button = gr.Button("Appliquer")
116
- submit_button.click(image_processing, inputs=[image_input, operation, threshold, width, height, angle], outputs=image_output)
117
 
118
  # Lancer l'application Gradio
119
  demo.launch()
 
4
  import numpy as np
5
  import cv2
6
  import matplotlib.pyplot as plt
7
+ import io
8
 
9
  # Fonctions de traitement d'image
10
  def load_image(image):
 
23
  _, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY)
24
  return Image.fromarray(binary)
25
 
26
+ def resize_image(image, width, height):
27
+ width = int(width)
28
+ height = int(height)
29
  return image.resize((width, height))
30
 
31
  def rotate_image(image, angle):
32
  return image.rotate(angle)
33
 
34
  def show_histogram(image):
35
+ image_gray = image.convert("L")
36
+ # Obtenir les données de l'image en niveaux de gris
37
+ image_array = np.array(image_gray)
38
+ # Calculer l'histogramme
39
+ hist, bins = np.histogram(image_array.flatten(), bins=256, range=[0,256])
40
+ # Créer une figure pour l'affichage de l'histogramme
41
+ fig, ax = plt.subplots()
42
+ ax.plot(hist, color='blue')
43
+ ax.set_xlim([0, 256])
44
+ ax.set_title('Histogram of Image')
45
+ # Enregistrer l'histogramme dans un buffer
46
+ buf = io.BytesIO()
47
+ plt.savefig(buf, format='png')
48
+ buf.seek(0)
49
+ # Ouvrir l'image du buffer en utilisant PIL
50
+ hist_image = Image.open(buf)
51
+ return hist_image
52
+
53
+ def gaussian_filter(image, shape=(3, 3)):
54
  image = np.array(image)
55
  filtered = cv2.GaussianBlur(image, shape, 0)
56
  return Image.fromarray(filtered)
57
 
58
+ def mean_filter(image, shape=(3, 3)):
59
  image = np.array(image)
60
  filtered = cv2.blur(image, shape)
61
  return Image.fromarray(filtered)
62
 
63
  def sobel_edges(image, k=5):
64
+ k = int(k)
65
  image = np.array(image.convert('L'))
66
  sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=k)
67
  sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=k)
68
  sobel_combined = cv2.magnitude(sobel_x, sobel_y)
69
  return Image.fromarray(np.uint8(sobel_combined))
70
 
71
+ def erosion(image, iterations=3, shape=(5, 5)):
72
+ iterations = int(iterations)
73
  image = np.array(image.convert("L"))
74
+ kernel = np.ones(shape, np.uint8)
75
  eroded_image = cv2.erode(image, kernel, iterations=iterations)
76
  return Image.fromarray(eroded_image)
77
 
78
+ def dilatation(image, iterations=3, shape=(5, 5)):
79
+ iterations = int(iterations)
80
  image = np.array(image.convert("L"))
81
+ kernel = np.ones(shape, np.uint8)
82
  dilated_image = cv2.dilate(image, kernel, iterations=iterations)
83
  return Image.fromarray(dilated_image)
84
 
85
 
 
 
86
  # Interface Gradio
87
+ def image_processing(image, operation, modified_image, threshold=128, width=100, height=100, angle=30, k=5, iterations=3):
88
+ current_image = modified_image if modified_image is not None else image
89
  if operation == "Négatif":
90
+ current_image = apply_negative(image)
91
  elif operation == "Image en Gris":
92
+ current_image = grayscale(image)
93
  elif operation == "Binarisation":
94
+ current_image = binarize_image(image, threshold)
95
  elif operation == "Redimensionner":
96
+ current_image = resize_image(image, width, height)
97
  elif operation == "Rotation":
98
+ current_image = rotate_image(image, angle)
 
 
99
  elif operation == 'Filtre Gaussien':
100
+ current_image = gaussian_filter(image)
101
  elif operation == 'Filtre Moyen':
102
+ current_image = mean_filter(image)
103
  elif operation == 'Sobel Edges Extraction':
104
+ current_image = sobel_edges(image, k)
105
  elif operation == 'Erosion':
106
+ current_image = erosion(image, iterations)
107
  elif operation == 'Dilatation':
108
+ current_image = dilatation(image, iterations)
109
+
110
+ return current_image, show_histogram(current_image)
111
 
112
  # Interface Gradio
113
  with gr.Blocks() as demo:
114
+ gr.Markdown("## Traitement d'Images")
115
+
116
+ with gr.Row():
117
+ operation = gr.Radio(["Négatif", "Image en Gris", "Binarisation", "Redimensionner", "Rotation", 'Filtre Gaussien',
118
+ 'Filtre Moyen', 'Sobel Edges Extraction', 'Erosion', 'Dilatation'], label="Opération", value="Négatif")
119
+ with gr.Row():
120
+ threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False)
121
+ width = gr.Number(value=100, label="Largeur", visible=False)
122
+ height = gr.Number(value=100, label="Hauteur", visible=False)
123
+ angle = gr.Slider(0, 360, 30, label="Angle de Rotation", visible=False)
124
+ k = gr.Number(value=5, label="k de Sobel", visible=False)
125
+ iterations = gr.Number(value=3, label="Nombre d'iteration pour les transformations morphologiques", visible=False)
126
+
127
+ def update_ui(operation):
128
+ # Mise à jour dynamique de la visibilité des champs
129
+ return {
130
+ threshold: gr.update(visible=operation == "Binarisation"),
131
+ width: gr.update(visible=operation == "Redimensionner"),
132
+ height: gr.update(visible=operation == "Redimensionner"),
133
+ angle: gr.update(visible=operation == "Rotation"),
134
+ k: gr.update(visible=operation == "Sobel Edges Extraction"),
135
+ iterations: gr.update(visible=operation in ["Erosion", "Dilatation"])
136
+ }
137
+
138
+ operation.change(update_ui, operation, [threshold, width, height, angle, k, iterations])
139
 
140
  with gr.Row():
141
+ image_input = gr.Image(type="pil", label="Charger Image", scale=2)
142
+ original_hist = gr.Image(label="Histogramme de l'Image Originale", scale=1)
 
 
 
 
 
 
143
  with gr.Row():
144
+ image_output = gr.Image(type="pil", label="Image Modifiée", interactive=False)
145
+ modified_hist = gr.Image(label="Histogramme de l'Image Modifiée", scale=1)
146
+
147
+ # Afficher l'histogramme de l'image d'entrée
148
+ def s_hist(image):
149
+ return show_histogram(image)
150
+ image_input.change(s_hist, inputs=image_input, outputs=original_hist)
151
+
152
  submit_button = gr.Button("Appliquer")
153
+ submit_button.click(image_processing, inputs=[image_input, operation, image_output, threshold, width, height, angle, k, iterations], outputs=[image_output, modified_hist])
154
 
155
  # Lancer l'application Gradio
156
  demo.launch()