File size: 3,651 Bytes
a3f0f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c73813
dbdffeb
a3f0f2d
 
 
 
 
 
 
 
 
 
1c73813
6f7bd55
 
1705ce6
6f7bd55
1c73813
a3f0f2d
 
 
 
 
1705ce6
a3f0f2d
1c73813
 
6f7bd55
1705ce6
a3f0f2d
4e37a66
a3f0f2d
830609c
 
 
a3f0f2d
 
 
 
 
 
6f7bd55
 
a3f0f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1705ce6
6f7bd55
3e282cd
a3f0f2d
1c73813
7b8d05e
 
 
 
 
 
 
a3f0f2d
 
12bf67c
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
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():
    with gr.Blocks() as app:
        gr.Markdown("# 📷 Real-Time Photo Filter App")
        
        # Khởi tạo components
        controls = create_filter_controls()
        filter_names = list(registry.filters.keys())
        
        filter_groups = {} # Store filter groups

        with gr.Row():
            with gr.Column():
                input_image = gr.Image(label="Input Image", type="numpy")
                filter_select = gr.Dropdown(
                    label="Select Filter",
                    choices=filter_names,
                    value="Original"
                )
                
                # Thêm các control vào UI
                control_components = []
                filter_groups = controls # Assign controls directly to filter_groups
                for filter_name, group in filter_groups.items(): # Iterate through groups
                    with group: # Put controls inside the group
                        for component in group.children:
                            control_components.append(component) # Collect sliders for event handling
            
            with gr.Column():
                output_image = gr.Image(label="Filtered Image")

        # Xử lý cập nhật UI
        def update_controls(filter_name):
            updates = []
            for group_name, group in filter_groups.items():
                visibility = (group_name == filter_name)
                updates.append(gr.Group.update(visible=visibility)) # Use gr.Group.update
            return updates


        # Xử lý ảnh real-time
        def process(*args, **kwargs):
            image = args[0] if len(args) > 0 else None
            filter_name = args[1] if len(args) > 1 else "Original" # Default filter
            if image is None:
                return None
                
            try:
                image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
                params = {
                    k.split('_', 1)[1]: v
                    for k, v in kwargs.items()
                    if k.startswith(filter_name)
                }
                
                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
            except Exception as e:
                print(f"Error processing image: {e}")
                return image

        # Kết nối sự kiện
        filter_select.change(
            update_controls,
            inputs=filter_select,
            outputs=[filter_groups[name] for name in filter_groups] # Pass list of groups as outputs
        )
        
        input_components_process = [input_image, filter_select] + control_components
        filter_select.change( # Attach change to filter_select
            process,
            inputs=input_components_process,
            outputs=output_image,
            show_progress=False
        )
        for component in control_components: # Attach change to sliders
            component.change(
                process,
                inputs=input_components_process,
                outputs=output_image,
                show_progress=False
            )

    return app

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