Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
author : OzlemAkgunoglu
|
3 |
+
github : https://github.com/OzlemAkgunoglu
|
4 |
+
Dynamic Photo Filter App
|
5 |
+
This is a Dynamic Photo Filter App that allows you to apply various filters to your images.
|
6 |
+
Adjust brightness, contrast, sharpening, and select a filter for real-time changes.
|
7 |
+
And this app is created using OpenCV and Gradio. Thank you for using it. '''
|
8 |
+
|
9 |
+
#Let's load the necessary libraries
|
10 |
+
import cv2 as cv #OpenCV for image processing
|
11 |
+
import numpy as np #Numpy for arrays
|
12 |
+
import gradio as gr #Gradio for UI
|
13 |
+
|
14 |
+
# Let's define the filter functions
|
15 |
+
def apply_grayscale(image):
|
16 |
+
return cv.cvtColor(image, cv.COLOR_BGR2GRAY) #Convert the image to grayscale
|
17 |
+
|
18 |
+
#Sepia filter function
|
19 |
+
def apply_sepia(image):
|
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) #Apply the filter
|
24 |
+
return np.clip(sepia_image, 0, 255).astype(np.uint8) #clip to hold values between 0 and 255 prevent excessive brightness or darkening.
|
25 |
+
|
26 |
+
def apply_negative(image):
|
27 |
+
return cv.bitwise_not(image) #Invert the image
|
28 |
+
|
29 |
+
#sketch filter
|
30 |
+
'''Apply Gaussian blur to decrease the noise
|
31 |
+
and remove unwanted details in the image for better sketch effect '''
|
32 |
+
def apply_sketch(image):
|
33 |
+
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) #Convert the image to grayscale
|
34 |
+
inv = cv.bitwise_not(gray) #Invert the grayscale image
|
35 |
+
blurred = cv.GaussianBlur(inv, (21, 21), sigmaX=0, sigmaY=0)
|
36 |
+
sketch_image = cv.divide(gray, 255 - blurred, scale=256)
|
37 |
+
# divide= gray / (255 - blurred) Normalizes the division result to prevent overly high values by scaling with 256
|
38 |
+
return sketch_image
|
39 |
+
|
40 |
+
def apply_sharpen(image, sharpening):
|
41 |
+
sharpening_filter = np.array([[0, -1, 0],
|
42 |
+
[-1, 5 + sharpening, -1],
|
43 |
+
[0, -1, 0]])
|
44 |
+
return cv.filter2D(image, -1, sharpening_filter) #Apply the filter each pixel is multiplied by the value in the kernel
|
45 |
+
def apply_edge_detection(image):
|
46 |
+
return cv.Canny(image, 100, 200)
|
47 |
+
def apply_fall_filter(frame):
|
48 |
+
fall_filter = np.array([[0.393, 0.769, 0.189],
|
49 |
+
[0.349, 0.686, 0.168],
|
50 |
+
[0.272, 0.534, 0.131]])
|
51 |
+
return cv.transform(frame, fall_filter)
|
52 |
+
|
53 |
+
|
54 |
+
# Dictionary to map filter names to functions
|
55 |
+
filter_functions = {
|
56 |
+
"Grayscale": apply_grayscale,
|
57 |
+
"Sepia": apply_sepia,
|
58 |
+
"Negative": apply_negative,
|
59 |
+
"Sketch": apply_sketch,
|
60 |
+
"Sharpen": apply_sharpen,
|
61 |
+
"Edge Detection": apply_edge_detection,
|
62 |
+
"Fall": apply_fall_filter
|
63 |
+
}
|
64 |
+
|
65 |
+
# Main function to apply selected filters
|
66 |
+
def apply_filters(image, filter_type, brightness, contrast, sharpening):
|
67 |
+
if image is None:
|
68 |
+
print("Input image is empty!") #for debugging
|
69 |
+
return None # Return None if the input image is empty
|
70 |
+
|
71 |
+
# Adjust brightness and contrast
|
72 |
+
image = cv.convertScaleAbs(image, alpha=contrast, beta=brightness)
|
73 |
+
|
74 |
+
#for debugging
|
75 |
+
#print("Image after brightness and contrast adjustment:", image)
|
76 |
+
|
77 |
+
|
78 |
+
# Apply the selected filter from dictionary called filter_functions in line 53
|
79 |
+
if filter_type in filter_functions:
|
80 |
+
if filter_type == "Sharpen":
|
81 |
+
image = filter_functions[filter_type](image, sharpening) # Calls the Sharpen filter with the sharpening parameter for custom sharpening level
|
82 |
+
else:
|
83 |
+
image = filter_functions[filter_type](image)
|
84 |
+
|
85 |
+
return image
|
86 |
+
|
87 |
+
# Define Interface
|
88 |
+
with gr.Blocks() as app:
|
89 |
+
# Title and Description
|
90 |
+
gr.Markdown("<h1 style='text-align: center;'>Dynamic Photo Filter App</h1>")
|
91 |
+
gr.Markdown("This app allows you to apply various filters to your images. Adjust brightness, contrast, sharpening, and select a filter for real-time changes.")
|
92 |
+
|
93 |
+
# Choices and Sliders at the Top
|
94 |
+
with gr.Row():
|
95 |
+
filter_choice = gr.Radio(["Original", "Grayscale", "Sketch", "Sepia", "Negative", "Sharpen", "Edge Detection","Fall"],label="Filter")
|
96 |
+
|
97 |
+
with gr.Column():
|
98 |
+
brightness_slider = gr.Slider(-100, 100, step=1, label="Brightness", value=0)
|
99 |
+
contrast_slider = gr.Slider(0.5, 3.0, step=0.1, label="Contrast", value=1.0)
|
100 |
+
sharpening_slider = gr.Slider(0, 5, step=0.1, label="Sharpening", value=0)
|
101 |
+
|
102 |
+
# Horizontal display of the images
|
103 |
+
with gr.Row():
|
104 |
+
image_input = gr.Image(label="Upload Image", type="numpy")
|
105 |
+
image_output = gr.Image(label="Filtered Image")
|
106 |
+
|
107 |
+
# Link events for real-time updates
|
108 |
+
image_input.change(apply_filters, inputs=[image_input, filter_choice, brightness_slider, contrast_slider, sharpening_slider], outputs=image_output)
|
109 |
+
filter_choice.change(apply_filters, inputs=[image_input, filter_choice, brightness_slider, contrast_slider, sharpening_slider], outputs=image_output)
|
110 |
+
brightness_slider.change(apply_filters, inputs=[image_input, filter_choice, brightness_slider, contrast_slider, sharpening_slider], outputs=image_output)
|
111 |
+
contrast_slider.change(apply_filters, inputs=[image_input, filter_choice, brightness_slider, contrast_slider, sharpening_slider], outputs=image_output)
|
112 |
+
sharpening_slider.change(apply_filters, inputs=[image_input, filter_choice, brightness_slider, contrast_slider, sharpening_slider], outputs=image_output)
|
113 |
+
|
114 |
+
app.launch(share=True)
|