|
import gradio as gr |
|
import torch |
|
import numpy as np |
|
from diffusers import StableDiffusionXLImg2ImgPipeline |
|
from transformers import DPTFeatureExtractor, DPTForDepthEstimation |
|
from PIL import Image, ImageEnhance, ImageOps |
|
|
|
|
|
device = "cpu" |
|
torch_dtype = torch.float32 |
|
|
|
print("Carregando modelo SDXL Img2Img...") |
|
pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained( |
|
"stabilityai/stable-diffusion-xl-base-1.0", |
|
torch_dtype=torch_dtype |
|
).to(device) |
|
|
|
print("Carregando pesos LoRA weights with PEFT...") |
|
pipe.load_lora_weights( |
|
"KappaNeuro/bas-relief", |
|
weight_name="BAS-RELIEF.safetensors", |
|
peft_backend="peft" |
|
) |
|
|
|
print("Carregando modelo de profundidade...") |
|
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large") |
|
depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large").to(device) |
|
|
|
|
|
def processar_profundidade(depth_arr: np.ndarray) -> Image.Image: |
|
d_min, d_max = depth_arr.min(), depth_arr.max() |
|
depth_stretched = (depth_arr - d_min) / (d_max - d_min + 1e-8) |
|
depth_stretched = (depth_stretched * 255).astype(np.uint8) |
|
|
|
depth_pil = Image.fromarray(depth_stretched) |
|
depth_pil = ImageOps.autocontrast(depth_pil) |
|
|
|
enhancer = ImageEnhance.Sharpness(depth_pil) |
|
depth_pil = enhancer.enhance(2.0) |
|
|
|
return depth_pil |
|
|
|
|
|
def processar_imagem(imagem: Image.Image): |
|
|
|
imagem = imagem.convert("RGB").resize((512, 512)) |
|
|
|
|
|
with torch.inference_mode(): |
|
resultado = pipe( |
|
prompt="BAS-RELIEF", |
|
image=imagem, |
|
strength=0.7, |
|
num_inference_steps=20, |
|
guidance_scale=7.5 |
|
) |
|
|
|
|
|
inputs = feature_extractor(resultado.images[0], return_tensors="pt").to(device) |
|
with (torch.no_grad()): |
|
outputs = depth_model(**inputs) |
|
predicted_depth = outputs.predicted_depth |
|
|
|
depth_map = torch.nn.functional.interpolate( |
|
predicted_depth.unsqueeze(1), |
|
size=imagem.size[::-1], |
|
mode="bicubic", |
|
align_corners=False |
|
).squeeze().cpu().numpy() |
|
|
|
return resultado.images[0], processar_profundidade(depth_map) |
|
|
|
|
|
|
|
interface = gr.Interface( |
|
fn=processar_imagem, |
|
inputs=gr.Image(type="pil"), |
|
outputs=[gr.Image(label="Resultado"), gr.Image(label="Profundidade")], |
|
title="Conversor para Baixo-relevo", |
|
description="Transforme imagens em baixo-relevo com mapa de profundidade" |
|
) |
|
|
|
if __name__ == "__main__": |
|
interface.launch() |