# import spaces # import os # import sys # import subprocess # import importlib.util # #Installazione silenziosa C++ # if importlib.util.find_spec("torch_cluster") is None: # print("Downloading precompiled torch-cluster...") # subprocess.check_call([ # sys.executable, "-m", "pip", "install", "torch-cluster", # "-f", "https://data.pyg.org/whl/torch-2.11.0+cu121.html", # "--no-build-isolation" # ]) # import os # import torch # import gradio as gr # from PIL import Image # from pathlib import Path # from huggingface_hub import hf_hub_download # import lightning as L # OUTPUT_DIR = Path("outputs") # OUTPUT_DIR.mkdir(exist_ok=True) # weight_file = "epoch=47-step=37544.ckpt" # if not os.path.exists(weight_file): # print(f"Downloading {weight_file} from cloud...") # hf_hub_download( # repo_id="FedericoGiulio/loomnet", # repo_type="model", # filename=weight_file, # local_dir="." # ) # GLOBAL_MODEL = None # @spaces.GPU(duration=120) # def run_demo(image_path, elevation, seed, without_loomnet): # global GLOBAL_MODEL # import hydra # from loomnet.utils import load_config # from inference import prepare_inputs # try: # L.seed_everything(int(seed), workers=True) # if GLOBAL_MODEL is None: # print("Initializing model...") # cfg = load_config("configs/baseline.yaml", cli_args=[]) # _original_load = torch.load # def _safe_load(*args, **kwargs): # kwargs['map_location'] = 'cpu' # kwargs['weights_only'] = False # return _original_load(*args, **kwargs) # torch.load = _safe_load # try: # GLOBAL_MODEL = hydra.utils.instantiate(cfg.system) # GLOBAL_MODEL.load_weights(weight_file) # finally: # torch.load = _original_load # GLOBAL_MODEL = GLOBAL_MODEL.to("cuda").eval() # # Bypass per immagini di esempio in Gradio # if "gradio" in image_path or "tmp" in image_path: # nome_file = os.path.basename(image_path) # percorso_reale = os.path.abspath(os.path.join("examples", nome_file)) # if os.path.exists(percorso_reale): # image_path = percorso_reale # data = prepare_inputs(image_path, elevation, "ele30") # for k, v in data.items(): # data[k] = v.unsqueeze(0).to("cuda") # if k not in ["k_near_indices"]: data[k] = data[k].float() # GLOBAL_MODEL.mv_model.without_loomnet = without_loomnet # with torch.no_grad(): # images_pred = GLOBAL_MODEL._generate_images(data) # views_folder = str(OUTPUT_DIR / "views") # os.makedirs(views_folder, exist_ok=True) # image_list = [] # for i, img in enumerate(images_pred[0]): # img_pil = Image.fromarray(img) # image_list.append(img_pil) # img_pil.save(os.path.join(views_folder, f"{i}.png")) # return image_list # except Exception as e_globale: # print(f"⚠️ ERRORE CRITICO INTERCETTATO NELLA FUNZIONE 2D: {e_globale}") # gr.Warning(f"Generazione fallita: {e_globale}") # return None # def run_demo_safe(image_path, elevation, seed, without_loomnet_in): # """Wrapper per proteggere il flusso nel caso in cui fallisca l'allocazione GPU""" # try: # return run_demo(image_path, elevation, seed, without_loomnet_in) # except Exception as e: # print(f"⚠️ ERRORE CRITICO A LIVELLO GPU/SCHEDULER (2D): {e}") # gr.Warning(f"Generazione fallita: {e}") # return None # with gr.Blocks() as demo: # with gr.Row(): # with gr.Column(): # img_in = gr.Image(type="filepath", label="Input Image", height=300) # # Blocco Esempi # examples_block = gr.Examples( # examples=[ # "examples/1.png", # "examples/2.jpg", # ], # inputs=img_in, # label="Or choose a sample image:" # ) # elevation_in = gr.Slider(-90, 90, value=12.0, label="Elevation of the input image") # seed_in = gr.Number(value=0, label="Seed") # without_loomnet_in = gr.Checkbox(label="Disable consistency mechanism (Useful for understanding LoomNet's contribution)", value=False) # btn = gr.Button("Generate", interactive=False) # with gr.Column(): # out_gallery = gr.Gallery( # label="Generated Views", # columns=4, # rows=4, # object_fit="contain", # height="auto" # ) # LOCK_OUTPUTS = [btn, img_in, elevation_in, seed_in, examples_block.dataset] # def blocca_interfaccia(): # return ( # gr.update(interactive=False), # btn # gr.update(interactive=False), # img_in # gr.update(interactive=False), # elevation_in # gr.update(interactive=False), # seed_in # gr.update(visible=False), # examples_block.dataset # ) # def sblocca_dopo_immagini(): # return ( # gr.update(interactive=True), # btn # gr.update(interactive=True), # img_in # gr.update(interactive=True), # elevation_in # gr.update(interactive=True), # seed_in # gr.update(visible=True), # examples_block.dataset # ) # img_in.change( # fn=lambda x: gr.update(interactive=(x is not None)), # inputs=[img_in], # outputs=[btn] # ) # btn.click( # fn=blocca_interfaccia, # inputs=[], # outputs=LOCK_OUTPUTS, # queue=False, # show_progress="hidden" # ).then( # fn=run_demo_safe, # inputs=[img_in, elevation_in, seed_in, without_loomnet_in], # outputs=[out_gallery] # ).then( # fn=sblocca_dopo_immagini, # inputs=[], # outputs=LOCK_OUTPUTS, # queue=False, # show_progress="hidden" # ) # if __name__ == "__main__": # demo.launch(share=True) import spaces import sys import subprocess import importlib.util #Installazione silenziosa C++ if importlib.util.find_spec("torch_cluster") is None: print("Downloading precompiled torch-cluster...") subprocess.check_call([ sys.executable, "-m", "pip", "install", "torch-cluster", "-f", "https://data.pyg.org/whl/torch-2.11.0+cu121.html", "--no-build-isolation" ]) import os if importlib.util.find_spec("rembg") is None: print("Installazione di rembg...") subprocess.check_call([ sys.executable, "-m", "pip", "install", "rembg", "onnxruntime-gpu" ]) import torch import gradio as gr from PIL import Image from pathlib import Path from huggingface_hub import hf_hub_download import lightning as L from rembg import remove, new_session import numpy as np OUTPUT_DIR = Path("outputs") OUTPUT_DIR.mkdir(exist_ok=True) _REMBG_SESSION = None def get_rembg_session(): global _REMBG_SESSION if _REMBG_SESSION is None: _REMBG_SESSION = new_session("birefnet-general") return _REMBG_SESSION def has_removed_background(image: Image.Image, threshold: float = 0.10) -> bool: """True se l'immagine ha già una porzione significativa di alpha trasparente.""" if image.mode != "RGBA": return False alpha = np.array(image.split()[-1]) return (alpha < 10).mean() > threshold def pad_to_square(image: Image.Image, fill=(0, 0, 0, 0)) -> Image.Image: """Aggiunge padding trasparente per rendere l'immagine quadrata, centrando il contenuto.""" width, height = image.size if width == height: return image side = max(width, height) squared = Image.new("RGBA", (side, side), fill) offset = ((side - width) // 2, (side - height) // 2) squared.paste(image, offset, image) return squared def remove_background_if_needed(image_path): """Rimuove lo sfondo (se presente) e rende l'immagine quadrata con padding trasparente.""" if not image_path: return image_path img = Image.open(image_path).convert("RGBA") already_clean = has_removed_background(img) already_square = (img.width == img.height) if already_clean and already_square: return image_path if not already_clean: img = remove(img, session=get_rembg_session()) img = pad_to_square(img) out_path = str(OUTPUT_DIR / "input_no_bg.png") img.save(out_path) return out_path weight_file = "epoch=47-step=37544.ckpt" if not os.path.exists(weight_file): print(f"Downloading {weight_file} from cloud...") hf_hub_download( repo_id="FedericoGiulio/loomnet", repo_type="model", filename=weight_file, local_dir="." ) GLOBAL_MODEL = None @spaces.GPU(duration=120) def run_demo(image_path, elevation, seed, without_loomnet): global GLOBAL_MODEL import hydra from loomnet.utils import load_config from inference import prepare_inputs try: L.seed_everything(int(seed), workers=True) if GLOBAL_MODEL is None: print("Initializing model...") cfg = load_config("configs/baseline.yaml", cli_args=[]) _original_load = torch.load def _safe_load(*args, **kwargs): kwargs['map_location'] = 'cpu' kwargs['weights_only'] = False return _original_load(*args, **kwargs) torch.load = _safe_load try: GLOBAL_MODEL = hydra.utils.instantiate(cfg.system) GLOBAL_MODEL.load_weights(weight_file) finally: torch.load = _original_load GLOBAL_MODEL = GLOBAL_MODEL.to("cuda").eval() # Bypass per immagini di esempio in Gradio if "gradio" in image_path or "tmp" in image_path: nome_file = os.path.basename(image_path) percorso_reale = os.path.abspath(os.path.join("examples", nome_file)) if os.path.exists(percorso_reale): image_path = percorso_reale data = prepare_inputs(image_path, elevation, "ele30") for k, v in data.items(): data[k] = v.unsqueeze(0).to("cuda") if k not in ["k_near_indices"]: data[k] = data[k].float() GLOBAL_MODEL.mv_model.without_loomnet = without_loomnet with torch.no_grad(): images_pred = GLOBAL_MODEL._generate_images(data) views_folder = str(OUTPUT_DIR / "views") os.makedirs(views_folder, exist_ok=True) image_list = [] for i, img in enumerate(images_pred[0]): img_pil = Image.fromarray(img) image_list.append(img_pil) img_pil.save(os.path.join(views_folder, f"{i}.png")) return image_list except Exception as e_globale: print(f"⚠️ ERRORE CRITICO INTERCETTATO NELLA FUNZIONE 2D: {e_globale}") gr.Warning(f"Generazione fallita: {e_globale}") return None def run_demo_safe(image_path, elevation, seed, without_loomnet_in): """Wrapper per proteggere il flusso nel caso in cui fallisca l'allocazione GPU""" try: return run_demo(image_path, elevation, seed, without_loomnet_in) except Exception as e: print(f"⚠️ ERRORE CRITICO A LIVELLO GPU/SCHEDULER (2D): {e}") gr.Warning(f"Generazione fallita: {e}") return None custom_css = """ #img-in-box.processing img { opacity: 0.4; animation: pulse-processing 1.2s ease-in-out infinite; } @keyframes pulse-processing { 0%, 100% { opacity: 0.4; } 50% { opacity: 0.85; } } """ with gr.Blocks() as demo: with gr.Row(): with gr.Column(): img_in = gr.Image( type="filepath", label="Input Image", height=300, elem_id="img-in-box", image_mode=None, # RGBA inferito solo per i PNG, RGB per gli altri formati format="png" ) # Blocco Esempi examples_block = gr.Examples( examples=[ "examples/1.png", "examples/2.jpg", #elevation 12 "examples/3.png", "examples/4.png", "examples/6.webp", "examples/7.webp", "examples/8.jpg", "examples/9.jpg", ], inputs=img_in, label="Or choose a sample image:" ) elevation_in = gr.Slider(-90, 90, value=12.0, label="Elevation of the input image") seed_in = gr.Number(value=0, label="Seed") without_loomnet_in = gr.Checkbox(label="Disable consistency mechanism (Useful for understanding LoomNet's contribution)", value=False) btn = gr.Button("Generate", interactive=False) with gr.Column(): out_gallery = gr.Gallery( label="Generated Views", columns=4, rows=4, object_fit="contain", height="auto" ) LOCK_OUTPUTS = [btn, img_in, elevation_in, seed_in, examples_block.dataset] def blocca_interfaccia(): return ( gr.update(interactive=False), # btn gr.update(interactive=False), # img_in gr.update(interactive=False), # elevation_in gr.update(interactive=False), # seed_in gr.update(visible=False), # examples_block.dataset ) def sblocca_dopo_immagini(): return ( gr.update(interactive=True), # btn gr.update(interactive=True), # img_in gr.update(interactive=True), # elevation_in gr.update(interactive=True), # seed_in gr.update(visible=True), # examples_block.dataset ) img_in.upload( fn=lambda: ( gr.update(interactive=False), gr.update(label="Removing background...", elem_classes=["processing"]) ), inputs=[], outputs=[btn, img_in], queue=False ).then( fn=remove_background_if_needed, inputs=[img_in], outputs=[img_in], show_progress="hidden" ).then( fn=lambda p: ( gr.update(interactive=(p is not None)), gr.update(label="Input Image", elem_classes=[]) ), inputs=[img_in], outputs=[btn, img_in], queue=False ) examples_block.load_input_event.then( fn=lambda: ( gr.update(interactive=False), gr.update(label="Rimozione sfondo in corso...", elem_classes=["processing"]) ), inputs=[], outputs=[btn, img_in], queue=False ).then( fn=remove_background_if_needed, inputs=[img_in], outputs=[img_in], show_progress="hidden" ).then( fn=lambda p: ( gr.update(interactive=(p is not None)), gr.update(label="Input Image", elem_classes=[]) ), inputs=[img_in], outputs=[btn, img_in], queue=False ) btn.click( fn=blocca_interfaccia, inputs=[], outputs=LOCK_OUTPUTS, queue=False, show_progress="hidden" ).then( fn=run_demo_safe, inputs=[img_in, elevation_in, seed_in, without_loomnet_in], outputs=[out_gallery] ).then( fn=sblocca_dopo_immagini, inputs=[], outputs=LOCK_OUTPUTS, queue=False, show_progress="hidden" ) if __name__ == "__main__": demo.launch(share=True, css=custom_css)