Spaces:
Runtime error
Runtime error
Commit ·
9751db3
1
Parent(s): 6e81f52
added gradio UI, removed deepface, added ITA logic
Browse filesFiles changed (10)
- .gitignore+1
- =1.9.0Added+37
- requirements.txtDeleted-109
- src/analyze.py+48-42
- src/gradio_app.pyAdded+75
- src/image.py+64-59
- src/main.py+82-79
- src/segmentation_utils.py+165-29
- src/skin_analyzer.pyAdded+128
- src/skin_utils.py+13-100
4 unmodified lines5674 unmodified linesvenv/workspace/*.pyc4 unmodified lines56784 unmodified linesvenv/workspace/*.pyctemp.jpg
12345678910111213141516171819202122232425262728293031323334353637Requirement already satisfied: tensorflow in ./venv/lib/python3.12/site-packages (2.16.1)Requirement already satisfied: absl-py>=1.0.0 in ./venv/lib/python3.12/site-packages (from tensorflow) (2.1.0)Requirement already satisfied: astunparse>=1.6.0 in ./venv/lib/python3.12/site-packages (from tensorflow) (1.6.3)Requirement already satisfied: flatbuffers>=23.5.26 in ./venv/lib/python3.12/site-packages (from tensorflow) (24.3.25)Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in ./venv/lib/python3.12/site-packages (from tensorflow) (0.5.4)Requirement already satisfied: google-pasta>=0.1.1 in ./venv/lib/python3.12/site-packages (from tensorflow) (0.2.0)Requirement already satisfied: h5py>=3.10.0 in ./venv/lib/python3.12/site-packages (from tensorflow) (3.11.0)Requirement already satisfied: libclang>=13.0.0 in ./venv/lib/python3.12/site-packages (from tensorflow) (18.1.1)Requirement already satisfied: ml-dtypes~=0.3.1 in ./venv/lib/python3.12/site-packages (from tensorflow) (0.3.2)Requirement already satisfied: opt-einsum>=2.3.2 in ./venv/lib/python3.12/site-packages (from tensorflow) (3.3.0)Requirement already satisfied: packaging in ./venv/lib/python3.12/site-packages (from tensorflow) (24.0)Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in ./venv/lib/python3.12/site-packages (from tensorflow) (4.25.3)Requirement already satisfied: requests<3,>=2.21.0 in ./venv/lib/python3.12/site-packages (from tensorflow) (2.32.3)Requirement already satisfied: setuptools in ./venv/lib/python3.12/site-packages (from tensorflow) (70.0.0)Requirement already satisfied: six>=1.12.0 in ./venv/lib/python3.12/site-packages (from tensorflow) (1.16.0)Requirement already satisfied: termcolor>=1.1.0 in ./venv/lib/python3.12/site-packages (from tensorflow) (2.4.0)Requirement already satisfied: typing-extensions>=3.6.6 in ./venv/lib/python3.12/site-packages (from tensorflow) (4.12.1)Requirement already satisfied: wrapt>=1.11.0 in ./venv/lib/python3.12/site-packages (from tensorflow) (1.16.0)Requirement already satisfied: grpcio<2.0,>=1.24.3 in ./venv/lib/python3.12/site-packages (from tensorflow) (1.64.1)Requirement already satisfied: tensorboard<2.17,>=2.16 in ./venv/lib/python3.12/site-packages (from tensorflow) (2.16.2)Requirement already satisfied: keras>=3.0.0 in ./venv/lib/python3.12/site-packages (from tensorflow) (3.3.3)Requirement already satisfied: numpy<2.0.0,>=1.26.0 in ./venv/lib/python3.12/site-packages (from tensorflow) (1.26.4)Requirement already satisfied: wheel<1.0,>=0.23.0 in ./venv/lib/python3.12/site-packages (from astunparse>=1.6.0->tensorflow) (0.43.0)Requirement already satisfied: rich in ./venv/lib/python3.12/site-packages (from keras>=3.0.0->tensorflow) (13.7.1)Requirement already satisfied: namex in ./venv/lib/python3.12/site-packages (from keras>=3.0.0->tensorflow) (0.0.8)Requirement already satisfied: optree in ./venv/lib/python3.12/site-packages (from keras>=3.0.0->tensorflow) (0.11.0)Requirement already satisfied: charset-normalizer<4,>=2 in ./venv/lib/python3.12/site-packages (from requests<3,>=2.21.0->tensorflow) (3.3.2)Requirement already satisfied: idna<4,>=2.5 in ./venv/lib/python3.12/site-packages (from requests<3,>=2.21.0->tensorflow) (3.7)Requirement already satisfied: urllib3<3,>=1.21.1 in ./venv/lib/python3.12/site-packages (from requests<3,>=2.21.0->tensorflow) (2.2.1)Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.12/site-packages (from requests<3,>=2.21.0->tensorflow) (2024.6.2)Requirement already satisfied: markdown>=2.6.8 in ./venv/lib/python3.12/site-packages (from tensorboard<2.17,>=2.16->tensorflow) (3.6)Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in ./venv/lib/python3.12/site-packages (from tensorboard<2.17,>=2.16->tensorflow) (0.7.2)Requirement already satisfied: werkzeug>=1.0.1 in ./venv/lib/python3.12/site-packages (from tensorboard<2.17,>=2.16->tensorflow) (3.0.3)Requirement already satisfied: MarkupSafe>=2.1.1 in ./venv/lib/python3.12/site-packages (from werkzeug>=1.0.1->tensorboard<2.17,>=2.16->tensorflow) (2.1.5)Requirement already satisfied: markdown-it-py>=2.2.0 in ./venv/lib/python3.12/site-packages (from rich->keras>=3.0.0->tensorflow) (3.0.0)Requirement already satisfied: pygments<3.0.0,>=2.13.0 in ./venv/lib/python3.12/site-packages (from rich->keras>=3.0.0->tensorflow) (2.18.0)Requirement already satisfied: mdurl~=0.1 in ./venv/lib/python3.12/site-packages (from markdown-it-py>=2.2.0->rich->keras>=3.0.0->tensorflow) (0.1.2)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109absl-py==2.1.0asttokens==2.4.1astunparse==1.6.3attrs==23.2.0beautifulsoup4==4.12.3blinker==1.8.2certifi==2024.2.2cffi==1.16.0charset-normalizer==3.3.2click==8.1.7colorama==0.4.6colored==1.3.93colormath==3.0.0contourpy==1.2.1cycler==0.12.1decorator==5.1.1deepface==0.0.91exceptiongroup==1.2.1executing==2.0.1filelock==3.14.0fire==0.6.0Flask==3.0.3flatbuffers==24.3.25fonttools==4.51.0gast==0.5.4gdown==5.1.0Gooey==1.0.8.1google-pasta==0.2.0grpcio==1.63.0gunicorn==22.0.0h5py==3.11.0HEIC2PNG==1.1.4idna==3.7importlib_metadata==7.1.0importlib_resources==6.4.0ipython==8.18.1itsdangerous==2.2.0jax==0.4.28jaxlib==0.4.28jedi==0.19.1Jinja2==3.1.4joblib==1.4.2keras==3.3.3kiwisolver==1.4.5libclang==18.1.1Markdown==3.6markdown-it-py==3.0.0MarkupSafe==2.1.5matplotlib==3.8.4matplotlib-inline==0.1.7mdurl==0.1.2mediapipe==0.10.14ml-dtypes==0.3.2mtcnn==0.1.1namex==0.0.8networkx==3.2.1numpy==1.26.4opencv-contrib-python==4.9.0.80opencv-python==4.9.0.80opt-einsum==3.3.0optree==0.11.0packaging==24.0pandas==2.2.2parso==0.8.4pexpect==4.9.0pillow==10.3.0pillow_heif==0.16.0pngquant-cli==2.17.0.post5prompt-toolkit==3.0.43protobuf==4.25.3psutil==5.9.8ptyprocess==0.7.0pure-eval==0.2.2pycparser==2.22Pygments==2.18.0pygtrie==2.5.0pyparsing==3.1.2PySocks==1.7.1python-dateutil==2.9.0.post0pytz==2024.1re-wx==0.0.10requests==2.31.0retina-face==0.0.17rich==13.7.1scikit-learn==1.4.2scipy==1.13.0six==1.16.0# Editable install with no version control (skin-tone-classifier==1.2.4)-e /Users/hardikuppal/Projects/ColorPalette/SkinToneClassifiersounddevice==0.4.6soupsieve==2.5stack-data==0.6.3tensorboard==2.16.2tensorboard-data-server==0.7.2tensorflow==2.16.1tensorflow-io-gcs-filesystem==0.37.0termcolor==2.4.0tf_keras==2.16.0threadpoolctl==3.5.0tqdm==4.66.4traitlets==5.14.3typing_extensions==4.11.0tzdata==2024.1urllib3==2.2.1wcwidth==0.2.13Werkzeug==3.0.3wrapt==1.16.0wxPython==4.2.1zipp==3.18.122 unmodified lines232425262728293031323 unmodified lines36373839404142245 unmodified lines2882892902912922932942952962972982993008 unmodified lines3093103113123133143153163173183193203213226 unmodified lines3293303313323333342 unmodified lines33733833934034134234334434534634734834935035135235335410 unmodified lines36536636736836937037137237337437537637737837938038138238313 unmodified lines3973983994004014024034044054064074084094104114124134144154164178 unmodified lines4264274284294304314 unmodified lines43643743843944044144244344444544644744844922 unmodified linesimage_np = image[mask > 0]pixels = image_np.reshape((-1, 3))n_colors_elbow = optimal_clusters_elbow(pixels, max_clusters=15)n_colors_silhouette = optimal_clusters_silhouette(pixels, max_clusters=5)kmeans_silhouette = KMeans(n_clusters=n_colors_silhouette)kmeans_silhouette.fit(pixels)kmeans_elbow = KMeans(n_clusters=n_colors_elbow)kmeans_elbow.fit(pixels)dominant_colors = kmeans_elbow.cluster_centers_3 unmodified linesdominant_percentages = [counts[i] / total_count for i in counts.keys()]if debug:visualize_clusters(image, mask, kmeans_elbow, tag="elbow")visualize_clusters(image, mask, kmeans_silhouette, tag="silhouette")return dominant_colors, dominant_percentages245 unmodified linesreturn analysis# Example usageresult = color_analysis("light", "golden blonde", "blue")print(result)def analyze_and_visualize(image, hair_mask, skin_mask, eye_mask, n_colors=3):image_np = np.array(image)hair_mask_np = np.array(hair_mask)skin_mask_np = np.array(skin_mask)8 unmodified linesraise ValueError("Image and all masks must have the same dimensions")hair_palette = HairColorPalette()hair_dominant_colors, hair_dominant_percentages = get_dominant_colors(image_np, hair_mask_np, n_colors, debug=True)import IPythonIPython.embed()raise Exception("stop")hair_color, hair_hex, hair_distance = get_closest_color(hair_dominant_colors, hair_palette.palette)6 unmodified linesskin_dominant_colors, skin_palette.palette)eye_palette = EyeColorPalette()eye_dominant_colors, eye_dominant_percentages = get_dominant_colors(image_np, eye_mask_np, n_colors, debug=True2 unmodified lineseye_dominant_colors, eye_palette.palette)# create overlay for different color for hair, skin and eyehair_overlay = np.zeros_like(image_np)skin_overlay = np.zeros_like(image_np)eye_overlay = np.zeros_like(image_np)hair_overlay[hair_mask_np > 0] = [0, 255, 0] # hex_to_rgb(hair_hex)skin_overlay[skin_mask_np > 0] = [255, 0, 0] # hex_to_rgb(skin_hex)eye_overlay[eye_mask_np > 0] = [0, 0, 255] # hex_to_rgb(eye_hex)combined_overlay = cv2.addWeighted(image_np, 0.8, hair_overlay, 0.2, 0)combined_overlay = cv2.addWeighted(combined_overlay, 0.8, skin_overlay, 0.2, 0)combined_overlay = cv2.addWeighted(combined_overlay, 0.8, eye_overlay, 0.2, 0)bar_width = 50hair_color_bar = create_dominant_color_bar(10 unmodified linesimage_np,list(hair_palette.palette.keys()).index(hair_color),hair_palette.palette,bar_width,)skin_palette_bar = create_tone_palette_bar(image_np,list(skin_palette.palette.keys()).index(skin_color),skin_palette.palette,bar_width,)eye_palette_bar = create_tone_palette_bar(image_np,list(eye_palette.palette.keys()).index(eye_color),eye_palette.palette,bar_width,)output_image = np.hstack(13 unmodified lineshair_dominant_percentages,hair_hex,hair_distance,img_shape,)msg_bar_skin = create_message_bar(skin_dominant_colors,skin_dominant_percentages,skin_hex,skin_distance,img_shape,)msg_bar_eye = create_message_bar(eye_dominant_colors, eye_dominant_percentages, eye_hex, eye_distance, img_shape)output_image = np.vstack([output_image, msg_bar_hair, msg_bar_skin, msg_bar_eye])# Create JSON recordanalysis_record = {"hair": {"dominant_colors": [rgb_to_hex(color) for color in hair_dominant_colors],8 unmodified lines"closest_color": skin_color,"closest_color_hex": skin_hex,"distance": skin_distance,},"eyes": {"dominant_colors": [rgb_to_hex(color) for color in eye_dominant_colors],4 unmodified lines},}# Save output image and JSON record# cv2.imwrite("output_image.png", output_image)# with open("analysis_record.json", "w") as json_file:# json.dump(analysis_record, json_file, indent=4)return output_image, analysis_record# cv2.imwrite("output_image.png", output_image)# Example usage# analyze_and_visualize('path_to_image.jpg', 'path_to_hair_mask.png', 'path_to_skin_mask.png', 'path_to_eye_mask.png')22 unmodified lines232425262728293031323 unmodified lines36373839404142245 unmodified lines2882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243258 unmodified lines3343353363373383393403413426 unmodified lines3493503513523533543553563573583592 unmodified lines36236336436536636736810 unmodified lines37938038138238338438538638738838939039139239339439539639713 unmodified lines4114124134144154164174184194204214224234244254264274284294304318 unmodified lines4404414424434444454464474 unmodified lines45245345445522 unmodified linesimage_np = image[mask > 0]pixels = image_np.reshape((-1, 3))n_colors_elbow = optimal_clusters_elbow(pixels, max_clusters=15)# n_colors_silhouette = optimal_clusters_silhouette(pixels, max_clusters=5)# kmeans_silhouette = KMeans(n_clusters=n_colors_silhouette)# kmeans_silhouette.fit(pixels)kmeans_elbow = KMeans(n_clusters=n_colors_elbow)kmeans_elbow.fit(pixels)dominant_colors = kmeans_elbow.cluster_centers_3 unmodified linesdominant_percentages = [counts[i] / total_count for i in counts.keys()]if debug:visualize_clusters(image, mask, kmeans_elbow, tag="elbow")# visualize_clusters(image, mask, kmeans_silhouette, tag="silhouette")return dominant_colors, dominant_percentages245 unmodified linesreturn analysis# # Example usage# result = color_analysis("light", "golden blonde", "blue")# print(result)def create_combined_overlay(image, hair_mask, skin_mask, eye_mask):"""Create an overlay image by combining the original image with the hair, skin, and eye masks.:param image: Original image as a numpy array.:param hair_mask: Hair mask as a numpy array.:param skin_mask: Skin mask as a numpy array.:param eye_mask: Eye mask as a numpy array.:return: Combined overlay image as a numpy array."""# Create overlays for different partshair_overlay = np.zeros_like(image)skin_overlay = np.zeros_like(image)eye_overlay = np.zeros_like(image)# Color the maskshair_overlay[hair_mask > 0] = [0, 255, 0] # Green for hairskin_overlay[skin_mask > 0] = [255, 0, 0] # Red for skineye_overlay[eye_mask > 0] = [0, 0, 255] # Blue for eyes# Combine the overlays with the original imagecombined_overlay = cv2.addWeighted(image, 0.8, hair_overlay, 0.2, 0)combined_overlay = cv2.addWeighted(combined_overlay, 0.8, skin_overlay, 0.2, 0)combined_overlay = cv2.addWeighted(combined_overlay, 0.8, eye_overlay, 0.2, 0)return combined_overlaydef analyze_and_visualize(image, hair_mask, skin_mask, eye_mask, n_colors=3):image_np = np.array(image)hair_mask_np = np.array(hair_mask)skin_mask_np = np.array(skin_mask)8 unmodified linesraise ValueError("Image and all masks must have the same dimensions")hair_palette = HairColorPalette()hair_dominant_colors, hair_dominant_percentages = get_dominant_colors(image_np, hair_mask_np, n_colors, debug=True)hair_color, hair_hex, hair_distance = get_closest_color(hair_dominant_colors, hair_palette.palette)6 unmodified linesskin_dominant_colors, skin_palette.palette)# Calculate ITA for the dominant skin colordominant_skin_color = skin_dominant_colors[0]ita = skin_palette.calculate_ita(dominant_skin_color)vectorscope_check = skin_palette.is_within_vectorscope_skin_tone_line(dominant_skin_color)eye_palette = EyeColorPalette()eye_dominant_colors, eye_dominant_percentages = get_dominant_colors(image_np, eye_mask_np, n_colors, debug=True2 unmodified lineseye_dominant_colors, eye_palette.palette)combined_overlay = create_combined_overlay(image_np, hair_mask_np, skin_mask_np, eye_mask_np)bar_width = 50hair_color_bar = create_dominant_color_bar(10 unmodified linesimage_np,list(hair_palette.palette.keys()).index(hair_color),hair_palette.palette,bar_width)skin_palette_bar = create_tone_palette_bar(image_np,list(skin_palette.palette.keys()).index(skin_color),skin_palette.palette,bar_width)eye_palette_bar = create_tone_palette_bar(image_np,list(eye_palette.palette.keys()).index(eye_color),eye_palette.palette,bar_width)output_image = np.hstack(13 unmodified lineshair_dominant_percentages,hair_hex,hair_distance,img_shape)msg_bar_skin = create_message_bar(skin_dominant_colors,skin_dominant_percentages,skin_hex,skin_distance,img_shape)msg_bar_eye = create_message_bar(eye_dominant_colors, eye_dominant_percentages, eye_hex, eye_distance, img_shape)output_image = np.vstack([output_image, msg_bar_hair, msg_bar_skin, msg_bar_eye])analysis_record = {"hair": {"dominant_colors": [rgb_to_hex(color) for color in hair_dominant_colors],8 unmodified lines"closest_color": skin_color,"closest_color_hex": skin_hex,"distance": skin_distance,"ita": ita,# "vectorscope_check": vectorscope_check,},"eyes": {"dominant_colors": [rgb_to_hex(color) for color in eye_dominant_colors],4 unmodified lines},}return output_image, analysis_record
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475import gradio as grimport cv2import numpy as npfrom PIL import Image
from skin_analyzer import analyze_skin_functionfrom image import ImageBundle
def process_image(image, analyze_skin, analyze_eyes, analyze_hair): """ Process the uploaded image and return the analysis results based on selected analyses. :param image: Uploaded image. :param analyze_skin: Whether to perform skin analysis. :param analyze_eyes: Whether to perform eye analysis. :param analyze_hair: Whether to perform hair analysis. :return: Analysis results. """ image_bundle = ImageBundle(image_array=image) # Detect faces and landmarks face_data = image_bundle.detect_faces_and_landmarks() landmarks = image_bundle.detect_face_landmarks()
# Perform segmentation segmentation_maps = image_bundle.segment_image() analysis_results = {} overlay_images = []
# if analyze_hair: # hair_mask = segmentation_maps["hair_mask"] # # analysis_results['hair_analysis'] = analyze_hair_function(image, hair_mask) # overlay_images.append(segmentation_maps["hair_mask"])
if analyze_skin: skin_mask = segmentation_maps["face_skin_mask"] skin_analysis = analyze_skin_function(image, skin_mask) analysis_results['skin_analysis'] = skin_analysis overlay_images.append(skin_analysis["overlay_image"])
# if analyze_eyes: # eye_mask = segmentation_maps["right_iris_mask"] | segmentation_maps["left_iris_mask"] # # analysis_results['eye_analysis'] = analyze_eye_function(image, eye_mask) # overlay_images.append(eye_mask)
# Combine overlay images if overlay_images: combined_overlay = np.maximum.reduce(overlay_images) else: combined_overlay = image
# Convert combined_overlay to PIL Image for display combined_overlay = Image.fromarray(combined_overlay)
return combined_overlay, analysis_results
# Define Gradio interfaceiface = gr.Interface( fn=process_image, inputs=[ gr.Image(type="numpy", label="Upload an Image"), gr.Checkbox(label="Skin Analysis", value=True), gr.Checkbox(label="Eye Analysis", value=False), gr.Checkbox(label="Hair Analysis", value=False), ], outputs=[ gr.Image(type="pil", label="Processed Image"), gr.JSON(label="Analysis Results") ], title="Color Palette Analysis", description="Upload an image to analyze the skin, hair, and eye colors. Select the analyses you want to perform.")
# Launch the Gradio interfaceiface.launch()123456789101112131415161718192021222324252627282930313233 unmodified lines66676869707133 unmodified lines105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170# Description: Image handling utilities for the Skin Tone Classifier.import loggingimport cv2import numpy as npfrom deepface import DeepFacefrom utils import is_url, extract_filename_and_extension, alphabet_id, ArgumentErrorfrom segmentation_utils import detect_eye_mask, mediapipe_selfie_segmentorimport requestsfrom PIL import Image, ExifTags, ImageCmsfrom io import BytesIOimport pillow_heif# Register HEIF openerpillow_heif.register_heif_opener()LOG = logging.getLogger(__name__)class ImageBundle:def __init__(self, image_source):"""Initialize the ImageHandler object.:param image_source: Path to the image file or URL of the image."""self.image_source = image_sourceself.exif_data = {}self.segmentation_maps = {}self._open_image()def _open_image(self):"""33 unmodified linesresponse.raise_for_status() # Raise an exception for HTTP errorsself.image = Image.open(BytesIO(response.content))def _handle_color_profile(self):"""Handle the color profile of the image if mentioned.33 unmodified linesreturn np.all(r == g) and np.all(g == b)return Falsedef _get_faces(self, detector_backend="retinaface", is_bw=False, min_size=(90, 90)):"""Get the coordinates of the detected faces in the image.:param detector_backend: Face detector backend to use.:param is_bw: Whether the image is black and white.gets a list of faces detected, with each face as dict of facial_area, face, confidence"""self.faces = DeepFace.extract_faces(self.numpy_image(), detector_backend=detector_backend, grayscale=is_bw)if len(self.faces) == 0:raise ValueError("No face is detected in the image.")elif len(self.faces) > 1:raise ValueError("Multiple faces are detected in the image.")else:# check if the face is too smallface = self.faces[0]if (face["facial_area"]["w"] < min_size[0]or face["facial_area"]["h"] < min_size[1]):raise ValueError("The face is too small.")else:self.faces = self.faces[0]return self.facesdef numpy_image(self):"""Convert the image to a numpy array.:return: Numpy array of the image."""image = np.array(self.image)# check if image is 4 chanel convert ot threeif image.shape[2] == 4:image = image[:, :, :3]# convert image to BGRimage = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)return imagedef _segment_image(self, segment=["face_skin", "body_skin", "hair", "eyes", "clothes"]):"""Get a segmentation map by name.:param name: Name of the segmentation map.:return: Segmentation map as a numpy array or None if not found."""if "eyes" in segment:del segment[segment.index("eyes")]eye_mask = detect_eye_mask(self.numpy_image(), self.faces)self.segmentation_maps["eyes_mask"] = eye_maskif ("face_skin" in segmentor "body_skin" in segmentor "hair" in segmentor "clothes" in segment):# import IPython; IPython.embed()mask_dict = mediapipe_selfie_segmentor(self.numpy_image(), segment=segment)for seg in segment:self.segmentation_maps[seg + "_mask"] = mask_dict[seg]return self.segmentation_maps12345678910111213141516171819202122232425262728293031323333 unmodified lines6768697071727374757677787980818283848586878833 unmodified lines122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175import loggingimport cv2import numpy as npfrom PIL import Image, ExifTags, ImageCmsfrom io import BytesIOimport requestsimport pillow_heiffrom segmentation_utils import detect_faces_and_landmarks, detect_face_landmarks, mediapipe_selfie_segmentor, create_feature_masksfrom utils import is_url, extract_filename_and_extension# Register HEIF openerpillow_heif.register_heif_opener()LOG = logging.getLogger(__name__)class ImageBundle:def __init__(self, image_source=None, image_array=None):"""Initialize the ImageHandler object.:param image_source: Path to the image file or URL of the image.:param image_array: Numpy array of the image."""self.image_source = image_sourceself.image_array = image_arrayself.exif_data = {}self.segmentation_maps = {}if image_array is not None:self._open_image_from_array(image_array)else:self._open_image()def _open_image(self):"""33 unmodified linesresponse.raise_for_status() # Raise an exception for HTTP errorsself.image = Image.open(BytesIO(response.content))def _open_image_from_array(self, image_array):"""Open an image from a numpy array.:param image_array: Numpy array of the image."""self.image = Image.fromarray(image_array)self._handle_color_profile()self._extract_exif_data()self.is_black_and_white = self._is_black_and_white()self.basename = "uploaded_image"self.ext = ".jpg"if self.is_black_and_white:raise ValueError("The image is black and white. Please provide a colored image.")def _handle_color_profile(self):"""Handle the color profile of the image if mentioned.33 unmodified linesreturn np.all(r == g) and np.all(g == b)return Falsedef detect_faces_and_landmarks(self):"""Detect faces and landmarks using MediaPipe.:return: List of dictionaries with face and landmark information."""image_np = np.array(self.image)face_data = detect_faces_and_landmarks(image_np)self.faces = face_datareturn self.facesdef detect_face_landmarks(self):"""Detect face landmarks using MediaPipe.:return: Dictionary with landmarks for iris, lips, eyebrows, and eyes."""image_np = np.array(self.image)landmarks = detect_face_landmarks(image_np)self.landmarks = landmarksreturn self.landmarksdef segment_image(self):"""Segment the image using MediaPipe Multi-Class Selfie Segmentation.:return: Dictionary of segmentation masks."""image_np = np.array(self.image)masks = mediapipe_selfie_segmentor(image_np)# Detect face landmarks and create masks for individual featureslandmarks = self.detect_face_landmarks()feature_masks = create_feature_masks(image_np, landmarks)# Subtract feature masks from face skin maskfor feature in ["lips_mask", "left_eyebrow_mask", "right_eyebrow_mask", "left_eye_mask", "right_eye_mask", "left_iris_mask", "right_iris_mask"]:if "iris" in feature:masks[feature] = feature_masks[feature]masks["face_skin_mask"] = cv2.subtract(masks["face_skin_mask"], feature_masks[feature])self.segmentation_maps = masksreturn self.segmentation_mapsdef numpy_image(self):"""Convert the image to a numpy array.:return: Numpy array of the image."""image = np.array(self.image)if image.shape[2] == 4:image = image[:, :, :3]image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)return image
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899import loggingfrom pathlib import Pathfrom typing import Union, Literalimport cv2from analyze import analyze_and_visualizefrom image import ImageBundlefrom utils import ArgumentErrorimport cv2from argparse import ArgumentParserfrom json import dumps # Optionalimport osLOG = logging.getLogger(__name__)def process(filename_or_url: Union[str, Path],n_dominant_colors: int = 3,min_size: tuple[int, int] = (90, 90),return_report_image=False,):"""Process the image and return the result.:param filename_or_url: The filename (in local devices) or URL (in Internet) of the image.:param image_type: Specify whether the input image(s) is/are colored or black/white.Valid choices are: "auto", "color" or "bw", Defaults to "auto", which will be detected automatically.:param convert_to_black_white: Whether to convert the image to black/white before processing. Defaults to False.:param n_dominant_colors: Number of dominant colors to be extracted from the image. Defaults to 2.:param min_size: Minimum possible face size. Faces smaller than that are ignored, defaults to (90, 90).:param return_report_image: Whether to return the report image(s) in the result. Defaults to False.:return:"""ib = ImageBundle(filename_or_url)basename, ext = ib.basename, ib.extfaces = ib._get_faces(detector_backend="retinaface", min_size=min_size)if len(faces) == 0:raise ArgumentError("No face detected in the image.")segmentation_maps = ib._segment_image()image = ib.numpy_image()# convert image to RGBimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)hair_mask = segmentation_maps["hair_mask"]skin_mask = segmentation_maps["face_skin_mask"]eye_mask = segmentation_maps["eyes_mask"]report_images, records = analyze_and_visualize(image, hair_mask, skin_mask, eye_mask, n_colors=n_dominant_colors)return {"basename": basename,"extension": ext,"faces": records,"report_images": report_images if return_report_image else None,}if __name__ == "__main__":args = ArgumentParser()args.add_argument("-i","--image_path",required=True,help="Path to the image",)args = args.parse_args()# import IPython# IPython.embed()print("Image path:", args.image_path)image_path = args.image_pathim_basename = os.path.basename(image_path).split(".")[0]result = process(image_path, return_report_image=True)report_images = result.pop("report_images")# Save the faces# for i, face in enumerate(report_images.keys()):cv2.imwrite(f"outputs/face_{im_basename}.png",cv2.cvtColor(report_images, cv2.COLOR_RGB2BGR),)# report_images.save(f"outputs/face_{im_basename}.png")# convert the result to jsonresult_json = dumps(result)# save the json dumpwith open(f"outputs/faces_{im_basename}.json", "w") as f:f.write(result_json)print(result["faces"]["hair"])print(result["faces"]["skin"])print(result["faces"]["eyes"])123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102import loggingfrom pathlib import Pathfrom typing import Unionfrom argparse import ArgumentParserimport cv2from PIL import Imagefrom analyze import analyze_and_visualizefrom skin_analyzer import analyze_skin_functionfrom image import ImageBundleimport osimport numpy as npLOG = logging.getLogger(__name__)def process_image(filename_or_url: Union[str, Path], analyze_skin, analyze_eyes, analyze_hair):"""Process the image and return the result.:param filename_or_url: The filename (in local devices) or URL (in Internet) of the image.:param analyze_skin: Whether to perform skin analysis.:param analyze_eyes: Whether to perform eye analysis.:param analyze_hair: Whether to perform hair analysis.:return: Analysis results."""image_bundle = ImageBundle(image_source=filename_or_url)# Detect faces and landmarksface_data = image_bundle.detect_faces_and_landmarks()landmarks = image_bundle.detect_face_landmarks()# Perform segmentationsegmentation_maps = image_bundle.segment_image()analysis_results = {}overlay_images = []image_np = image_bundle.numpy_image()if analyze_hair:hair_mask = segmentation_maps["hair_mask"]hair_analysis = analyze_hair_function(image_np, hair_mask)analysis_results['hair_analysis'] = hair_analysisoverlay_images.append(segmentation_maps["hair_mask"])if analyze_skin:skin_mask = segmentation_maps["face_skin_mask"]skin_analysis = analyze_skin_function(image_np, skin_mask)overlay_images.append(skin_analysis["overlay_image"])if "overlay_image" in skin_analysis.keys():del skin_analysis["overlay_image"]analysis_results['skin_analysis'] = skin_analysis# del overlay_images[-1]if analyze_eyes:eye_mask = segmentation_maps["right_eye_mask"] | segmentation_maps["left_eye_mask"]eye_analysis = analyze_eye_function(image_np, eye_mask)analysis_results['eye_analysis'] = eye_analysisoverlay_images.append(segmentation_maps["right_eye_mask"])# Combine overlay imagesif overlay_images:combined_overlay = np.maximum.reduce(overlay_images)else:combined_overlay = image_np# Convert combined_overlay to BGR for saving with OpenCV# combined_overlay_bgr = cv2.cvtColor(combined_overlay, cv2.COLOR_RGB2BGR)return combined_overlay, analysis_resultsif __name__ == "__main__":parser = ArgumentParser()parser.add_argument("-i", "--image_path", required=True, help="Path to the image")parser.add_argument("--analyze_skin", action="store_true", help="Perform skin analysis")parser.add_argument("--analyze_eyes", action="store_true", help="Perform eye analysis")parser.add_argument("--analyze_hair", action="store_true", help="Perform hair analysis")args = parser.parse_args()image_path = args.image_path# Process the imagecombined_overlay, analysis_results = process_image(image_path,analyze_skin=args.analyze_skin,analyze_eyes=args.analyze_eyes,analyze_hair=args.analyze_hair)# Save the resultsim_basename = os.path.basename(image_path).split(".")[0]overlay_image_path = f"outputs/overlay_{im_basename}.png"cv2.imwrite(overlay_image_path, combined_overlay)import json# Save the analysis results as JSON, including the path to the overlay imageanalysis_results["overlay_image_path"] = overlay_image_pathwith open(f"outputs/analysis_{im_basename}.json", "w") as f:json.dump(analysis_results, f, indent=4)print("Analysis complete. Results saved in 'outputs' directory.")
123456789101112132 unmodified lines1617181920212223242526272829303132333435363738394041424344454647484950from mediapipe.tasks import pythonfrom mediapipe.tasks.python import visionimport mediapipe as mpimport cv2import numpy as npdef mediapipe_selfie_segmentor(image: np.ndarray, segment: list = ["face_skin", "body_skin", "hair"]):# Create the options that will be used for ImageSegmenterbase_options = python.BaseOptions(model_asset_path="model_weights/selfie_segmenter.tflite")options = vision.ImageSegmenterOptions(base_options=base_options,2 unmodified lines)with vision.ImageSegmenter.create_from_options(options) as segmenter:# Create the MediaPipe image file that will be segmentedimage = mp.Image(image_format=mp.ImageFormat.SRGB, data=image)# Retrieve the masks for the segmented imagesegmentation_result = segmenter.segment(image)category_mask = segmentation_result.category_mask.numpy_view()bg_mask = (category_mask == 0)hair_mask = (category_mask == 1)body_skin_mask = (category_mask == 2)face_skin_mask = (category_mask == 3)clothes_mask = (category_mask == 4)return_dict = {}for seg in segment:return_dict[seg] = locals()[seg + "_mask"]return return_dictdef detect_eye_mask(image, face_coord):h, w = image.shape[0:2]imgMask = np.zeros((h, w), np.uint8)left_eye = face_coord["facial_area"]["left_eye"]right_eye = face_coord["facial_area"]["right_eye"]eye_distance = np.linalg.norm(np.array(left_eye)-np.array(right_eye))eye_radius = eye_distance/12 # approximatecv2.circle(imgMask, left_eye, int(eye_radius), (255,255,255), -1)cv2.circle(imgMask, right_eye, int(eye_radius), (255,255,255), -1)return imgMask12345678910111213141516171819202122232425262728293031323334353637383940414243444546472 unmodified lines5051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186from mediapipe.tasks import pythonfrom mediapipe.tasks.python import visionimport mediapipe as mpimport cv2import numpy as np# Initialize mediapipe solutionsmp_face_detection = mp.solutions.face_detectionmp_face_mesh = mp.solutions.face_meshdef detect_faces_and_landmarks(image: np.ndarray):"""Detect faces and landmarks using MediaPipe Face Detection.:param image: Input image as a numpy array.:return: List of dictionaries with face and landmark information."""with mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5) as face_detection:results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))face_data = []if results.detections:for detection in results.detections:bboxC = detection.location_data.relative_bounding_boxh, w, c = image.shapebbox = int(bboxC.xmin * w), int(bboxC.ymin * h), \int(bboxC.width * w), int(bboxC.height * h)landmarks = detection.location_data.relative_keypointsface_data.append({"bbox": bbox,"landmarks": landmarks})return face_datadef mediapipe_selfie_segmentor(image: np.ndarray, segment: list = ["face_skin", "body_skin", "hair"]):"""Segment image using MediaPipe Multi-Class Selfie Segmentation.:param image: Input image as a numpy array.:param segment: List of segments to extract.:return: Dictionary of segmentation masks."""# Create the options that will be used for ImageSegmenterbase_options = python.BaseOptions(model_asset_path="model_weights/selfie_multiclass_256x256.tflite")options = vision.ImageSegmenterOptions(base_options=base_options,2 unmodified lines)with vision.ImageSegmenter.create_from_options(options) as segmenter:# Create the MediaPipe image file that will be segmentedmp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=image)# Retrieve the masks for the segmented imagesegmentation_result = segmenter.segment(mp_image)category_mask = segmentation_result.category_mask.numpy_view()h, w = category_mask.shapemasks = {"face_skin_mask": np.zeros((h, w), dtype=np.uint8),"hair_mask": np.zeros((h, w), dtype=np.uint8),"body_skin_mask": np.zeros((h, w), dtype=np.uint8)}# Define class labels based on MediaPipe segmentation (example, may need adjustment)face_skin_class = 3hair_class = 1body_skin_class = 2masks["face_skin_mask"][category_mask == face_skin_class] = 255masks["hair_mask"][category_mask == hair_class] = 255masks["body_skin_mask"][category_mask == body_skin_class] = 255return masksdef detect_face_landmarks(image: np.ndarray):"""Detect face landmarks using MediaPipe Face Mesh.:param image: Input image as a numpy array.:return: Dictionary with landmarks for iris, lips, eyebrows, and eyes."""with mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5) as face_mesh:results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))face_landmarks = {"left_iris": [],"right_iris": [],"lips": [],"left_eyebrow": [],"right_eyebrow": [],"left_eye": [],"right_eye": []}if results.multi_face_landmarks:for face_landmarks_data in results.multi_face_landmarks:# Left iris landmarksfor i in range(468, 473): # Left iris landmarkslandmark = face_landmarks_data.landmark[i]face_landmarks["left_iris"].append((landmark.x, landmark.y))# Right iris landmarksfor i in range(473, 478): # Right iris landmarkslandmark = face_landmarks_data.landmark[i]face_landmarks["right_iris"].append((landmark.x, landmark.y))# Outer lips landmarksfor i in [61, 146, 91, 181, 84, 17, 314, 405, 321, 375, 291, 0, 409, 270, 269, 267, 37,39, 40, 185]:landmark = face_landmarks_data.landmark[i]face_landmarks["lips"].append((landmark.x, landmark.y))# Left eyebrow landmarksfor i in [70, 63, 105, 66, 107]:landmark = face_landmarks_data.landmark[i]face_landmarks["left_eyebrow"].append((landmark.x, landmark.y))# Right eyebrow landmarksfor i in [336, 296, 334, 293, 300]:landmark = face_landmarks_data.landmark[i]face_landmarks["right_eyebrow"].append((landmark.x, landmark.y))# Left eye landmarksfor i in [33, 246, 161, 160, 159, 158, 157, 173, 133, 155, 154, 153, 145, 144, 163, 7]:landmark = face_landmarks_data.landmark[i]face_landmarks["left_eye"].append((landmark.x, landmark.y))# Right eye landmarksfor i in [463, 398, 384, 385, 386, 387, 388, 466, 263, 249, 390, 373, 374, 380, 381, 382]:landmark = face_landmarks_data.landmark[i]face_landmarks["right_eye"].append((landmark.x, landmark.y))return face_landmarksdef create_feature_masks(image: np.ndarray, landmarks: dict):"""Create individual masks for facial features based on landmarks.:param image: Input image as a numpy array.:param landmarks: Dictionary with landmarks for iris, lips, eyebrows, and eyes.:return: Dictionary with masks for each facial feature."""h, w = image.shape[:2]masks = {"lips_mask": np.zeros((h, w), dtype=np.uint8),"left_eyebrow_mask": np.zeros((h, w), dtype=np.uint8),"right_eyebrow_mask": np.zeros((h, w), dtype=np.uint8),"left_eye_mask": np.zeros((h, w), dtype=np.uint8),"right_eye_mask": np.zeros((h, w), dtype=np.uint8),"left_iris_mask": np.zeros((h, w), dtype=np.uint8),"right_iris_mask": np.zeros((h, w), dtype=np.uint8)}# Define the order of the points to form polygons correctlylips_order = [61, 146, 91, 181, 84, 17, 314, 405, 321, 375, 291, 0, 409, 270, 269, 267, 37,39, 40, 185]left_eyebrow_order = [70, 63, 105, 66, 107]right_eyebrow_order = [336, 296, 334, 293, 300]left_eye_order = [33, 246, 161, 160, 159, 158, 157, 173, 133, 155, 154, 153, 145, 144, 163, 7]right_eye_order = [463, 398, 384, 385, 386, 387, 388, 466, 263, 249, 390, 373, 374, 380, 381, 382]left_iris_order = [468, 469, 470, 471, 472]right_iris_order = [473, 474, 475, 476, 477]orders = {"lips": lips_order,"left_eyebrow": left_eyebrow_order,"right_eyebrow": right_eyebrow_order,"left_eye": left_eye_order,"right_eye": right_eye_order,"left_iris": left_iris_order,"right_iris": right_iris_order}for feature, order in orders.items():points = np.array([(int(landmarks[feature][i][0] * w), int(landmarks[feature][i][1] * h)) for i in range(len(order))], dtype=np.int32)if len(points) > 0:cv2.fillPoly(masks[f"{feature}_mask"], [points], 255)return masksif __name__ == "__main__":# Test the face detection and segmentationimage = cv2.imread("inputs/grace.jpg")face_data = detect_faces_and_landmarks(image)print(face_data)masks = mediapipe_selfie_segmentor(image)# write it to diskfor key, mask in masks.items():if key == "face_skin_mask":# create feature maskslandmarks = detect_face_landmarks(image)feature_masks = create_feature_masks(image, landmarks)# subtract eyes, lips and eyebrows from face skin maskfor feature, feature_mask in feature_masks.items():if "iris_mask" in feature:cv2.imwrite(f"outputs/{feature}.png", feature_mask)mask = cv2.subtract(mask, feature_mask)cv2.imwrite(f"outputs/{key}.png", mask)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128import cv2import numpy as npfrom colormath.color_objects import LabColor, sRGBColorfrom colormath.color_conversions import convert_colorfrom PIL import Image
def sample_skin_pixels(image, mask, l_min=10, l_max=90): """ Sample skin pixels from different areas of the face, filtering out very light and very dark pixels. :param image: Input image as a numpy array. :param mask: Skin mask as a numpy array. :param l_min: Minimum L* value to keep. :param l_max: Maximum L* value to keep. :return: List of sampled skin pixels in LAB color space. """ skin_pixels = image[mask > 0]
# Convert skin pixels to LAB color space and filter based on L* value lab_pixels = [] for pixel in skin_pixels: rgb = sRGBColor(pixel[2], pixel[1], pixel[0], is_upscaled=True) lab = convert_color(rgb, LabColor) if l_min <= lab.lab_l <= l_max: lab_pixels.append([lab.lab_l, lab.lab_a, lab.lab_b]) return np.array(lab_pixels)
def calculate_ita(lab_pixels): """ Calculate the Individual Typology Angle (ITA) from LAB pixels. :param lab_pixels: List of skin pixels in LAB color space. :return: ITA value. """ mean_l = np.mean(lab_pixels[:, 0]) mean_b = np.mean(lab_pixels[:, 2]) ita = np.arctan((mean_l - 50) / mean_b) * (180 / np.pi) return ita
def determine_undertones(lab_pixels): """ Determine the undertones based on LAB color values. :param lab_pixels: List of skin pixels in LAB color space. :return: Undertone category (warm, cool, neutral). """ mean_a = np.mean(lab_pixels[:, 1]) mean_b = np.mean(lab_pixels[:, 2])
if mean_b > 0 and mean_a > 0: undertone = "Warm" elif mean_a > 0 and mean_b <= 0: undertone = "Cool" else: undertone = "Neutral"
return undertone
def determine_season(ita, mean_l, mean_a, mean_b): """ Determine the color season based on ITA and LAB values. :param ita: ITA value. :param mean_l: Mean L* value. :param mean_a: Mean a* value. :param mean_b: Mean b* value. :return: Season category. """ if 41 < ita <= 53: if mean_a > 0 and mean_b > 0: return "Light Spring" elif mean_a <= 0 and mean_b <= 0: return "Light Summer" elif mean_a > 0 and mean_b > 20: return "Light Autumn" elif mean_a <= 0 and mean_b <= 0: return "Light Winter" elif 28 < ita <= 41: if mean_a > 0 and mean_b > 0: return "True Spring" elif mean_a <= 0 and mean_b <= 0: return "True Summer" elif mean_a > 0 and mean_b > 20: return "True Autumn" elif mean_a <= 0 and mean_b <= 0: return "True Winter" elif 20 < ita <= 28: if mean_a > 0 and mean_b > 0: return "Deep Spring" elif mean_a <= 0 and mean_b <= 0: return "Deep Summer" elif mean_a > 0 and mean_b > 20: return "Deep Autumn" elif mean_a <= 0 and mean_b <= 0: return "Deep Winter"
return "Unknown"
def analyze_skin_function(image, skin_mask): """ Analyze the skin tone and determine undertones and color season. :param image: Input image as a numpy array. :param skin_mask: Skin mask as a numpy array. :return: Analysis result with undertone, ITA, season, and overlayed image. """ lab_pixels = sample_skin_pixels(image, skin_mask) ita = calculate_ita(lab_pixels) print("ITA:", ita) undertone = determine_undertones(lab_pixels) print("Undertone:", undertone) mean_l = np.mean(lab_pixels[:, 0]) mean_a = np.mean(lab_pixels[:, 1]) mean_b = np.mean(lab_pixels[:, 2]) print("Mean L*:", mean_l) print("Mean a*:", mean_a) print("Mean b*:", mean_b) season = determine_season(ita, mean_l, mean_a, mean_b) print("Season:", season)
# Create an overlayed image overlay = image.copy() overlay[skin_mask > 0] = (0, 255, 0) overlay = cv2.addWeighted(image, 0.7, overlay, 0.3, 0)
return { "undertone": undertone, "ita": ita, "season": season, "overlay_image": overlay }1 unmodified line2345678912 unmodified lines2223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768691 unmodified line71727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521 unmodified lineimport numpy as npfrom sklearn.cluster import KMeansfrom collections import Counterfrom color_utils import calculate_color_distance_labclass SkinTonePalette:def __init__(self):12 unmodified lines}def get_dominant_colors(self, image_np, n_colors):# Reshape the image to be a list of pixelspixels = image_np.reshape((-1, 3))# Use KMeans to find n_colors clusters in the imagekmeans = KMeans(n_clusters=n_colors)kmeans.fit(pixels)# Get the cluster centers (dominant colors)dominant_colors = kmeans.cluster_centers_# Get the number of pixels in each clustercounts = Counter(kmeans.labels_)# Sort the colors by the number of pixels in each clusterdominant_colors = [dominant_colors[i] for i in counts.keys()]return dominant_colorsdef get_closest_color(self, image, mask, n_colors=3):# Convert images to numpy arraysimage_np = np.array(image)mask_np = np.array(mask)# Ensure the images have the same h and wif image_np.shape[:2] != mask_np.shape[:2]:raise ValueError("Image and mask must have the same dimensions")# Extract the skin region from the imageskin_pixels = image_np[mask_np > 0]# Get dominant colors from the skin regiondominant_colors = self.get_dominant_colors(skin_pixels, n_colors)# Find the closest color in the paletteclosest_color = Noneclosest_hex = Nonemin_distance = float("inf")for dom_color in dominant_colors:for color_name, (color_value, color_hex) in self.palette.items():distance = calculate_color_distance_lab(dom_color, color_value)# distance = np.linalg.norm(dom_color - np.array(color_value))if distance < min_distance:min_distance = distanceclosest_color = color_name1 unmodified linereturn closest_color, closest_hex# Example usage# palette = SkinTonePalette()# closest_color, closest_hex = palette.get_closest_color('path_to_image.jpg', 'path_to_mask.png')# print(f"The closest skin tone is: {closest_color} with hex code: {closest_hex}")# def dominant_colors(image, to_bw, n_clusters=2):# if to_bw:# data = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# data = cv2.cvtColor(data, cv2.COLOR_GRAY2BGR)# else:# data = image# data = np.reshape(data, (-1, 3))# data = data[np.all(data != 0, axis=1)]# data = np.float32(data)# criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)# flags = cv2.KMEANS_RANDOM_CENTERS# compactness, labels, colors = cv2.kmeans(data, n_clusters, None, criteria, 10, flags)# labels, counts = np.unique(labels, return_counts=True)# order = (-counts).argsort()# colors = colors[order]# counts = counts[order]# percents = counts / counts.sum()# return colors, percents# DEFAULT_TONE_PALETTE = {# "color": [# "#373028",# "#422811",# "#513b2e",# "#6f503c",# "#81654f",# "#9d7a54",# "#bea07e",# "#e5c8a6",# "#e7c1b8",# "#f3dad6",# "#fbf2f3",# ],# # Refer to this paper:# # Leigh, A., & Susilo, T. (2009). Is voting skin-deep? Estimating the effect of candidate ballot photographs on election outcomes.# # Journal of Economic Psychology, 30(1), 61-70.# "bw": [# "#FFFFFF",# "#F0F0F0",# "#E0E0E0",# "#D0D0D0",# "#C0C0C0",# "#B0B0B0",# "#A0A0A0",# "#909090",# "#808080",# "#707070",# "#606060",# "#505050",# "#404040",# "#303030",# "#202020",# "#101010",# "#000000",# ],# }# DEFAULT_TONE_LABELS = {# "color": ["C" + alphabet_id(i) for i in range(len(DEFAULT_TONE_PALETTE["color"]))],# "bw": ["B" + alphabet_id(i) for i in range(len(DEFAULT_TONE_PALETTE["bw"]))],# }# def skin_tone(colors, percents, skin_tone_palette, tone_labels):# lab_tones = [convert_color(sRGBColor.new_from_rgb_hex(rgb), LabColor) for rgb in skin_tone_palette]# lab_colors = [convert_color(sRGBColor(rgb_r=r, rgb_g=g, rgb_b=b, is_upscaled=True), LabColor) for b, g, r in colors]# distances = [np.sum([delta_e_cie2000(c, label) * p for c, p in zip(lab_colors, percents)]) for label in lab_tones]# tone_id = np.argmin(distances)# distance: float = distances[tone_id]# tone_hex = skin_tone_palette[tone_id].upper()# tone_label = tone_labels[tone_id]# return tone_id, tone_hex, tone_label, distance1 unmodified line2345678912 unmodified lines22232425262728293031323334353637383940414243444546474849501 unmodified line52535455565758596061626364651 unmodified lineimport numpy as npfrom sklearn.cluster import KMeansfrom collections import Counterfrom color_utils import calculate_color_distance_lab, rgb_to_labimport cv2class SkinTonePalette:def __init__(self):12 unmodified lines}def get_dominant_colors(self, image_np, n_colors):pixels = image_np.reshape((-1, 3))kmeans = KMeans(n_clusters=n_colors)kmeans.fit(pixels)dominant_colors = kmeans.cluster_centers_counts = Counter(kmeans.labels_)dominant_colors = [dominant_colors[i] for i in counts.keys()]return dominant_colorsdef get_closest_color(self, image, mask, n_colors=3):image_np = np.array(image)mask_np = np.array(mask)if image_np.shape[:2] != mask_np.shape[:2]:raise ValueError("Image and mask must have the same dimensions")skin_pixels = image_np[mask_np > 0]dominant_colors = self.get_dominant_colors(skin_pixels, n_colors)closest_color = Noneclosest_hex = Nonemin_distance = float("inf")for dom_color in dominant_colors:for color_name, (color_value, color_hex) in self.palette.items():distance = calculate_color_distance_lab(dom_color, color_value)if distance < min_distance:min_distance = distanceclosest_color = color_name1 unmodified linereturn closest_color, closest_hexdef calculate_ita(self, rgb_color):lab_color = rgb_to_lab(rgb_color)L = lab_color.lab_lb = lab_color.lab_bita = np.arctan2(L - 50, b) * (180 / np.pi)return itadef is_within_vectorscope_skin_tone_line(self, rgb_color):ycbcr_color = cv2.cvtColor(np.uint8([[rgb_color]]), cv2.COLOR_RGB2YCrCb)[0][0]cb, cr = ycbcr_color[1], ycbcr_color[2]return 80 <= cb <= 120 and 133 <= cr <= 173
.gitignore+1
=1.9.0Added+37
requirements.txtDeleted-109
src/analyze.py+48-42
src/gradio_app.pyAdded+75
src/image.py+64-59
src/main.py+82-79
src/segmentation_utils.py+165-29
src/skin_analyzer.pyAdded+128
src/skin_utils.py+13-100