selva1909 commited on
Commit
02546ba
·
verified ·
1 Parent(s): 6a5d197

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+
6
+
7
+ # -----------------------------
8
+ # KERNELS
9
+ # -----------------------------
10
+ kernels = {
11
+ "Blur": np.ones((3, 3), np.float32) / 9,
12
+
13
+ "Sharpen": np.array([
14
+ [0, -1, 0],
15
+ [-1, 5, -1],
16
+ [0, -1, 0]
17
+ ]),
18
+
19
+ "Edge Detection": np.array([
20
+ [-1, -1, -1],
21
+ [-1, 8, -1],
22
+ [-1, -1, -1]
23
+ ]),
24
+
25
+ "Emboss": np.array([
26
+ [-2, -1, 0],
27
+ [-1, 1, 1],
28
+ [0, 1, 2]
29
+ ]),
30
+
31
+ "Sobel X": np.array([
32
+ [-1, 0, 1],
33
+ [-2, 0, 2],
34
+ [-1, 0, 1]
35
+ ]),
36
+
37
+ "Sobel Y": np.array([
38
+ [-1, -2, -1],
39
+ [0, 0, 0],
40
+ [1, 2, 1]
41
+ ])
42
+ }
43
+
44
+
45
+ # -----------------------------
46
+ # MAIN FUNCTION
47
+ # -----------------------------
48
+ def process_image(image, kernel_name):
49
+
50
+ # Convert RGB → Grayscale
51
+ gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
52
+
53
+ # Get selected kernel
54
+ kernel = kernels[kernel_name]
55
+
56
+ # Apply convolution
57
+ filtered = cv2.filter2D(gray, -1, kernel)
58
+
59
+ # Histogram Equalization
60
+ equalized = cv2.equalizeHist(gray)
61
+
62
+ # Thresholding
63
+ _, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
64
+
65
+ # Edge Detection
66
+ canny = cv2.Canny(gray, 100, 200)
67
+
68
+ # -----------------------------
69
+ # VISUALIZATION FIGURE
70
+ # -----------------------------
71
+ fig, axs = plt.subplots(2, 3, figsize=(12, 8))
72
+
73
+ axs[0, 0].imshow(image)
74
+ axs[0, 0].set_title("Original Image")
75
+ axs[0, 0].axis("off")
76
+
77
+ axs[0, 1].imshow(gray, cmap='gray')
78
+ axs[0, 1].set_title("Grayscale")
79
+ axs[0, 1].axis("off")
80
+
81
+ axs[0, 2].imshow(filtered, cmap='gray')
82
+ axs[0, 2].set_title(f"{kernel_name} Output")
83
+ axs[0, 2].axis("off")
84
+
85
+ axs[1, 0].imshow(equalized, cmap='gray')
86
+ axs[1, 0].set_title("Histogram Equalization")
87
+ axs[1, 0].axis("off")
88
+
89
+ axs[1, 1].imshow(binary, cmap='gray')
90
+ axs[1, 1].set_title("Binary Threshold")
91
+ axs[1, 1].axis("off")
92
+
93
+ axs[1, 2].imshow(canny, cmap='gray')
94
+ axs[1, 2].set_title("Canny Edge Detection")
95
+ axs[1, 2].axis("off")
96
+
97
+ plt.tight_layout()
98
+
99
+ return fig
100
+
101
+
102
+ # -----------------------------
103
+ # GRADIO UI
104
+ # -----------------------------
105
+ demo = gr.Interface(
106
+ fn=process_image,
107
+
108
+ inputs=[
109
+ gr.Image(type="numpy", label="Upload Image"),
110
+
111
+ gr.Dropdown(
112
+ choices=list(kernels.keys()),
113
+ value="Edge Detection",
114
+ label="Select Kernel"
115
+ )
116
+ ],
117
+
118
+ outputs=gr.Plot(label="Visualization"),
119
+
120
+ title="Kernel Matrix Visualization for Image Processing",
121
+
122
+ description="""
123
+ Upload an image and visualize:
124
+ - Grayscale conversion
125
+ - Convolution kernels
126
+ - Edge detection
127
+ - Thresholding
128
+ - Histogram Equalization
129
+ - CNN-style preprocessing
130
+ """
131
+ )
132
+
133
+ demo.launch()