File size: 4,721 Bytes
a3f0f2d
 
 
 
 
 
 
 
d85418d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3f0f2d
 
 
 
d85418d
dbdffeb
d85418d
 
 
 
 
 
 
a3f0f2d
 
d85418d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3f0f2d
 
1705ce6
a3f0f2d
1c73813
d85418d
fd32459
d85418d
54f4fee
a3f0f2d
d85418d
 
a3f0f2d
d85418d
f90960c
a3f0f2d
 
f90960c
 
 
d85418d
a3f0f2d
 
 
 
 
 
 
 
d85418d
a3f0f2d
d85418d
 
 
 
 
 
 
 
a3f0f2d
 
 
 
1705ce6
d85418d
b8e12f6
3e282cd
a3f0f2d
d85418d
 
f90960c
d85418d
 
f90960c
a3f0f2d
 
 
 
 
dbdffeb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import gradio as gr
import cv2
import numpy as np
from registry import registry
from filters import *
from components import create_filter_controls

def create_app():
    # Định nghĩa theme tùy chỉnh
    theme = gr.themes.Soft(
        primary_hue="indigo",
        secondary_hue="slate",
    ).set(
        body_background_fill="*background_fill_secondary",
        block_background_fill="*background_fill_primary",
        block_border_width="0",
        block_shadow="*shadow_drop_lg",
        button_primary_background_fill="*primary_500",
        button_primary_background_fill_hover="*primary_600",
        button_primary_text_color="white",
    )

    with gr.Blocks(theme=theme) as app:
        gr.Markdown("""
        # 📷 Photo Filter App
        ### Chỉnh sửa ảnh với các bộ lọc chuyên nghiệp
        """)
        
        # Khởi tạo components
        controls = create_filter_controls()
        filter_names = list(registry.filters.keys())
        filter_groups = controls

        with gr.Row(equal_height=True):
            with gr.Column(scale=1):
                input_image = gr.Image(
                    label="Ảnh gốc",
                    type="numpy",
                    tool="select",
                    height=400
                )
                
                with gr.Row():
                    filter_select = gr.Dropdown(
                        label="Chọn bộ lọc",
                        choices=filter_names,
                        value="Original",
                        container=False
                    )
                    apply_button = gr.Button(
                        "Áp dụng",
                        variant="primary",
                        size="sm"
                    )

                # Tạo container cho các điều khiển
                with gr.Box():
                    gr.Markdown("### Tùy chỉnh bộ lọc")
                    control_components = []
                    for filter_name, group in filter_groups.items():
                        for component in group.children:
                            control_components.append(component)
                
                filter_doc = gr.Markdown(
                    container=False,
                    show_label=False
                )

            with gr.Column(scale=1):
                output_image = gr.Image(
                    label="Ảnh đã chỉnh sửa",
                    height=400,
                    show_download_button=True
                )

        # Xử lý cập nhật UI
        def update_controls(filter_name):
            updates = []
            for group_name, group in filter_groups.items():
                updates.append(gr.update(visible=group_name == filter_name))
            
            doc = registry.filters[filter_name].__doc__ or "Không có mô tả chi tiết."
            return updates + [doc]

        # Xử lý ảnh
        def process(image, filter_name, *args):
            if image is None:
                return None, gr.update(visible=True, value="⚠️ Vui lòng chọn ảnh trước khi áp dụng bộ lọc")
            
            try:
                image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
                params = {}
                param_names = list(registry.params_map.get(filter_name, {}).keys())
                for i, param_name in enumerate(param_names):
                    params[param_name] = args[i]
                
                processed = registry.filters[filter_name](image, **params)
                
                if len(processed.shape) == 2:
                    processed = cv2.cvtColor(processed, cv2.COLOR_GRAY2RGB)
                else:
                    processed = cv2.cvtColor(processed, cv2.COLOR_BGR2RGB)
                
                return processed, gr.update(visible=False)
            except Exception as e:
                return None, gr.update(visible=True, value=f"❌ Lỗi xử lý ảnh: {str(e)}")

        # Thêm thông báo lỗi
        error_message = gr.Markdown(
            visible=False,
            value="",
            container=False
        )

        # Kết nối sự kiện
        filter_select.change(
            update_controls,
            inputs=filter_select,
            outputs=list(filter_groups.values()) + [filter_doc],
            api_name=False
        )
        
        input_components = [input_image, filter_select] + control_components
        apply_button.click(
            process,
            inputs=input_components,
            outputs=[output_image, error_message],
        )

    return app

if __name__ == "__main__":
    app = create_app()
    app.launch(share=True)