Spaces:
Sleeping
Sleeping
Update
Browse files
app.py
CHANGED
|
@@ -25,14 +25,14 @@ 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
|
| 36 |
|
| 37 |
def filtre_gauss(image, kernel_width, kernel_height):
|
| 38 |
img_np = np.array(image)
|
|
@@ -40,23 +40,23 @@ def filtre_gauss(image, kernel_width, kernel_height):
|
|
| 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)
|
|
@@ -84,7 +84,7 @@ with gr.Blocks() as demo:
|
|
| 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)
|
|
@@ -96,16 +96,27 @@ with gr.Blocks() as demo:
|
|
| 96 |
|
| 97 |
image_output = gr.Image(label="Image Modifiée")
|
| 98 |
|
| 99 |
-
def
|
|
|
|
| 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,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 hist
|
| 36 |
|
| 37 |
def filtre_gauss(image, kernel_width, kernel_height):
|
| 38 |
img_np = np.array(image)
|
|
|
|
| 40 |
return Image.fromarray(blurred)
|
| 41 |
|
| 42 |
def erosion(image, taille):
|
| 43 |
+
img_np = np.array(image.convert('L'))
|
| 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.convert('L'))
|
| 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 |
+
# Interface Gradio
|
| 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)
|
|
|
|
| 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)
|
|
|
|
| 96 |
|
| 97 |
image_output = gr.Image(label="Image Modifiée")
|
| 98 |
|
| 99 |
+
def update_inputs(operation):
|
| 100 |
+
# Fonction pour afficher/masquer les contrôles en fonction de l'opération
|
| 101 |
if operation == "Binarisation":
|
| 102 |
+
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), gr.update(visible=False)
|
| 103 |
elif operation == "Redimensionner":
|
| 104 |
+
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), gr.update(visible=False)
|
| 105 |
elif operation == "Rotation":
|
| 106 |
+
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), gr.update(visible=False)
|
| 107 |
elif operation == "Filtre gaussien":
|
| 108 |
+
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), gr.update(visible=False)
|
| 109 |
elif operation == "Erosion":
|
| 110 |
+
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), gr.update(visible=True)
|
| 111 |
elif operation == "Dilatation":
|
| 112 |
+
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), gr.update(visible=False, visible=True)
|
| 113 |
+
else:
|
| 114 |
+
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), gr.update(visible=False)
|
| 115 |
+
|
| 116 |
+
operation.change(update_inputs, inputs=operation, outputs=[threshold, width, height, angle, kernel_width, kernel_height, taille_e, taille_d])
|
| 117 |
+
|
| 118 |
+
submit_button = gr.Button("Appliquer")
|
| 119 |
+
submit_button.click(image_processing, inputs=[image_input, operation, threshold, width, height, angle, kernel_width, kernel_height, taille_e, taille_d], outputs=image_output)
|
| 120 |
+
|
| 121 |
+
# Lancer l'application Gradio
|
| 122 |
+
demo.launch()
|