eienmojiki commited on
Commit
a3f0f2d
·
verified ·
1 Parent(s): c5ead0d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from registry import registry
5
+ from filters import *
6
+ from components import create_filter_controls
7
+
8
+ def create_app():
9
+ with gr.Blocks() as app:
10
+ gr.Markdown("# 📷 Real-Time Photo Filter App")
11
+
12
+ # Khởi tạo components
13
+ controls = create_filter_controls()
14
+ filter_names = list(registry.filters.keys())
15
+
16
+ with gr.Row():
17
+ with gr.Column():
18
+ input_image = gr.Image(label="Input Image", type="numpy")
19
+ filter_select = gr.Dropdown(
20
+ label="Select Filter",
21
+ choices=filter_names,
22
+ value="Original"
23
+ )
24
+
25
+ # Thêm các control vào UI
26
+ control_components = [c for c in controls.values()]
27
+ for component in control_components:
28
+ component.render()
29
+
30
+ with gr.Column():
31
+ output_image = gr.Image(label="Filtered Image")
32
+
33
+ # Xử lý cập nhật UI
34
+ def update_controls(filter_name):
35
+ updates = []
36
+ for key in controls:
37
+ current_filter, _ = key.split('_', 1)
38
+ updates.append(gr.update(visible=current_filter == filter_name))
39
+ return updates
40
+
41
+ # Xử lý ảnh real-time
42
+ def process(image, filter_name, **kwargs):
43
+ if image is None:
44
+ return None
45
+
46
+ try:
47
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
48
+ params = {
49
+ k.split('_', 1)[1]: v
50
+ for k, v in kwargs.items()
51
+ if k.startswith(filter_name)
52
+ }
53
+
54
+ processed = registry.filters[filter_name](image, **params)
55
+
56
+ if len(processed.shape) == 2:
57
+ processed = cv2.cvtColor(processed, cv2.COLOR_GRAY2RGB)
58
+ else:
59
+ processed = cv2.cvtColor(processed, cv2.COLOR_BGR2RGB)
60
+
61
+ return processed
62
+ except Exception as e:
63
+ print(f"Error processing image: {e}")
64
+ return image
65
+
66
+ # Kết nối sự kiện
67
+ filter_select.change(
68
+ update_controls,
69
+ inputs=filter_select,
70
+ outputs=list(controls.values())
71
+
72
+ components = [input_image, filter_select] + list(controls.values())
73
+ for component in components:
74
+ component.change(
75
+ process,
76
+ inputs=components,
77
+ outputs=output_image,
78
+ show_progress=False
79
+ )
80
+
81
+ return app
82
+
83
+ if __name__ == "__main__":
84
+ app = create_app()
85
+ app.launch()