| import logging |
| from pathlib import Path |
| from typing import Union |
| from argparse import ArgumentParser |
| import cv2 |
| from PIL import Image |
|
|
| |
| from src.skin_analyzer import analyze_skin_function |
| from src.image import ImageBundle |
| import os |
| import numpy as np |
| import timeit |
|
|
| LOG = 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. |
| """ |
| |
| |
| start = timeit.default_timer() |
| image_bundle = ImageBundle(image_source=filename_or_url) |
| stop = timeit.default_timer() |
| print("load image time: ", stop - start) |
|
|
| |
| start = timeit.default_timer() |
| face_data = image_bundle.detect_faces_and_landmarks() |
| stop = timeit.default_timer() |
| print("detect faces and landmarks time: ", stop - start) |
| start = timeit.default_timer() |
| landmarks = image_bundle.detect_face_landmarks() |
| stop = timeit.default_timer() |
| print("detect face landmarks time: ", stop - start) |
|
|
| start = timeit.default_timer() |
| |
| segmentation_maps = image_bundle.segment_image() |
| stop = timeit.default_timer() |
| print("segment image time: ", stop - start) |
|
|
| analysis_results = {} |
| overlay_images = [] |
|
|
| image_np = image_bundle.numpy_image() |
| print("image_np shape: ", image_np.shape) |
| |
|
|
| if analyze_hair: |
| hair_mask = segmentation_maps["hair_mask"] |
| hair_analysis = analyze_hair_function(image_np, hair_mask) |
| analysis_results["hair_analysis"] = hair_analysis |
| overlay_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"] |
| if "filtered_skin_mask" in skin_analysis.keys(): |
| del skin_analysis["filtered_skin_mask"] |
| if "dominant_skin_tones" in skin_analysis.keys(): |
| del skin_analysis["dominant_skin_tones"] |
| analysis_results["skin_analysis"] = skin_analysis |
|
|
| |
|
|
| 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_analysis |
| overlay_images.append(segmentation_maps["right_eye_mask"]) |
|
|
| |
| if overlay_images: |
| combined_overlay = np.maximum.reduce(overlay_images) |
| else: |
| combined_overlay = image_np |
|
|
| |
| combined_overlay_bgr = cv2.cvtColor(combined_overlay, cv2.COLOR_RGB2BGR) |
|
|
| return combined_overlay_bgr, analysis_results |
|
|
|
|
| if __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 |
|
|
| |
| combined_overlay, analysis_results = process_image( |
| image_path, |
| analyze_skin=args.analyze_skin, |
| analyze_eyes=args.analyze_eyes, |
| analyze_hair=args.analyze_hair, |
| ) |
|
|
| |
| im_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 |
|
|
| |
| analysis_results["overlay_image_path"] = overlay_image_path |
| print(analysis_results) |
|
|
| with 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.") |
|
|