Sadiyath commited on
Commit
84aedff
·
verified ·
1 Parent(s): dbc9cae
Files changed (1) hide show
  1. app.py +111 -93
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
- hist = cv2.calcHist([image], [0], None, [256], [0, 256])
29
- plt.plot(hist)
30
- plt.title('Histogramme des niveaux de gris')
31
- plt.xlabel('Intensité des pixels')
32
- plt.ylabel('Nombre de pixels')
33
- plt.show()
34
- return hist
35
-
36
- def filtre_gauss(image, kernel_width, kernel_heigth):
37
- return cv2.GaussianBlur(image, (kernel_width, kernel_heigth), 0)
38
-
39
- def erosion(image, taille):
40
- return image.filter(ImageFilter.MinFilter(taille_e))
41
-
42
- def dilatation(image, taille):
43
- return image.filter(ImageFilter.MaxFilter(taille_d))
44
-
45
- def extract_edges(image):
46
- image_sobelx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize = 5)
47
- image_sobely = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize = 5)
48
- return image_sobelx, image_sobely
49
-
50
- # Interface Gradio
51
- def image_processing(image, operation, threshold=128, width=100, height=100, angle=0):
52
- if operation == "Négatif":
53
- return apply_negative(image)
54
- elif operation == "Binarisation":
55
- return binarize_image(image, threshold)
56
- elif operation == "Redimensionner":
57
- return resize_image(image, width, height)
58
- elif operation == "Rotation":
59
- return rotate_image(image, angle)
60
- elif operation == "Histogramme des niveaux de gris":
61
- return histo_gray(image)
62
- elif operation == "Filtre gaussien":
63
- return filtre_gauss(image, kernel_width, kernel_heigth)
64
- elif operation == "Erosion":
65
- return erosion(image, taille_e)
66
- elif operation == "Dilatation":
67
- return dilatation(image, taille_d)
68
- elif operation == "Extraction de contours":
69
- return extract_edges(image)
70
-
71
- # Interface Gradio
72
- with gr.Blocks() as demo:
73
- gr.Markdown("## Projet de Traitement d'Image")
74
-
75
- with gr.Row():
76
- image_input = gr.Image(type="pil", label="Charger Image")
77
- operation = gr.Radio(["Négatif", "Binarisation", "Redimension", "Rotation", "Histogramme des niveaux de gris", "Filtre gaussien", "Extraction de contours", "Erosion", "Dilatation"], label="Opération")
78
- threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False)
79
- width = gr.Number(value=100, label="Largeur", visible=False)
80
- height = gr.Number(value=100, label="Hauteur", visible=False)
81
- angle = gr.Number(value=0, label="Angle de Rotation", visible=False)
82
- kernel_width = gr.Number(value=5, label="Largeur du kernel du filtre gaussien", visible=False)
83
- kernel_heigth = gr.Number(value=5, label="Hauteur du kernel du filtre gaussien", visible=False)
84
- taille_e = gr.Number(value=3, label="Taille du filtre pour l'érosion", visible=False)
85
- taille_d = gr.Number(value=3, label="Taille du filtre pour la dilatation", visible=False)
86
-
87
- image_output = gr.Image(label="Image Modifiée")
88
-
89
- submit_button = gr.Button("Appliquer")
90
- submit_button.click(image_processing, inputs=[image_input, operation, threshold, width, height, angle], outputs=image_output)
91
-
92
- # Lancer l'application Gradio
93
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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