Spaces:
Sleeping
Sleeping
File size: 6,009 Bytes
84aedff f2ad9ee 84aedff f2ad9ee 84aedff f2ad9ee 84aedff f2ad9ee 84aedff f2ad9ee 84aedff f2ad9ee 84aedff 7575ed2 84aedff f2ad9ee 7575ed2 e1fcff6 84aedff f2ad9ee 84aedff 7575ed2 84aedff 7575ed2 84aedff 7575ed2 84aedff 7575ed2 84aedff 7575ed2 84aedff 7575ed2 f2ad9ee 7575ed2 f2ad9ee 7575ed2 212ef57 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | import gradio as gr
from PIL import Image, ImageFilter
import numpy as np
import cv2
import matplotlib.pyplot as plt
def load_image(image):
return image
def apply_negative(image):
img_np = np.array(image)
negative = 255 - img_np
return Image.fromarray(negative)
def binarize_image(image, threshold):
img_np = np.array(image.convert('L'))
_, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY)
return Image.fromarray(binary)
def resize_image(image, width, height):
return image.resize((width, height))
def rotate_image(image, angle):
return image.rotate(angle)
def histo_gray(image):
img_np = np.array(image.convert('L'))
hist = cv2.calcHist([img_np], [0], None, [256], [0, 256])
plt.plot(hist)
plt.title('Histogramme des niveaux de gris')
plt.xlabel('Intensité des pixels')
plt.ylabel('Nombre de pixels')
plt.show()
return hist
def filtre_gauss(image, kernel_width, kernel_height):
img_np = np.array(image)
blurred = cv2.GaussianBlur(img_np, (kernel_width, kernel_height), 0)
return Image.fromarray(blurred)
def erosion(image, taille):
img_np = np.array(image.convert('L'))
kernel = np.ones((taille, taille), np.uint8)
eroded = cv2.erode(img_np, kernel, iterations=1)
return Image.fromarray(eroded)
def dilatation(image, taille):
img_np = np.array(image.convert('L'))
kernel = np.ones((taille, taille), np.uint8)
dilated = cv2.dilate(img_np, kernel, iterations=1)
return Image.fromarray(dilated)
def extract_edges(image):
img_np = np.array(image.convert('L'))
edges = cv2.Canny(img_np, 100, 200)
return Image.fromarray(edges)
# Interface Gradio
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):
if operation == "Négatif":
return apply_negative(image)
elif operation == "Binarisation":
return binarize_image(image, threshold)
elif operation == "Redimensionner":
return resize_image(image, width, height)
elif operation == "Rotation":
return rotate_image(image, angle)
elif operation == "Histogramme des niveaux de gris":
return histo_gray(image)
elif operation == "Filtre gaussien":
return filtre_gauss(image, kernel_width, kernel_height)
elif operation == "Erosion":
return erosion(image, taille_e)
elif operation == "Dilatation":
return dilatation(image, taille_d)
elif operation == "Extraction de contours":
return extract_edges(image)
with gr.Blocks() as demo:
gr.Markdown("## Projet de Traitement d'Image")
with gr.Row():
image_input = gr.Image(type="pil", label="Charger Image")
operation = gr.Radio(["Négatif", "Binarisation", "Redimensionner", "Rotation", "Histogramme des niveaux de gris", "Filtre gaussien", "Extraction de contours", "Erosion", "Dilatation"], label="Opération")
threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False)
width = gr.Number(value=100, label="Largeur", visible=False)
height = gr.Number(value=100, label="Hauteur", visible=False)
angle = gr.Number(value=0, label="Angle de Rotation", visible=False)
kernel_width = gr.Number(value=5, label="Largeur du kernel du filtre gaussien", visible=False)
kernel_height = gr.Number(value=5, label="Hauteur du kernel du filtre gaussien", visible=False)
taille_e = gr.Number(value=3, label="Taille du filtre pour l'érosion", visible=False)
taille_d = gr.Number(value=3, label="Taille du filtre pour la dilatation", visible=False)
image_output = gr.Image(label="Image Modifiée")
def update_inputs(operation):
if operation == "Binarisation":
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), gr.update(visible=False)
elif operation == "Redimensionner":
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), gr.update(visible=False)
elif operation == "Rotation":
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), gr.update(visible=False)
elif operation == "Filtre gaussien":
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), gr.update(visible=False)
elif operation == "Erosion":
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), gr.update(visible=False)
elif operation == "Dilatation":
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), gr.update(visible=True)
else:
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), gr.update(visible=False)
operation.change(update_inputs, inputs=operation, outputs=[threshold, width, height, angle, kernel_width, kernel_height, taille_e, taille_d])
submit_button = gr.Button("Appliquer")
submit_button.click(image_processing, inputs=[image_input, operation, threshold, width, height, angle, kernel_width, kernel_height, taille_e, taille_d], outputs=image_output)
demo.launch(share=True)
|