Skin Tone Detector for ComfyUI: A Deep Dive
The Skin Tone Detector for ComfyUI is a powerful node that analyzes images to detect and classify skin tones according to the standard emoji skin tone palette. Developed by Kevin McMahon, this tool is a unique addition to the ComfyUI framework, combining computer vision and color science to provide insightful image analysis. Below, we delve into its functionality, implementation, and potential use cases.
Key Features
- Skin Tone Detection:
- The node identifies skin tones in an image and maps them to the emoji skin tone categories: Light 🏻, Medium-Light 🏼, Medium 🏽, Medium-Dark 🏾, and Dark 🏿.
- If no skin tone is detected, it returns “NOT_DETECTED.”
- Color Space Conversion:
- Uses the LAB color space, which is ideal for perceptual color differences, to match detected skin tones to reference values.
- Face Detection:
- Employs MediaPipe for facial detection, ensuring accurate identification of facial regions for skin tone analysis.
- Shadow Handling and Normalization:
- Implements preprocessing techniques like illumination normalization and shadow exclusion to enhance accuracy in various lighting conditions.
- Output Integration:
- Designed as a ComfyUI node with a simple input/output structure, making it easy to integrate into workflows.
Implementation Details
Dependencies
The node leverages a variety of libraries:
- OpenCV: For image preprocessing and color space conversion.
- MediaPipe: For robust facial detection.
- NumPy and scikit-image: For numerical computations and image processing.
- PyTorch: For handling image data tensors within the ComfyUI pipeline.
Core Class: SkinToneDetector
The SkinToneDetector class encapsulates the node’s functionality. Below are the primary components:
1. Initialization
The constructor initializes the LAB reference values for emoji skin tones and sets up MediaPipe’s face detection model:
self.SKIN_TONES = {...}
self.REFERENCE_TONES_LAB = {...}
self.face_detection = self.mp_face_detection.FaceDetection(
model_selection=1,
min_detection_confidence=0.5
)
2. Face Detection and Region Extraction
The _get_primary_detection method identifies the most prominent face based on detection scores, while _extract_face_region crops the corresponding face region for further analysis.
3. Image Preprocessing
Key preprocessing steps include:
- Illumination Normalization: Adjusts lighting conditions using Contrast Limited Adaptive Histogram Equalization (CLAHE).
- Shadow Exclusion: Excludes shadowed areas based on the L* channel of LAB color space.
4. Skin Tone Classification
After preprocessing, the tool converts skin pixel data into the LAB color space. It calculates the Euclidean distance between the median LAB values of detected skin pixels and the reference tones to determine the closest match:
closest_tone = min(
self.REFERENCE_TONES_LAB.items(),
key=lambda x: self.calculate_lab_distance(avg_lab, x[1])
)[0]
Node Design for ComfyUI
Input and Output
The node accepts an input image and outputs the detected skin tone as a string:
- Input:
image(supports both Torch Tensors and PIL images). - Output: Detected skin tone (e.g., “MEDIUM_LIGHT”).
Node Configuration
The following configurations ensure seamless integration into ComfyUI:
def INPUT_TYPES(cls):
return {"required": {"image": ("IMAGE",)}}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("Skin Tone",)
FUNCTION = "detect_skin_tone"
CATEGORY = "image/analysis"
OUTPUT_NODE = True
Installation and Usage
Installation
The node can be installed by cloning the repository:
git clone https://github.com/kevinmcmahondev/comfyui-skin-tone-detector
Usage in ComfyUI
After installation, the node becomes available in the ComfyUI interface under the “image/analysis” category. Users can feed an image into the node and receive the corresponding skin tone as output.
Applications and Potential Enhancements
Applications
- Diversity Analysis: Analyzing diversity in image datasets.
- Skin Tone Matching: In AR/VR or beauty tech, for matching products to specific skin tones.
- Emoji Personalization: Enabling personalized emoji suggestions based on skin tone.
Potential Enhancements
- Real-Time Processing: Adapting the tool for real-time video analysis.
- Expanded Categories: Incorporating more nuanced skin tone classifications.
- Lighting Robustness: Further improving detection under extreme lighting conditions.
Conclusion
The Skin Tone Detector for ComfyUI is a sophisticated tool that showcases the intersection of computer vision and color science. Its thoughtful implementation and practical applications make it a valuable addition to the ComfyUI ecosystem. With potential for expansion and integration into diverse domains, this node exemplifies the versatility of open-source development in advancing image analysis capabilities.