shashank1024 commited on
Commit
91f3098
·
verified ·
1 Parent(s): e0bc829

Create cv2

Browse files
Files changed (1) hide show
  1. cv2 +50 -0
cv2 ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import gradio as gr
4
+
5
+ def apply_blur(image):
6
+ return cv2.GaussianBlur(image, (15, 15), 0)
7
+
8
+ def apply_invert(image):
9
+ return cv2.bitwise_not(image)
10
+
11
+ def apply_sharpen(image):
12
+ kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
13
+ return cv2.filter2D(image, -1, kernel)
14
+
15
+ def apply_black_white(image):
16
+ return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
17
+
18
+ def apply_filter(image, filter_type):
19
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
20
+
21
+ if filter_type == 'blur':
22
+ result = apply_blur(image)
23
+ elif filter_type == 'invert':
24
+ result = apply_invert(image)
25
+ elif filter_type == 'sharpen':
26
+ result = apply_sharpen(image)
27
+ elif filter_type == 'black & white':
28
+ result = apply_black_white(image)
29
+ else:
30
+ result = image
31
+
32
+ if len(result.shape) == 2:
33
+ result = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR)
34
+
35
+ return cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
36
+
37
+ def run_interface():
38
+ filter_choices = ['blur', 'invert', 'sharpen', 'black & white']
39
+
40
+ interface = gr.Interface(
41
+ fn=apply_filter,
42
+ inputs=[gr.Image(type='numpy'),
43
+ gr.Dropdown(filter_choices, label='Select Filter')],
44
+ outputs=gr.Image(type='numpy'),
45
+ live=2
46
+ )
47
+ return interface
48
+
49
+ iface = run_interface()
50
+ iface.launch()