Dekonstruktio commited on
Commit
5d84b16
·
verified ·
1 Parent(s): 0165541

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -138
app.py CHANGED
@@ -1,138 +1,134 @@
1
- import cv2
2
- import numpy as np
3
- import gradio as gr
4
- import random
5
-
6
- def apply_cartoon_filter(frame):
7
- """Cartoon Filter"""
8
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
9
- gray = cv2.medianBlur(gray, 5)
10
- edges = cv2.adaptiveThreshold(gray, 255,
11
- cv2.ADAPTIVE_THRESH_MEAN_C,
12
- cv2.THRESH_BINARY, 11, 7)
13
- color = cv2.bilateralFilter(frame, 9, 300, 300)
14
- cartoon = cv2.bitwise_and(color, color, mask=edges)
15
- return cartoon
16
-
17
- def apply_neon_effect(frame):
18
- """Neon Light Filter"""
19
- # Intensify colors
20
- frame_neon = frame.copy().astype(np.float32)
21
- frame_neon = np.clip(frame_neon * 1.5, 0, 255).astype(np.uint8)
22
-
23
- # Highlight edges
24
- edges = cv2.Canny(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), 100, 200)
25
- edges_colored = cv2.applyColorMap(edges, cv2.COLORMAP_JET)
26
-
27
- # Blend
28
- result = cv2.addWeighted(frame_neon, 0.7, edges_colored, 0.3, 0)
29
- return result
30
-
31
- def apply_pixelate_effect(frame, pixel_size=15):
32
- """Pixelate Effect"""
33
- h, w = frame.shape[:2]
34
- small = cv2.resize(frame, (w//pixel_size, h//pixel_size), interpolation=cv2.INTER_LINEAR)
35
- return cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
36
-
37
- def apply_glitch_effect(frame):
38
- """Glitch Filter"""
39
- glitched = frame.copy()
40
-
41
- # Randomly shift color channels
42
- glitched[:, :, 0] = np.roll(glitched[:, :, 0], random.randint(-50, 50), axis=0)
43
- glitched[:, :, 1] = np.roll(glitched[:, :, 1], random.randint(-50, 50), axis=1)
44
-
45
- # Add noise to random areas
46
- noise = np.random.randint(0, 255, frame.shape, dtype=np.uint8)
47
- glitched = cv2.addWeighted(glitched, 0.7, noise, 0.3, 0)
48
-
49
- return glitched
50
-
51
- def apply_watercolor_effect(frame):
52
- """Watercolor Effect"""
53
- # Smooth using bilateral filtering
54
- frame_soft = cv2.bilateralFilter(frame, 9, 75, 75)
55
-
56
- # Highlight edges
57
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
58
- edges = cv2.Canny(gray, 100, 200)
59
- edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
60
-
61
- # Blend
62
- result = cv2.addWeighted(frame_soft, 0.8, edges, 0.2, 0)
63
- return result
64
-
65
- def apply_upscale(frame, scale_factor=1.5):
66
- """
67
- Upscaling Effect
68
-
69
- Args:
70
- frame (numpy.ndarray): Input Image
71
- scale_factor (float): Scaling Factor (default 1.5)
72
-
73
- Returns:
74
- numpy.ndarray: Upscaled Image
75
- """
76
- interpolation_methods = [
77
- cv2.INTER_CUBIC,
78
- cv2.INTER_LANCZOS4
79
- ]
80
-
81
- method = random.choice(interpolation_methods)
82
-
83
- height, width = frame.shape[:2]
84
- new_height = int(height * scale_factor)
85
- new_width = int(width * scale_factor)
86
-
87
- upscaled = cv2.resize(frame, (new_width, new_height), interpolation=method)
88
-
89
- kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
90
- sharpened = cv2.filter2D(upscaled, -1, kernel)
91
-
92
- return sharpened
93
-
94
- def apply_filter(filter_type, input_image=None):
95
- if input_image is None:
96
- cap = cv2.VideoCapture(0)
97
- ret, frame = cap.read()
98
- cap.release()
99
- if not ret:
100
- return "Failed to capture image from webcam"
101
- else:
102
- frame = input_image
103
-
104
- if filter_type == "Upscale":
105
- return apply_upscale(frame)
106
- elif filter_type == "Cartoon":
107
- return apply_cartoon_filter(frame)
108
- elif filter_type == "Neon Light":
109
- return apply_neon_effect(frame)
110
- elif filter_type == "Pixelate":
111
- return apply_pixelate_effect(frame)
112
- elif filter_type == "Glitch":
113
- return apply_glitch_effect(frame)
114
- elif filter_type == "Watercolor":
115
- return apply_watercolor_effect(frame)
116
-
117
- # Gradio interface
118
- with gr.Blocks() as demo:
119
- gr.Markdown('# <p align="center"> OpenCV Image Effects </p>')
120
-
121
- # Filter options
122
- filter_type = gr.Dropdown(
123
- label="Select Filter",
124
- choices=["Upscale","Cartoon", "Neon Light", "Pixelate", "Glitch", "Watercolor"],
125
- value="Upscale"
126
- )
127
-
128
- with gr.Row():
129
- input_image = gr.Image(label="Upload Image", type="numpy")
130
- output_image = gr.Image(label="Filtered Image")
131
-
132
- # Apply filter button
133
- apply_button = gr.Button("Apply Filter")
134
-
135
- # Apply filter function on button click
136
- apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
137
-
138
- demo.launch()
 
1
+ import cv2
2
+ import numpy as np
3
+ import gradio as gr
4
+ import random
5
+
6
+ def apply_cartoon_filter(frame):
7
+ """Cartoon Filter"""
8
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
9
+ gray = cv2.medianBlur(gray, 5)
10
+ edges = cv2.adaptiveThreshold(gray, 255,
11
+ cv2.ADAPTIVE_THRESH_MEAN_C,
12
+ cv2.THRESH_BINARY, 11, 7)
13
+ color = cv2.bilateralFilter(frame, 9, 300, 300)
14
+ cartoon = cv2.bitwise_and(color, color, mask=edges)
15
+ return cartoon
16
+
17
+ def apply_neon_effect(frame):
18
+ """Neon Light Filter"""
19
+ # Intensify colors
20
+ frame_neon = frame.copy().astype(np.float32)
21
+ frame_neon = np.clip(frame_neon * 1.5, 0, 255).astype(np.uint8)
22
+
23
+ # Highlight edges
24
+ edges = cv2.Canny(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), 100, 200)
25
+ edges_colored = cv2.applyColorMap(edges, cv2.COLORMAP_JET)
26
+
27
+ # Blend
28
+ result = cv2.addWeighted(frame_neon, 0.7, edges_colored, 0.3, 0)
29
+ return result
30
+
31
+ def apply_pixelate_effect(frame, pixel_size=15):
32
+ """Pixelate Effect"""
33
+ h, w = frame.shape[:2]
34
+ small = cv2.resize(frame, (w//pixel_size, h//pixel_size), interpolation=cv2.INTER_LINEAR)
35
+ return cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
36
+
37
+ def apply_glitch_effect(frame):
38
+ """Glitch Filter"""
39
+ glitched = frame.copy()
40
+
41
+ # Randomly shift color channels
42
+ glitched[:, :, 0] = np.roll(glitched[:, :, 0], random.randint(-50, 50), axis=0)
43
+ glitched[:, :, 1] = np.roll(glitched[:, :, 1], random.randint(-50, 50), axis=1)
44
+
45
+ return glitched
46
+
47
+ def apply_watercolor_effect(frame):
48
+ """Watercolor Effect"""
49
+ # Smooth using bilateral filtering
50
+ frame_soft = cv2.bilateralFilter(frame, 9, 75, 75)
51
+
52
+ # Highlight edges
53
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
54
+ edges = cv2.Canny(gray, 100, 200)
55
+ edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
56
+
57
+ # Blend
58
+ result = cv2.addWeighted(frame_soft, 0.8, edges, 0.2, 0)
59
+ return result
60
+
61
+ def apply_upscale(frame, scale_factor=1.5):
62
+ """
63
+ Upscaling Effect
64
+
65
+ Args:
66
+ frame (numpy.ndarray): Input Image
67
+ scale_factor (float): Scaling Factor (default 1.5)
68
+
69
+ Returns:
70
+ numpy.ndarray: Upscaled Image
71
+ """
72
+ interpolation_methods = [
73
+ cv2.INTER_CUBIC,
74
+ cv2.INTER_LANCZOS4
75
+ ]
76
+
77
+ method = random.choice(interpolation_methods)
78
+
79
+ height, width = frame.shape[:2]
80
+ new_height = int(height * scale_factor)
81
+ new_width = int(width * scale_factor)
82
+
83
+ upscaled = cv2.resize(frame, (new_width, new_height), interpolation=method)
84
+
85
+ kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
86
+ sharpened = cv2.filter2D(upscaled, -1, kernel)
87
+
88
+ return sharpened
89
+
90
+ def apply_filter(filter_type, input_image=None):
91
+ if input_image is None:
92
+ cap = cv2.VideoCapture(0)
93
+ ret, frame = cap.read()
94
+ cap.release()
95
+ if not ret:
96
+ return "Failed to capture image from webcam"
97
+ else:
98
+ frame = input_image
99
+
100
+ if filter_type == "Upscale":
101
+ return apply_upscale(frame)
102
+ elif filter_type == "Cartoon":
103
+ return apply_cartoon_filter(frame)
104
+ elif filter_type == "Neon Light":
105
+ return apply_neon_effect(frame)
106
+ elif filter_type == "Pixelate":
107
+ return apply_pixelate_effect(frame)
108
+ elif filter_type == "Glitch":
109
+ return apply_glitch_effect(frame)
110
+ elif filter_type == "Watercolor":
111
+ return apply_watercolor_effect(frame)
112
+
113
+ # Gradio interface
114
+ with gr.Blocks() as demo:
115
+ gr.Markdown('# <p align="center"> OpenCV Image Effects </p>')
116
+
117
+ # Filter options
118
+ filter_type = gr.Dropdown(
119
+ label="Select Filter",
120
+ choices=["Upscale","Cartoon", "Neon Light", "Pixelate", "Glitch", "Watercolor"],
121
+ value="Upscale"
122
+ )
123
+
124
+ with gr.Row():
125
+ input_image = gr.Image(label="Upload Image", type="numpy")
126
+ output_image = gr.Image(label="Filtered Image")
127
+
128
+ # Apply filter button
129
+ apply_button = gr.Button("Apply Filter")
130
+
131
+ # Apply filter function on button click
132
+ apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
133
+
134
+ demo.launch()