File size: 13,484 Bytes
492772b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
"""
Binary Image Segmentation Tool
A lightweight, professional implementation for foreground object segmentation.

Supports multiple models:
- U2NETP (fastest, 1.1M params)
- BiRefNet (best accuracy, larger model)
- RMBG (good balance)
"""

import os
import logging
from pathlib import Path
from typing import Literal, Tuple, Optional
import numpy as np
import torch
from PIL import Image
from torchvision import transforms
import cv2

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Device configuration
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
logger.info(f"Using device: {DEVICE}")


class U2NETP(torch.nn.Module):
    """U2-Net Portrait (U2NETP) - Lightweight segmentation model"""
    
    def __init__(self, in_ch=3, out_ch=1):
        super(U2NETP, self).__init__()
        
        # Encoder
        self.stage1 = self._make_stage(in_ch, 16, 64)
        self.pool12 = torch.nn.MaxPool2d(2, stride=2, ceil_mode=True)
        
        self.stage2 = self._make_stage(64, 16, 64)
        self.pool23 = torch.nn.MaxPool2d(2, stride=2, ceil_mode=True)
        
        self.stage3 = self._make_stage(64, 16, 64)
        self.pool34 = torch.nn.MaxPool2d(2, stride=2, ceil_mode=True)
        
        self.stage4 = self._make_stage(64, 16, 64)
        
        # Bridge
        self.stage5 = self._make_stage(64, 16, 64)
        
        # Decoder
        self.stage4d = self._make_stage(128, 16, 64)
        self.stage3d = self._make_stage(128, 16, 64)
        self.stage2d = self._make_stage(128, 16, 64)
        self.stage1d = self._make_stage(128, 16, 64)
        
        # Side outputs
        self.side1 = torch.nn.Conv2d(64, out_ch, 3, padding=1)
        self.side2 = torch.nn.Conv2d(64, out_ch, 3, padding=1)
        self.side3 = torch.nn.Conv2d(64, out_ch, 3, padding=1)
        self.side4 = torch.nn.Conv2d(64, out_ch, 3, padding=1)
        self.side5 = torch.nn.Conv2d(64, out_ch, 3, padding=1)
        
        # Output fusion
        self.outconv = torch.nn.Conv2d(5 * out_ch, out_ch, 1)
    
    def _make_stage(self, in_ch, mid_ch, out_ch):
        return torch.nn.Sequential(
            torch.nn.Conv2d(in_ch, mid_ch, 3, padding=1),
            torch.nn.ReLU(inplace=True),
            torch.nn.Conv2d(mid_ch, mid_ch, 3, padding=1),
            torch.nn.ReLU(inplace=True),
            torch.nn.Conv2d(mid_ch, out_ch, 3, padding=1),
            torch.nn.ReLU(inplace=True)
        )
    
    def forward(self, x):
        hx = x
        
        # Encoder
        hx1 = self.stage1(hx)
        hx = self.pool12(hx1)
        
        hx2 = self.stage2(hx)
        hx = self.pool23(hx2)
        
        hx3 = self.stage3(hx)
        hx = self.pool34(hx3)
        
        hx4 = self.stage4(hx)
        hx5 = self.stage5(hx4)
        
        # Decoder
        hx4d = self.stage4d(torch.cat((hx5, hx4), 1))
        hx4dup = torch.nn.functional.interpolate(hx4d, scale_factor=2, mode='bilinear', align_corners=True)
        
        hx3d = self.stage3d(torch.cat((hx4dup, hx3), 1))
        hx3dup = torch.nn.functional.interpolate(hx3d, scale_factor=2, mode='bilinear', align_corners=True)
        
        hx2d = self.stage2d(torch.cat((hx3dup, hx2), 1))
        hx2dup = torch.nn.functional.interpolate(hx2d, scale_factor=2, mode='bilinear', align_corners=True)
        
        hx1d = self.stage1d(torch.cat((hx2dup, hx1), 1))
        
        # Side outputs
        d1 = self.side1(hx1d)
        d2 = torch.nn.functional.interpolate(self.side2(hx2d), size=d1.shape[2:], mode='bilinear', align_corners=True)
        d3 = torch.nn.functional.interpolate(self.side3(hx3d), size=d1.shape[2:], mode='bilinear', align_corners=True)
        d4 = torch.nn.functional.interpolate(self.side4(hx4d), size=d1.shape[2:], mode='bilinear', align_corners=True)
        d5 = torch.nn.functional.interpolate(self.side5(hx5), size=d1.shape[2:], mode='bilinear', align_corners=True)
        
        # Fusion
        d0 = self.outconv(torch.cat((d1, d2, d3, d4, d5), 1))
        
        return torch.sigmoid(d0), torch.sigmoid(d1), torch.sigmoid(d2), torch.sigmoid(d3), torch.sigmoid(d4), torch.sigmoid(d5)


class BinarySegmenter:
    """
    Professional binary segmentation tool with multiple model backends.
    
    Args:
        model_type: Choice of segmentation model
        cache_dir: Directory to cache downloaded models
    """
    
    def __init__(
        self,
        model_type: Literal["u2netp", "birefnet", "rmbg"] = "u2netp",
        cache_dir: str = "./.model_cache"
    ):
        self.model_type = model_type
        self.cache_dir = Path(cache_dir)
        self.cache_dir.mkdir(exist_ok=True)
        
        self.model = None
        self.transform = None
        self._load_model()
    
    def _load_model(self):
        """Load the specified segmentation model"""
        logger.info(f"Loading {self.model_type} model...")
        
        if self.model_type == "u2netp":
            self._load_u2netp()
        elif self.model_type == "birefnet":
            self._load_birefnet()
        elif self.model_type == "rmbg":
            self._load_rmbg()
        else:
            raise ValueError(f"Unknown model type: {self.model_type}")
        
        self.model.to(DEVICE)
        self.model.eval()
        logger.info(f"{self.model_type} loaded successfully")
    
    def _load_u2netp(self):
        """Load U2NETP model (1.1M parameters, fastest)"""
        self.model = U2NETP(3, 1)
        
        # Try to load pretrained weights
        model_path = self.cache_dir / "u2netp.pth"
        
        if model_path.exists():
            logger.info(f"Loading weights from {model_path}")
            self.model.load_state_dict(
                torch.load(model_path, map_location=DEVICE)
            )
        else:
            logger.warning(f"No pretrained weights found at {model_path}")
            logger.warning("Download from: https://github.com/xuebinqin/U-2-Net")
        
        # Standard ImageNet normalization
        self.transform = transforms.Compose([
            transforms.Resize((320, 320)),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ])
    
    def _load_birefnet(self):
        """Load BiRefNet model (best accuracy, larger)"""
        try:
            from transformers import AutoModelForImageSegmentation
            
            self.model = AutoModelForImageSegmentation.from_pretrained(
                'ZhengPeng7/BiRefNet',
                trust_remote_code=True,
                cache_dir=str(self.cache_dir)
            )
            
            self.transform = transforms.Compose([
                transforms.Resize((1024, 1024)),
                transforms.ToTensor(),
                transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
            ])
        except ImportError:
            raise ImportError("BiRefNet requires: pip install transformers")
    
    def _load_rmbg(self):
        """Load RMBG model (good balance)"""
        try:
            from transformers import AutoModelForImageSegmentation
            
            self.model = AutoModelForImageSegmentation.from_pretrained(
                'briaai/RMBG-1.4',
                trust_remote_code=True,
                cache_dir=str(self.cache_dir)
            )
            
            self.transform = transforms.Compose([
                transforms.Resize((1024, 1024)),
                transforms.ToTensor(),
                transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
            ])
        except ImportError:
            raise ImportError("RMBG requires: pip install transformers")
    
    def segment(
        self,
        image: np.ndarray,
        threshold: float = 0.5,
        return_type: Literal["mask", "rgba", "both"] = "mask"
    ) -> Tuple[Optional[np.ndarray], Optional[Image.Image]]:
        """
        Segment foreground object from image.
        
        Args:
            image: Input image as numpy array (H, W, 3) in RGB or BGR
            threshold: Threshold for binary mask (0-1)
            return_type: What to return - "mask", "rgba", or "both"
        
        Returns:
            Tuple of (binary_mask, rgba_image) based on return_type
        """
        # Convert BGR to RGB if needed
        if len(image.shape) == 3 and image.shape[2] == 3:
            if image[0, 0, 0] != image[0, 0, 2]:  # Simple heuristic
                image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            else:
                image_rgb = image
        else:
            raise ValueError("Input must be a color image (H, W, 3)")
        
        # Convert to PIL
        image_pil = Image.fromarray(image_rgb)
        original_size = image_pil.size
        
        # Transform
        input_tensor = self.transform(image_pil).unsqueeze(0).to(DEVICE)
        
        # Inference
        with torch.no_grad():
            if self.model_type == "u2netp":
                outputs = self.model(input_tensor)
                pred = outputs[0]  # Main output
            else:  # birefnet or rmbg
                pred = self.model(input_tensor)[-1].sigmoid()
        
        # Post-process
        pred = pred.squeeze().cpu().numpy()
        
        # Resize to original
        pred_resized = cv2.resize(pred, original_size, interpolation=cv2.INTER_LINEAR)
        
        # Normalize to 0-255
        pred_normalized = ((pred_resized - pred_resized.min()) / 
                          (pred_resized.max() - pred_resized.min() + 1e-8) * 255)
        
        # Create binary mask
        binary_mask = (pred_normalized > (threshold * 255)).astype(np.uint8) * 255
        
        # Optional: Morphological operations for cleaner mask
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
        binary_mask = cv2.morphologyEx(binary_mask, cv2.MORPH_CLOSE, kernel)
        binary_mask = cv2.morphologyEx(binary_mask, cv2.MORPH_OPEN, kernel)
        
        # Create RGBA if needed
        rgba_image = None
        if return_type in ["rgba", "both"]:
            # Create 4-channel image
            rgba = np.dstack([image_rgb, binary_mask])
            rgba_image = Image.fromarray(rgba, mode='RGBA')
        
        # Return based on type
        if return_type == "mask":
            return binary_mask, None
        elif return_type == "rgba":
            return None, rgba_image
        else:  # both
            return binary_mask, rgba_image
    
    def batch_segment(
        self,
        images: list[np.ndarray],
        threshold: float = 0.5,
        return_type: Literal["mask", "rgba", "both"] = "mask"
    ) -> list:
        """
        Segment multiple images in batch.
        
        Args:
            images: List of input images
            threshold: Threshold for binary masks
            return_type: What to return for each image
        
        Returns:
            List of segmentation results
        """
        results = []
        for i, img in enumerate(images):
            logger.info(f"Processing image {i+1}/{len(images)}")
            result = self.segment(img, threshold, return_type)
            results.append(result)
        return results


def segment_image_file(
    input_path: str,
    output_path: str,
    model_type: str = "u2netp",
    threshold: float = 0.5,
    save_rgba: bool = True
):
    """
    Convenience function to segment an image file.
    
    Args:
        input_path: Path to input image
        output_path: Path to save output (mask or RGBA)
        model_type: Model to use
        threshold: Segmentation threshold
        save_rgba: If True, save RGBA; if False, save binary mask
    """
    # Load image
    image = cv2.imread(input_path)
    if image is None:
        raise FileNotFoundError(f"Could not load image: {input_path}")
    
    # Create segmenter
    segmenter = BinarySegmenter(model_type=model_type)
    
    # Segment
    return_type = "rgba" if save_rgba else "mask"
    mask, rgba = segmenter.segment(image, threshold, return_type)
    
    # Save
    output_path = Path(output_path)
    output_path.parent.mkdir(parents=True, exist_ok=True)
    
    if save_rgba and rgba is not None:
        rgba.save(output_path)
        logger.info(f"Saved RGBA to: {output_path}")
    elif mask is not None:
        cv2.imwrite(str(output_path), mask)
        logger.info(f"Saved mask to: {output_path}")
    
    return str(output_path)


# Example usage
if __name__ == "__main__":
    import argparse
    
    parser = argparse.ArgumentParser(description="Binary image segmentation")
    parser.add_argument("input", help="Input image path")
    parser.add_argument("output", help="Output path")
    parser.add_argument(
        "--model",
        choices=["u2netp", "birefnet", "rmbg"],
        default="u2netp",
        help="Segmentation model"
    )
    parser.add_argument(
        "--threshold",
        type=float,
        default=0.5,
        help="Segmentation threshold (0-1)"
    )
    parser.add_argument(
        "--format",
        choices=["mask", "rgba"],
        default="rgba",
        help="Output format"
    )
    
    args = parser.parse_args()
    
    # Process
    segment_image_file(
        args.input,
        args.output,
        model_type=args.model,
        threshold=args.threshold,
        save_rgba=(args.format == "rgba")
    )