File size: 3,579 Bytes
b39b584
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
135
136
137
138
139
140
141
142
143
"""Example of using custom configurations with PANDORA."""

from PIL import Image

from src.pandora_removal import PandoraRemoval, PandoraConfig


def example_high_quality():
    """Use higher quality settings for better results (slower)."""

    config = PandoraConfig(
        model_path="stabilityai/stable-diffusion-2-1",
        device="cuda",
        max_steps=100,  # More steps for better quality
        guidance_scale_ladg=10.0,  # Higher guidance
        percentile=95.0,  # Stricter attention control
    )

    model = PandoraRemoval(config=config)
    model.load_model()

    result = model.remove_object(
        image="input.jpg",
        mask="mask.png",
        border_size=20,  # Larger border
        num_steps=100
    )

    result.save("output_high_quality.png")
    print("βœ“ High-quality result saved!")


def example_fast_inference():
    """Use faster settings for quick results (lower quality)."""

    config = PandoraConfig(
        model_path="stabilityai/stable-diffusion-2-1",
        device="cuda",
        max_steps=25,  # Fewer steps for speed
        guidance_scale_ladg=5.0,  # Lower guidance
    )

    model = PandoraRemoval(config=config)
    model.load_model()

    result = model.remove_object(
        image="input.jpg",
        mask="mask.png",
        border_size=10,  # Smaller border
        num_steps=25
    )

    result.save("output_fast.png")
    print("βœ“ Fast result saved!")


def example_different_object_types():
    """Different settings for different object types."""

    model = PandoraRemoval()
    model.load_model()

    # Small, well-defined objects (e.g., text, logos)
    result1 = model.remove_object(
        image="image_with_text.jpg",
        mask="text_mask.png",
        border_size=2,  # Small border for crisp edges
        guidance_scale=10.0
    )
    result1.save("output_text_removed.png")

    # Large, complex objects (e.g., people, buildings)
    result2 = model.remove_object(
        image="image_with_person.jpg",
        mask="person_mask.png",
        border_size=22,  # Large border for smooth blending
        guidance_scale=7.5
    )
    result2.save("output_person_removed.png")

    # Medium objects (default settings)
    result3 = model.remove_object(
        image="image_with_object.jpg",
        mask="object_mask.png",
        border_size=17,  # Standard border
        guidance_scale=7.5
    )
    result3.save("output_object_removed.png")

    print("βœ“ All results saved!")


def example_cpu_inference():
    """Run inference on CPU (no GPU required)."""

    config = PandoraConfig(
        model_path="stabilityai/stable-diffusion-2-1",
        device="cpu",  # Use CPU
        max_steps=50,
    )

    model = PandoraRemoval(config=config)
    model.load_model()

    result = model.remove_object(
        image="input.jpg",
        mask="mask.png",
        border_size=17
    )

    result.save("output_cpu.png")
    print("βœ“ CPU inference result saved!")


def main():
    """Run all examples."""
    print("Choose an example to run:")
    print("1. High-quality inference")
    print("2. Fast inference")
    print("3. Different object types")
    print("4. CPU inference")

    choice = input("\nEnter choice (1-4): ")

    examples = {
        "1": example_high_quality,
        "2": example_fast_inference,
        "3": example_different_object_types,
        "4": example_cpu_inference,
    }

    example_func = examples.get(choice)
    if example_func:
        example_func()
    else:
        print("Invalid choice!")


if __name__ == "__main__":
    main()