Width.ai

Introduction to the Clothseg Repository for Clothing Segmentation

Matt Payne
·
August 6, 2023

The Clothseg repository is a powerful tool designed for image processing tasks such as clothing segmentation, background removal, and background masking. Built as a wrapper of rem_bg for auto1111's stable diffusion gui, it offers a seamless way to manipulate images, particularly in the context of clothing. Whether you're working on a fashion-related project or simply need to segment different parts of an outfit from an image, Clothseg is an excellent tool to consider.

Features and Functionalities of Clothseg

clothing segmentation examples
From the github repo

One of the key features of the Clothseg repository is its ability to perform clothing segmentation. This involves separating different parts of clothing from an image, such as the top, bottom, and combined. This feature is especially useful in fashion-related projects where you need to identify and isolate specific clothing items from an image.

In addition to clothing segmentation, Clothseg also offers background removal and masking capabilities. Background removal is the process of eliminating the background from an image, leaving only the object of interest. On the other hand, background masking involves creating a mask that separates the object of interest from its background. These features are particularly useful in image editing and manipulation tasks.

Works with background removal as well!
Works with background removal as well!

The README provides several examples that demonstrate these features. For instance, there are images showing the original photo, the clothing mask, the result of background removal, and the final image after background masking. There's even an example showing how the repository can handle complex images such as castles.

Installation and Usage of Clothseg Repository

Installing the Clothseg repository is a straightforward process. According to the README, it can be installed from the extensions tab. However, you might need to separately install rem_bg using the command `pip install rem_bg`. For is-net, you may need to update rem_bg using the command `pip install -U rem_bg`. Using the Clothseg repository involves selecting the "remove_bg vX.X.X" script from the script selection box in either txt2img or img2img. The masking button will give a black/white output which is very useful for inpainting.

example of remove bg in the UI

The repository uses several models, including `u2net_cloth_seg`, `u2net`, `u2netp`, `silueta`, and `u2net_human_segmentation`. These models output images with the background removed or perform clothing segmentation. The `u2net_human_segmentation` model is optimized for keeping humans and tends to keep legs, which are sometimes cut by the previous models.

Useful Extensions

The Clothseg repository can be enhanced with the use of certain extensions. One such extension is the High Resolution Depth Maps for Stable Diffusion WebUI. This program is an addon for AUTOMATIC1111's Stable Diffusion WebUI that creates depth maps. It can generate 3D stereo image pairs, normal maps, and 3D meshes. The outputs of the script can be viewed directly or used as an asset for a 3D engine. This extension integrates with Rembg and supports batch processing, processing of videos, and can also be run in standalone mode, without Stable Diffusion WebUI.

Understanding the Python Script

The python script in the Clothseg repository offers a detailed look into the functionality of the repository. The script is organized into classes and methods that handle different aspects of the repository's functionality.

The primary class in the script is `Script`, which encapsulates the core functionality of the repository. It includes methods for specifying the title, determining whether the script should be shown, defining the user interface, and running the script.

The `run` method is particularly important as it handles the main functionality of the script. It processes the images, unloads the model, initializes the torch device, starts a new session, and performs the background removal or clothing segmentation. It also handles any errors that might occur during the execution of the script.

The script also interacts with different models used in the repository. For instance, the `u2net_cloth_seg` model outputs three images for the clothing categories top, bottom, and combined. The script handles these outputs and processes them as per the user's requirements.

Error handling is another crucial aspect of the script. The script is designed to handle out-of-memory errors during the execution of the `run` method. This ensures that the script can handle large images or a large number of images without crashing.

Finally, the script uses several parameters in its functions, such as `model_type`, `convert_to_mask`, `output_top`, `output_bottom`, and `output_combined`. These parameters can be tweaked to achieve different outcomes, providing flexibility to the users. For instance, the `convert_to_mask` parameter can be set to True to get a black/white output, which is useful for inpainting. Its well-organized structure, error handling capabilities, and flexible parameters make it a valuable tool for anyone interested in these tasks.

Updates and Acknowledgements

The Clothseg repository is continuously updated to improve its functionality and fix any issues. The most recent update, as mentioned in the README, includes examples and README updates. The repository also acknowledges the sources and references used in its development. Some code was used from Thygate's stable-diffusion-webui-depthmap-script, and several other resources were used, including rem_bg by danielgatis, U-2-Net by NathanUA, pymatting, and DIS by xuebinqin.

Looking to build a clothing segmentation pipeline?

Chat with a computer vision expert today at Width.ai on how you can implement this specific pipeline. Contact us today!

width.ai logo for computer vision