Skip to main content

An easy-to-use library for skin tone classification

Project description

Skin Tone Classifier (stone)

PyPI - Python Version PyPI GitHub release (latest by date including pre-releases) License: GPL v3

An easy-to-use library for skin tone classification.

This can be used to detect face or skin area in the specified images. The detected skin tones are then classified into the specified color categories. The library finally generates results to report the detected faces (if any), dominant skin tones and color category.


Citation

If you are interested in our work, please cite:

@article{https://doi.org/10.1111/ssqu.13242,
author = {Rej\'{o}n Pi\tilde{n}a, Ren\'{e} Alejandro and Ma, Chenglong},
title = {Classification Algorithm for Skin Color (CASCo): A new tool to measure skin color in social science research},
journal = {Social Science Quarterly},
volume = {n/a},
number = {n/a},
pages = {},
keywords = {colorism, measurement, photo elicitation, racism, skin color, spectrometers},
doi = {https://doi.org/10.1111/ssqu.13242},
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/ssqu.13242},
eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/ssqu.13242},
abstract = {Abstract Objective A growing body of literature reveals that skin color has significant effects on people's income, health, education, and employment. However, the ways in which skin color has been measured in empirical research have been criticized for being inaccurate, if not subjective and biased. Objective Introduce an objective, automatic, accessible and customizable Classification Algorithm for Skin Color (CASCo). Methods We review the methods traditionally used to measure skin color (verbal scales, visual aids or color palettes, photo elicitation, spectrometers and image-based algorithms), noting their shortcomings. We highlight the need for a different tool to measure skin color Results We present CASCo, a (social researcher-friendly) Python library that uses face detection, skin segmentation and k-means clustering algorithms to determine the skin tone category of portraits. Conclusion After assessing the merits and shortcomings of all the methods available, we argue CASCo is well equipped to overcome most challenges and objections posed against its alternatives. While acknowledging its limitations, we contend that CASCo should complement researchers. toolkit in this area.}
}

Installation

To install SkinToneClassifier:

pip install skin-tone-classifier

HOW TO USE

Quick Start

Given the famous photo of Lenna, to detect her skin tone,

Lenna picture
just run:
stone -i /path/to/lenna.jpg --debug

Then, you can find the processed image in ./debug folder, e.g.,

processed Lenna picture

In this image, from left to right you can find the following information:

  1. detected face enclosed by a rectangle.
  2. dominant colors.
    1. The number of colors depends on settings (default is 2) and their sizes depend on their proportion.
  3. specified color categories and the target label is enclosed by a rectangle.
  4. you can find a summary text at the bottom.

Furthermore, there will be a report file named result.csv which contains more detailed information, e.g.,

file face_location dominant_1 props_1 dominant_2 props_2 category PERLA distance(0-100)
lena_std 84:153 #CB6268 0.51 #E1A299 0.49 #e7c1b8 I 17.37

Detailed Usage

To see the usage and parameters, run:

stone -h

Output in console:

usage: stone [-h] [-i IMAGE FILENAME [IMAGE FILENAME ...]]
               [-c COLOR [COLOR ...]] [-d] [-o DIRECTORY] [--n_colors N]
               [--new_width WIDTH] [--scale SCALE] [--min_nbrs NEIGHBORS]
               [--min_size WIDTH [HEIGHT ...]]

Skin Tone Classifier

optional arguments:
  -h, --help            show this help message and exit
  -i IMAGE FILENAME [IMAGE FILENAME ...], --images IMAGE FILENAME [IMAGE FILENAME ...]
                        Image filename(s) to process;
                        supports multiple values separated by space, e.g., "a.jpg b.png";
                        supports directory or file name(s), e.g., "./path/to/images/ a.jpg";
                        The app will search all images in the directory of this script in default.
  -c COLOR [COLOR ...], --categories COLOR [COLOR ...]
                        Skin tone categories; supports RGB hex value leading by # or RGB values separated by comma(,).
  -d, --debug           Whether to output processed images, used for debugging and verification.
  -o DIRECTORY, --output DIRECTORY
                        The path of output file, defaults to the directory of this script.
  --n_colors N          CONFIG: the number of dominant colors to be extracted, defaults to 2.
  --new_width WIDTH     CONFIG: resize the images with the specified width. Negative value will be ignored, defaults to 250.
  --scale SCALE         CONFIG: how much the image size is reduced at each image scale, defaults to 1.1
  --min_nbrs NEIGHBORS  CONFIG: how many neighbors each candidate rectangle should have to retain it.
                                Higher value results in less detections but with higher quality, defaults to 5.
  --min_size WIDTH [HEIGHT ...]
                        CONFIG: minimum possible face size. Faces smaller than that are ignored, defaults to "30 30".

Use Cases

1. To process multiple images

1.1 Multiple filenames

stone -i (or --images) a.jpg b.png

1.2 Images in some folder(s)

stone -i ./path/to/images/

NB: Supported image formats: .jpg, .gif, .png, .jpeg, .webp, .tif.

In default (i.e., stone without -i option), the app will search images in current folder.

2. To specify color categories

2.1 Use hex values

stone -c (or --categories) #373028 #422811 #fbf2f3

NB: Values start with '#'.

2.2 Use RGB tuple values

stone -c 55,48,40 66,40,17 251,242,243

NB: Values split by comma ',', multiple values are still separated by space.

3. Specify output folder

The app puts the final report (result.csv) in current folder in default.

To change the output folder:

stone -o (or --output) ./path/to/output/

The output folder will be created if it does not exist.

In result.csv, each row is showing the color information of each detected face. If more than one faces are detected, there will be multiple rows for that image.

4. Store processed image for debugging

stone -d (or --debug)

This option will store the processed image (like the Lenna example above) in ./path/to/output/debug/faces_<n> folder, where <n> is the number of faces detected in the image.

By default, to save space, the app does not store processed images.

Like in the result.csv file, there will be more than one processed images if 2 or more faces were detected.

5. Tune parameters of face detection

The rest parameters of CONFIG are used to detect face. Please refer to https://stackoverflow.com/a/20805153/8860079 for detailed information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

skin-tone-classifier-0.1.12.tar.gz (22.0 kB view hashes)

Uploaded Source

Built Distribution

skin_tone_classifier-0.1.12-py3-none-any.whl (22.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page