Views
No views yet
Transform your selfies into beautiful cartoon avatars using state-of-the-art conditional diffusion models!
1# Install required packages
2pip install torch torchvision torchaudio
3pip install diffusers transformers accelerate
4pip install mediapipe opencv-python pillow numpy1from cartoon_diffusion import CartoonDiffusionPipeline
2
3# Initialize pipeline
4pipeline = CartoonDiffusionPipeline.from_pretrained("wizcodes12/image_to_cartoonify")
5
6# Generate cartoon from selfie
7cartoon = pipeline("path/to/your/selfie.jpg")
8cartoon.save("cartoon_output.png")1# Custom attribute control
2cartoon = pipeline(
3 "selfie.jpg",
4 hair_color=0.8, # Lighter hair
5 glasses=0.9, # Add glasses
6 facial_hair=0.2, # Minimal facial hair
7 num_inference_steps=50,
8 guidance_scale=7.5
9)OptimizedConditionedUNet
├── Time Embedding (224 → 448 dims)
├── Attribute Embedding (18 → 448 dims)
├── Encoder (4 down-sampling blocks)
│ ├── 56 → 112 channels
│ ├── 112 → 224 channels
│ ├── 224 → 448 channels
│ └── 448 → 448 channels
├── Bottleneck (Attribute Injection)
└── Decoder (4 up-sampling blocks)
├── 448 → 448 channels
├── 448 → 224 channels
├── 224 → 112 channels
└── 112 → 56 channels| Attribute | Range | Description |
|---|---|---|
eye_angle | 0-2 | Angle/tilt of eyes |
eye_lashes | 0-1 | Eyelash prominence |
eye_lid | 0-1 | Eyelid visibility |
chin_length | 0-2 | Chin length/prominence |
eyebrow_weight | 0-1 | Eyebrow thickness |
eyebrow_shape | 0-13 | Eyebrow curvature |
eyebrow_thickness | 0-3 | Eyebrow density |
face_shape | 0-6 | Overall face shape |
facial_hair | 0-14 | Facial hair presence |
hair | 0-110 | Hair style/volume |
eye_color | 0-4 | Eye color tone |
face_color | 0-10 | Skin tone |
hair_color | 0-9 | Hair color |
glasses | 0-11 | Glasses presence/style |
glasses_color | 0-6 | Glasses color |
eye_slant | 0-2 | Eye slant angle |
eyebrow_width | 0-2 | Eyebrow width |
eye_eyebrow_distance | 0-2 | Distance between eyes and eyebrows |
L = ||ε - ε_θ(x_t, t, c)||²ε is the ground truth noiseε_θ is the predicted noisex_t is the noisy image at timestep tc is the conditioning vector (facial attributes)| Metric | Value |
|---|---|
| Final Training Loss | 0.0234 |
| Best Validation Loss | 0.0251 |
| Parameters | ~50M |
| Inference Time (GPU) | 2-3 seconds |
| Inference Time (CPU) | 15-30 seconds |
| Memory Usage (GPU) | 4GB |
| Memory Usage (CPU) | 2GB |
1import torch
2from pathlib import Path
3
4# Process multiple selfies
5selfie_dir = Path("input_selfies/")
6output_dir = Path("cartoon_outputs/")
7
8for selfie_path in selfie_dir.glob("*.jpg"):
9 cartoon = pipeline(str(selfie_path))
10 cartoon.save(output_dir / f"cartoon_{selfie_path.stem}.png")1# Create variations with different attributes
2base_image = "selfie.jpg"
3variations = [
4 {"hair_color": 0.2, "name": "dark_hair"},
5 {"hair_color": 0.8, "name": "light_hair"},
6 {"glasses": 0.9, "name": "with_glasses"},
7 {"facial_hair": 0.7, "name": "with_beard"}
8]
9
10for variation in variations:
11 name = variation.pop("name")
12 cartoon = pipeline(base_image, **variation)
13 cartoon.save(f"cartoon_{name}.png")1import gradio as gr
2
3def generate_cartoon(image, hair_color, glasses, facial_hair):
4 return pipeline(
5 image,
6 hair_color=hair_color,
7 glasses=glasses,
8 facial_hair=facial_hair
9 )
10
11# Create Gradio interface
12interface = gr.Interface(
13 fn=generate_cartoon,
14 inputs=[
15 gr.Image(type="pil"),
16 gr.Slider(0, 1, value=0.5, label="Hair Color"),
17 gr.Slider(0, 1, value=0.0, label="Glasses"),
18 gr.Slider(0, 1, value=0.0, label="Facial Hair")
19 ],
20 outputs=gr.Image(type="pil"),
21 title="Cartoon Generator"
22)
23
24interface.launch()1# Analyze facial features from input image
2features = pipeline.extract_features("selfie.jpg")
3print("Detected facial attributes:")
4for i, attr_name in enumerate(pipeline.attribute_names):
5 print(f"{attr_name}: {features[i]:.3f}")__init__(model_path, device='auto')__call__(image, **kwargs)image (str|PIL.Image): Input selfie imagenum_inference_steps (int, default=50): Number of denoising stepsguidance_scale (float, default=7.5): Classifier-free guidance scalegenerator (torch.Generator, optional): Random number generator**attribute_kwargs: Override specific facial attributesPIL.Image: Generated cartoon imageextract_features(image)image (str|PIL.Image): Input imagetorch.Tensor: 18-dimensional feature vector1git clone https://github.com/wizcodes12/image_to_cartoonify
2cd image_to_cartoonify
3pip install -e .
4pip install -r requirements-dev.txtpytest tests/1@misc{image_to_cartoonify_2024,
2 title={Image to Cartoonify: Selfie to Cartoon Generator},
3 author={wizcodes12},
4 year={2024},
5 howpublished={\url{https://huggingface.co/wizcodes12/image_to_cartoonify}},
6 note={Accessed: \today}
7}