File size: 2,314 Bytes
6202025
 
 
 
690b5d3
6202025
 
 
690b5d3
 
 
 
 
 
 
 
 
 
6202025
 
690b5d3
 
 
6202025
690b5d3
 
6202025
690b5d3
 
6202025
690b5d3
 
 
 
6202025
690b5d3
 
6202025
690b5d3
 
 
6202025
690b5d3
 
6202025
690b5d3
 
 
 
 
 
 
6202025
690b5d3
 
 
 
 
6202025
 
690b5d3
6202025
 
 
 
 
 
 
 
 
 
 
 
 
690b5d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import gradio as gr
from PIL import Image
import torch
from diffusers import StableDiffusionInpaintPipeline
import traceback

device = "cpu"

try:
    MODEL_ID = "runwayml/stable-diffusion-inpainting"
    pipe = StableDiffusionInpaintPipeline.from_pretrained(
        MODEL_ID,
        torch_dtype=torch.float32
    )
    pipe = pipe.to(device)
except Exception as e:
    print("Model yüklenirken hata oluştu:", e)
    traceback.print_exc()

def tryon(person_img, cloth_img, prompt="A photo of the person wearing the selected clothing, realistic, photorealistic"):
    try:
        if person_img is None or cloth_img is None:
            return None

        person = person_img.convert("RGB")
        cloth = cloth_img.convert("RGBA")

        pw, ph = person.size
        cw, ch = cloth.size

        scale = 0.6 * pw / cw
        new_w = max(10, int(cw * scale))
        new_h = max(10, int(ch * scale))
        cloth_resized = cloth.resize((new_w, new_h), Image.LANCZOS)

        alpha = cloth_resized.split()[-1]
        mask = Image.new("L", (pw, ph), 0)

        x = int((pw - new_w) / 2)
        y = int(ph * 0.18)
        mask.paste(alpha, (x, y))

        composite = person.copy()
        composite.paste(cloth_resized, (x, y), cloth_resized)

        result = pipe(
            prompt=prompt,
            image=composite,
            mask_image=mask,
            guidance_scale=7.5,
            num_inference_steps=30
        ).images[0]

        return result
    except Exception as e:
        print("Giydirme sırasında hata oluştu:", e)
        traceback.print_exc()
        return None

with gr.Blocks() as demo:
    gr.Markdown("## 👕 AI Kıyafet Giydirme\\nFotoğrafını ve kıyafet resmini yükle, sonucu gör!")

    with gr.Row():
        with gr.Column():
            person_in = gr.Image(type="pil", label="Kullanıcı Fotoğrafı")
            cloth_in = gr.Image(type="pil", label="Kıyafet Fotoğrafı (PNG, şeffaf arka plan)")
            prompt_in = gr.Textbox(label="Prompt (opsiyonel)", placeholder="Açıklama ekleyebilirsin...")
            btn = gr.Button("Giydir")
        with gr.Column():
            out = gr.Image(type="pil", label="Sonuç")

    btn.click(fn=tryon, inputs=[person_in, cloth_in, prompt_in], outputs=out)

if __name__ == "__main__":
    demo.launch()