Spaces:
Runtime error
Runtime error
StephaneBah
commited on
Commit
•
08aeadf
1
Parent(s):
962b4b7
v1
Browse files- app.py +119 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
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):
|
10 |
+
return image
|
11 |
+
|
12 |
+
def apply_negative(image):
|
13 |
+
img_np = np.array(image)
|
14 |
+
negative = 255 - img_np
|
15 |
+
return Image.fromarray(negative)
|
16 |
+
|
17 |
+
def grayscale(image):
|
18 |
+
return image.convert('L')
|
19 |
+
|
20 |
+
def binarize_image(image, threshold):
|
21 |
+
img_np = np.array(image.convert('L'))
|
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()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.40.0
|
2 |
+
Pillow==9.4.0
|
3 |
+
opencv-python-headless==4.8.0.74
|
4 |
+
matplotlib==3.7.1
|
5 |
+
numpy==1.24.3
|