File size: 8,965 Bytes
436df5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""
Example usage of WearIT Garment Mask Pipeline
"""
import os
from pathlib import Path
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

# Option 1: Using Hugging Face transformers (if available)
try:
    from transformers import pipeline
    USE_HF_PIPELINE = True
except ImportError:
    USE_HF_PIPELINE = False
    print("Transformers not available, using direct import")

# Option 2: Direct import (always works)
from pipeline import GarmentMaskPipeline


def visualize_results(results, save_dir="./visualization"):
    """
    Visualize and save the mask generation results.

    Args:
        results: List of result dictionaries from the pipeline
        save_dir: Directory to save visualizations
    """
    save_path = Path(save_dir)
    save_path.mkdir(parents=True, exist_ok=True)

    for result in results:
        image_id = result["image_id"]
        image = result["image_standardized"]
        masks = result["masks"]

        # Create a figure for each image
        n_masks = len(masks)
        fig, axes = plt.subplots(1, n_masks + 1, figsize=(5 * (n_masks + 1), 5))

        if n_masks == 1:
            axes = [axes] if not isinstance(axes, np.ndarray) else axes

        # Show original image
        axes[0].imshow(image)
        axes[0].set_title(f"Original\n{image_id}")
        axes[0].axis("off")

        # Show masks
        for idx, (garment_type, mask_data) in enumerate(masks.items(), 1):
            mask = mask_data["person_mask"]

            # Overlay mask on image
            img_array = np.array(image)
            mask_array = np.array(mask)

            # Create colored overlay
            overlay = img_array.copy()
            overlay[mask_array > 127] = [255, 0, 0]  # Red for mask

            # Blend
            blended = (0.6 * img_array + 0.4 * overlay).astype(np.uint8)

            axes[idx].imshow(blended)
            axes[idx].set_title(f"{garment_type.capitalize()} Mask")
            axes[idx].axis("off")

            # Save individual mask
            mask.save(save_path / f"{image_id}_{garment_type}_mask.png")

        plt.tight_layout()
        plt.savefig(save_path / f"{image_id}_visualization.png", dpi=150, bbox_inches="tight")
        plt.close()

        # Save standardized image
        image.save(save_path / f"{image_id}_standardized.png")

    print(f"✓ Visualizations saved to {save_dir}")


def example_1_basic_usage():
    """Example 1: Basic single image processing"""
    print("\n" + "="*60)
    print("Example 1: Basic Usage - Single Image")
    print("="*60)

    if USE_HF_PIPELINE:
        # Using Hugging Face pipeline
        pipe = pipeline(
            "image-segmentation",
            model=".",  # Local directory
            trust_remote_code=True,
            device="cuda:0"
        )
    else:
        # Direct instantiation
        pipe = GarmentMaskPipeline(
            device="cuda:0",
            densepose_ckpt="chkpt/DensePose",
            schp_atr_ckpt="chkpt/SCHP/exp-schp-201908301523-atr.pth",
            schp_lip_ckpt="chkpt/SCHP/exp-schp-201908261155-lip.pth"
        )

    # Process single image
    results = pipe(
        "path/to/your/image.jpg",
        garment_types="upper"
    )

    # Access results
    result = results[0]
    upper_mask = result["masks"]["upper"]["person_mask"]
    upper_mask.save("output_upper_mask.png")

    print(f"✓ Generated mask for image: {result['image_id']}")
    print(f"  - Standardized image size: {result['image_standardized'].size}")
    print(f"  - Mask size: {upper_mask.size}")

    return results


def example_2_multiple_garments():
    """Example 2: Generate multiple garment masks per image"""
    print("\n" + "="*60)
    print("Example 2: Multiple Garment Types")
    print("="*60)

    pipe = GarmentMaskPipeline(device="cuda:0")

    # Generate masks for upper, lower, and full body
    results = pipe(
        "path/to/your/image.jpg",
        garment_types=["upper", "lower", "dress"]
    )

    result = results[0]
    print(f"✓ Generated {len(result['masks'])} masks:")
    for garment_type in result['masks'].keys():
        print(f"  - {garment_type}")

    return results


def example_3_batch_processing():
    """Example 3: Batch processing multiple images"""
    print("\n" + "="*60)
    print("Example 3: Batch Processing")
    print("="*60)

    pipe = GarmentMaskPipeline(
        device="cuda:0",
        save_images=True
    )

    # Process multiple images at once
    image_paths = [
        "path/to/image1.jpg",
        "path/to/image2.jpg",
        "path/to/image3.jpg"
    ]

    results = pipe(
        image_paths,
        garment_types=["upper", "lower"],
        image_ids=["person_001", "person_002", "person_003"],
        output_dir="./batch_output"
    )

    print(f"✓ Processed {len(results)} images")
    for result in results:
        print(f"  - {result['image_id']}: {list(result['masks'].keys())}")

    return results


def example_4_custom_configuration():
    """Example 4: Custom configuration"""
    print("\n" + "="*60)
    print("Example 4: Custom Configuration")
    print("="*60)

    # Create pipeline with custom settings
    pipe = GarmentMaskPipeline(
        device="cuda:0",
        output_height=2048,  # Higher resolution output
        process_size=768,     # Larger processing size
        use_convex_hull=False,  # Disable convex hull
        allowed_strategies=["ellipse", "box"],  # Only use ellipse and box
        save_images=True
    )

    results = pipe(
        "path/to/your/image.jpg",
        garment_types="upper",
        output_dir="./custom_output",
        save_mask_s=True,  # Save tight mask before expansion
        save_strong_protect=True  # Save protected areas
    )

    print("✓ Custom configuration applied:")
    print(f"  - Output height: 2048px")
    print(f"  - Allowed strategies: ellipse, box")
    print(f"  - Saved intermediate results to ./custom_output")

    return results


def example_5_pil_images():
    """Example 5: Using PIL Images instead of paths"""
    print("\n" + "="*60)
    print("Example 5: Using PIL Images")
    print("="*60)

    pipe = GarmentMaskPipeline(device="cuda:0")

    # Load images as PIL
    image1 = Image.open("path/to/image1.jpg")
    image2 = Image.open("path/to/image2.jpg")

    # Process PIL images
    results = pipe(
        [image1, image2],
        garment_types="upper"
    )

    print(f"✓ Processed {len(results)} PIL images")

    return results


def example_6_integration_with_inpainting():
    """Example 6: Integration with image inpainting pipeline"""
    print("\n" + "="*60)
    print("Example 6: Integration with Inpainting")
    print("="*60)

    # Generate mask
    pipe = GarmentMaskPipeline(device="cuda:0")
    results = pipe("path/to/your/image.jpg", garment_types="upper")

    result = results[0]
    image = result["image_standardized"]
    mask = result["masks"]["upper"]["person_mask"]

    # Now use with an inpainting model (example with stable diffusion)
    try:
        from diffusers import StableDiffusionInpaintPipeline

        inpaint_pipe = StableDiffusionInpaintPipeline.from_pretrained(
            "stabilityai/stable-diffusion-2-inpainting"
        ).to("cuda")

        # Inpaint the masked region
        inpainted = inpaint_pipe(
            prompt="a stylish red dress",
            image=image,
            mask_image=mask,
            num_inference_steps=50
        ).images[0]

        inpainted.save("inpainted_result.png")
        print("✓ Inpainting completed")

    except ImportError:
        print("⚠ Diffusers not installed, skipping inpainting demo")
        print("  Install with: pip install diffusers")

    return results


def main():
    """Run all examples"""
    print("\n" + "="*60)
    print("WearIT Garment Mask Pipeline - Examples")
    print("="*60)

    # Check if example images exist
    if not os.path.exists("path/to/your/image.jpg"):
        print("\n⚠ WARNING: Please update image paths in the examples!")
        print("  Replace 'path/to/your/image.jpg' with actual image paths.\n")
        return

    # Run examples (uncomment the ones you want to try)

    # results = example_1_basic_usage()
    # visualize_results(results, "./example1_output")

    # results = example_2_multiple_garments()
    # visualize_results(results, "./example2_output")

    # results = example_3_batch_processing()

    # results = example_4_custom_configuration()

    # results = example_5_pil_images()

    # results = example_6_integration_with_inpainting()

    print("\n" + "="*60)
    print("Examples completed!")
    print("="*60 + "\n")


if __name__ == "__main__":
    # Quick test without running all examples
    print("WearIT Garment Mask Pipeline - Example Usage Script")
    print("\nTo run examples, uncomment the desired example in main() function")
    print("and update the image paths.\n")

    # Uncomment to run all examples:
    # main()