Spaces:
Runtime error
Runtime error
Correction
Browse files
app.py
CHANGED
|
@@ -65,26 +65,45 @@ def histogramme(image):
|
|
| 65 |
|
| 66 |
return Image.fromarray(data[:,:,:3])
|
| 67 |
|
| 68 |
-
def
|
| 69 |
"""
|
| 70 |
Détecter et afficher les contours des images
|
| 71 |
"""
|
| 72 |
img = np.array(image)
|
| 73 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
def
|
| 78 |
"""
|
| 79 |
-
Appliquer un
|
| 80 |
"""
|
| 81 |
img = np.array(image)
|
| 82 |
flou = cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
|
| 83 |
return Image.fromarray(flou)
|
| 84 |
|
| 85 |
-
def
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
def update_fields(operation):
|
| 90 |
if operation == "Redimensionner":
|
|
@@ -105,7 +124,7 @@ def update_fields(operation):
|
|
| 105 |
gr.update(visible=False),
|
| 106 |
gr.update(visible=True),
|
| 107 |
gr.update(visible=False))
|
| 108 |
-
elif operation == "
|
| 109 |
return (gr.update(visible=False),
|
| 110 |
gr.update(visible=False),
|
| 111 |
gr.update(visible=False),
|
|
@@ -128,10 +147,14 @@ def image_processing(image, operation, threshold=128, width=100, height=100, ang
|
|
| 128 |
return resize_image(image, width, height)
|
| 129 |
elif operation == "Rotation":
|
| 130 |
return rotate_image(image, angle)
|
| 131 |
-
elif operation == "
|
| 132 |
-
return
|
| 133 |
-
elif operation == "
|
| 134 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
return image
|
| 137 |
|
|
@@ -144,8 +167,21 @@ with gr.Blocks() as demo:
|
|
| 144 |
gr.Markdown("## Image Master")
|
| 145 |
|
| 146 |
with gr.Row():
|
| 147 |
-
image_input = gr.Image(
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False)
|
| 150 |
width = gr.Slider(minimum=50, maximum=1000, value=100, step=10, label="Largeur", visible=False)
|
| 151 |
height = gr.Slider(minimum=50, maximum=1000, value=100, step=10, label="Hauteur", visible=False)
|
|
|
|
| 65 |
|
| 66 |
return Image.fromarray(data[:,:,:3])
|
| 67 |
|
| 68 |
+
def contours_Sobel(image):
|
| 69 |
"""
|
| 70 |
Détecter et afficher les contours des images
|
| 71 |
"""
|
| 72 |
img = np.array(image)
|
| 73 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 74 |
+
sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
|
| 75 |
+
sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
|
| 76 |
+
sobel = np.sqrt(sobel_x**2 + sobel_y**2)
|
| 77 |
+
sobel = np.uint8(sobel)
|
| 78 |
+
return Image.fromarray(sobel)
|
| 79 |
|
| 80 |
+
def filtre_Gaussien(image, kernel_size=5):
|
| 81 |
"""
|
| 82 |
+
Appliquer un Filtre Gaussien avec une matrice définissable par l'utilisateur à l'image
|
| 83 |
"""
|
| 84 |
img = np.array(image)
|
| 85 |
flou = cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
|
| 86 |
return Image.fromarray(flou)
|
| 87 |
|
| 88 |
+
def erosion(image):
|
| 89 |
+
croix = np.array([
|
| 90 |
+
[0,1,0],
|
| 91 |
+
[1,1,1],
|
| 92 |
+
[0,1,0]], dtype=np.uint8)
|
| 93 |
+
croix = croix*255
|
| 94 |
+
image_test = np.array(image)
|
| 95 |
+
img_erosion = cv2.erode(image_test, croix)
|
| 96 |
+
return Image.fromarray(img_erosion)
|
| 97 |
+
|
| 98 |
+
def dilatation(image):
|
| 99 |
+
croix = np.array([
|
| 100 |
+
[0,1,0],
|
| 101 |
+
[1,1,1],
|
| 102 |
+
[0,1,0]], dtype=np.uint8)
|
| 103 |
+
croix = croix*255
|
| 104 |
+
image_test = np.array(image)
|
| 105 |
+
img_dilate = cv2.dilate(image_test, croix)
|
| 106 |
+
return Image.fromarray(img_dilate)
|
| 107 |
|
| 108 |
def update_fields(operation):
|
| 109 |
if operation == "Redimensionner":
|
|
|
|
| 124 |
gr.update(visible=False),
|
| 125 |
gr.update(visible=True),
|
| 126 |
gr.update(visible=False))
|
| 127 |
+
elif operation == "Filtre Gaussien":
|
| 128 |
return (gr.update(visible=False),
|
| 129 |
gr.update(visible=False),
|
| 130 |
gr.update(visible=False),
|
|
|
|
| 147 |
return resize_image(image, width, height)
|
| 148 |
elif operation == "Rotation":
|
| 149 |
return rotate_image(image, angle)
|
| 150 |
+
elif operation == "Contours Sobel":
|
| 151 |
+
return contours_Sobel(image)
|
| 152 |
+
elif operation == "Filtre Gaussien":
|
| 153 |
+
return filtre_Gaussien(image, kernel_size)
|
| 154 |
+
elif operation == "Erosion":
|
| 155 |
+
return erosion(image)
|
| 156 |
+
elif operation == "Dilatation":
|
| 157 |
+
return dilatation(image)
|
| 158 |
|
| 159 |
return image
|
| 160 |
|
|
|
|
| 167 |
gr.Markdown("## Image Master")
|
| 168 |
|
| 169 |
with gr.Row():
|
| 170 |
+
image_input = gr.Image(
|
| 171 |
+
type="pil",
|
| 172 |
+
label="Charger Image"
|
| 173 |
+
)
|
| 174 |
+
operation = gr.Radio(
|
| 175 |
+
["Négatif",
|
| 176 |
+
"Binarisation",
|
| 177 |
+
"Redimensionner",
|
| 178 |
+
"Rotation",
|
| 179 |
+
"Contours Sobel",
|
| 180 |
+
"Filtre Gaussien",
|
| 181 |
+
"Erosion",
|
| 182 |
+
"Dilatation"
|
| 183 |
+
],
|
| 184 |
+
label="Opération")
|
| 185 |
threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False)
|
| 186 |
width = gr.Slider(minimum=50, maximum=1000, value=100, step=10, label="Largeur", visible=False)
|
| 187 |
height = gr.Slider(minimum=50, maximum=1000, value=100, step=10, label="Hauteur", visible=False)
|