Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Farklı filtreleri tanımlama
|
6 |
+
def apply_filter(image, filter_type):
|
7 |
+
if filter_type == "Blur":
|
8 |
+
return cv2.GaussianBlur(image, (15, 15), 0)
|
9 |
+
elif filter_type == "Sharpen":
|
10 |
+
sharpen_kernel = np.array([
|
11 |
+
[0, -1, 0],
|
12 |
+
[-1, 5, -1],
|
13 |
+
[0, -1, 0]
|
14 |
+
])
|
15 |
+
return cv2.filter2D(image, -1, sharpen_kernel)
|
16 |
+
elif filter_type == "Edge Detection":
|
17 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
18 |
+
edges = cv2.Canny(gray, 100, 200)
|
19 |
+
return cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
20 |
+
elif filter_type == "Grayscale":
|
21 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
22 |
+
elif filter_type == "Sepia":
|
23 |
+
sepia_filter = np.array([
|
24 |
+
[0.272, 0.534, 0.131],
|
25 |
+
[0.349, 0.686, 0.168],
|
26 |
+
[0.393, 0.769, 0.189]
|
27 |
+
])
|
28 |
+
return cv2.transform(image, sepia_filter)
|
29 |
+
elif filter_type == "Negative":
|
30 |
+
return cv2.bitwise_not(image)
|
31 |
+
elif filter_type == "Emboss":
|
32 |
+
emboss_kernel = np.array([
|
33 |
+
[-2, -1, 0],
|
34 |
+
[-1, 1, 1],
|
35 |
+
[0, 1, 2]
|
36 |
+
])
|
37 |
+
return cv2.filter2D(image, -1, emboss_kernel)
|
38 |
+
else:
|
39 |
+
return image
|
40 |
+
|
41 |
+
# Gradio arayüz fonksiyonu
|
42 |
+
def process_image(image, filter_type):
|
43 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
44 |
+
output_image = apply_filter(image, filter_type)
|
45 |
+
if filter_type == "Grayscale":
|
46 |
+
return output_image # Grayscale çıktı zaten tek kanallı olur
|
47 |
+
else:
|
48 |
+
return cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
|
49 |
+
|
50 |
+
# Gradio arayüzü tanımlama
|
51 |
+
filter_choices = [
|
52 |
+
"Original", "Blur", "Sharpen", "Edge Detection",
|
53 |
+
"Grayscale", "Sepia", "Negative", "Emboss"
|
54 |
+
]
|
55 |
+
|
56 |
+
interface = gr.Interface(
|
57 |
+
fn=process_image,
|
58 |
+
inputs=[
|
59 |
+
gr.Image(type="numpy", label="Upload Image"),
|
60 |
+
gr.Radio(choices=filter_choices, label="Choose Filter")
|
61 |
+
],
|
62 |
+
outputs=gr.Image(type="numpy", label="Filtered Image"),
|
63 |
+
title="Advanced Image Filter Application",
|
64 |
+
description="Upload an image and apply various filters using OpenCV."
|
65 |
+
)
|
66 |
+
|
67 |
+
# Arayüzü başlat
|
68 |
+
interface.launch()
|