Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import cv2 as cv
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Filter functions
|
| 6 |
+
def apply_filter(image, filter_type, brightness=0, blur_level=1):
|
| 7 |
+
if filter_type == "Grayscale":
|
| 8 |
+
return cv.cvtColor(image, cv.COLOR_BGR2GRAY)
|
| 9 |
+
elif filter_type == "Blur":
|
| 10 |
+
blur_value = max(1, 2 * int(blur_level) + 1)
|
| 11 |
+
return cv.GaussianBlur(image, (blur_value, blur_value), 0)
|
| 12 |
+
elif filter_type == "Edge Detection":
|
| 13 |
+
gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
|
| 14 |
+
return cv.Canny(gray_image, 100, 200)
|
| 15 |
+
elif filter_type == "Brightness Increase":
|
| 16 |
+
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
|
| 17 |
+
hsv[:, :, 2] = cv.add(hsv[:, :, 2], brightness)
|
| 18 |
+
return cv.cvtColor(hsv, cv.COLOR_HSV2BGR)
|
| 19 |
+
elif filter_type == "Sepia":
|
| 20 |
+
sepia_filter = np.array([[0.272, 0.534, 0.131],
|
| 21 |
+
[0.349, 0.686, 0.168],
|
| 22 |
+
[0.393, 0.769, 0.189]])
|
| 23 |
+
sepia_image = cv.transform(image, sepia_filter)
|
| 24 |
+
sepia_image = np.clip(sepia_image, 0, 255).astype(np.uint8)
|
| 25 |
+
return sepia_image
|
| 26 |
+
elif filter_type == "Invert Colors":
|
| 27 |
+
return cv.bitwise_not(image)
|
| 28 |
+
elif filter_type == "Sharpen":
|
| 29 |
+
sharpen_filter = np.array([[0, -1, 0],
|
| 30 |
+
[-1, 5,-1],
|
| 31 |
+
[0, -1, 0]])
|
| 32 |
+
return cv.filter2D(image, -1, sharpen_filter)
|
| 33 |
+
else:
|
| 34 |
+
return image # Return original image by default
|
| 35 |
+
|
| 36 |
+
# Gradio interface
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("Upload an image and select a filter to apply.")
|
| 39 |
+
gr.Markdown('Drop the file..')
|
| 40 |
+
# Input and Output components
|
| 41 |
+
image_input = gr.Image(type="numpy", label="Input Image")
|
| 42 |
+
filter_buttons = gr.Radio(["Grayscale", "Blur", "Edge Detection", "Brightness Increase", "Sepia", "Invert Colors", "Sharpen"],
|
| 43 |
+
label="Choose a Filter", interactive=True)
|
| 44 |
+
brightness_slider = gr.Slider(0, 100, step=1, label="Brightness Level", visible=False)
|
| 45 |
+
blur_slider = gr.Slider(1, 20, step=1, label="Blur Level", visible=False)
|
| 46 |
+
image_output = gr.Image(type="numpy", label="Output Image")
|
| 47 |
+
|
| 48 |
+
# Update slider visibility based on filter type
|
| 49 |
+
def update_visibility(filter_type):
|
| 50 |
+
return {
|
| 51 |
+
brightness_slider: gr.update(visible=(filter_type == "Brightness Increase")),
|
| 52 |
+
blur_slider: gr.update(visible=(filter_type == "Blur")),
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
# Trigger live updates when sliders or filter type change
|
| 56 |
+
filter_buttons.change(fn=update_visibility, inputs=filter_buttons, outputs=[brightness_slider, blur_slider])
|
| 57 |
+
filter_buttons.change(fn=apply_filter, inputs=[image_input, filter_buttons, brightness_slider, blur_slider], outputs=image_output)
|
| 58 |
+
brightness_slider.change(fn=apply_filter, inputs=[image_input, filter_buttons, brightness_slider, blur_slider], outputs=image_output)
|
| 59 |
+
blur_slider.change(fn=apply_filter, inputs=[image_input, filter_buttons, brightness_slider, blur_slider], outputs=image_output)
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
demo.launch()
|