Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import sys
|
| 7 |
+
from PIL import Image,ImageFilter
|
| 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 binarize_image(image, threshold):
|
| 18 |
+
img_np = np.array(image.convert('L'))
|
| 19 |
+
_, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY)
|
| 20 |
+
return Image.fromarray(binary)
|
| 21 |
+
|
| 22 |
+
def resize_image(image, width, height):
|
| 23 |
+
#image=image.convert('L')
|
| 24 |
+
return image.resize((width, height))
|
| 25 |
+
|
| 26 |
+
def rotate_image(image, angle):
|
| 27 |
+
return image.rotate(angle)
|
| 28 |
+
|
| 29 |
+
def hister(image):
|
| 30 |
+
#image=cv2.imread(image,cv2.IMREAD_GRAYSCALE)
|
| 31 |
+
image=np.array(image.convert('L'))
|
| 32 |
+
if image is None:
|
| 33 |
+
print('Image load failed !')
|
| 34 |
+
sys.exit()
|
| 35 |
+
hist=cv2.calcHist([image],[0],None,[256],[0,256])
|
| 36 |
+
#cv2.imshow('Gray Scale Histogram',image)
|
| 37 |
+
#cv2.waitkey(1)
|
| 38 |
+
|
| 39 |
+
#plt.plot(hist)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
fig, ax = plt.subplots()
|
| 43 |
+
ax.plot(hist)
|
| 44 |
+
|
| 45 |
+
fig.canvas.draw()
|
| 46 |
+
img_plot = np.array(fig.canvas.renderer.buffer_rgba())
|
| 47 |
+
|
| 48 |
+
#cv2.imshow('Image', cv2.cvtColor(img_plot, cv2.COLOR_RGBA2BGR))
|
| 49 |
+
|
| 50 |
+
#cv2.waitKey(0)
|
| 51 |
+
#image_result=plt.savefig('histogram.png')
|
| 52 |
+
#plt.show()
|
| 53 |
+
#image_result=Image.open('histogram.png')
|
| 54 |
+
#image_result=cv2.imread('histogram.png')
|
| 55 |
+
return Image.fromarray(img_plot)
|
| 56 |
+
|
| 57 |
+
def gauss_filterer(image,radius):
|
| 58 |
+
return image.filter(ImageFilter.GaussianBlur(radius))
|
| 59 |
+
|
| 60 |
+
def contour_extraction(image):
|
| 61 |
+
image=image.convert('L')
|
| 62 |
+
return image.filter(ImageFilter.FIND_EDGES)
|
| 63 |
+
|
| 64 |
+
def erode(image):
|
| 65 |
+
return image.filter(ImageFilter.MinFilter(3))
|
| 66 |
+
|
| 67 |
+
def dilate(image):
|
| 68 |
+
return image.filter(ImageFilter.MaxFilter(3))
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Ajoutez d'autres fonctions pour l'histogramme, le filtrage, Sobel, etc.
|
| 75 |
+
|
| 76 |
+
# Interface Gradio
|
| 77 |
+
def image_processing(image, operation, threshold=128, width=100, height=100, angle=0,radius=9):
|
| 78 |
+
if operation == "Négatif":
|
| 79 |
+
return apply_negative(image)
|
| 80 |
+
elif operation == "Binarisation":
|
| 81 |
+
return binarize_image(image, threshold)
|
| 82 |
+
elif operation == "Redimensionner":
|
| 83 |
+
return resize_image(image, width, height)
|
| 84 |
+
elif operation == "Rotation":
|
| 85 |
+
return rotate_image(image, angle)
|
| 86 |
+
elif operation == "Histogramme":
|
| 87 |
+
#print("Yes ....")
|
| 88 |
+
return hister(image)
|
| 89 |
+
elif operation=="Filtre":
|
| 90 |
+
return gauss_filterer(image,radius)
|
| 91 |
+
elif operation=="Contour":
|
| 92 |
+
return contour_extraction(image)
|
| 93 |
+
elif operation=="Erosion":
|
| 94 |
+
return erode(image)
|
| 95 |
+
elif operation=="Dilatation":
|
| 96 |
+
return dilate(image)
|
| 97 |
+
# Ajouter d'autres conditions pour les autres opérations
|
| 98 |
+
return image
|
| 99 |
+
|
| 100 |
+
# Interface Gradio
|
| 101 |
+
with gr.Blocks() as demo:
|
| 102 |
+
gr.Markdown("## Image Processing")
|
| 103 |
+
gr.Markdown("This is the result of my Week 2 work based on image processing and filters .Let me make you discover it!")
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
image_input = gr.Image(type="pil", label="Charger Image")
|
| 107 |
+
operation = gr.Radio(["Négatif", "Binarisation", "Redimensionner", "Rotation","Histogramme","Filtre","Contour","Erosion","Dilatation"], label="Opération")
|
| 108 |
+
#advanced_operation=gr.Radio(["Histogramme"],label="Advanced")
|
| 109 |
+
threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False)
|
| 110 |
+
width = gr.Number(value=100, label="Largeur", visible=False)
|
| 111 |
+
height = gr.Number(value=100, label="Hauteur", visible=False)
|
| 112 |
+
angle = gr.Number(value=0, label="Angle de Rotation", visible=False)
|
| 113 |
+
|
| 114 |
+
image_output = gr.Image(label="Image Modifiée")
|
| 115 |
+
|
| 116 |
+
submit_button = gr.Button("Appliquer")
|
| 117 |
+
submit_button.click(fn=image_processing, inputs=[image_input, operation, threshold, width, height, angle], outputs=image_output)
|
| 118 |
+
|
| 119 |
+
# Lancer l'application Gradio
|
| 120 |
+
demo.launch()
|