#🔒 Need Help with NameError: name 'torch' is not defined

9 messages · Page 1 of 1 (latest)

forest nimbus
#

just wanted to use Dreamgaussian local in my Python and then yh this error keeps coming even after gettin python 3.11, 3.8 and different torch version help plsplsplspls

gleaming wigeonBOT
#

@forest nimbus

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

forest nimbus
#

Traceback (most recent call last):
File "main.py", line 92, in <module>
gui = GUI(opt)
File "main.py", line 25, in init
self.renderer = Renderer(sh_degree=self.opt.sh_degree)
File "C:\Users\emina\OneDrive\Dokumente\dreamgaussian-app\dreamgaussian\gs_renderer.py", line 16, in init
self.device = torch.device("cpu") # CPU-Only Einstellung
NameError: name 'torch' is not defined

#

i want to use a picture to generate a 3d model of it and i expect to yk get an obj to the output folder but yh this is the error i got

#

import torch
import os
import cv2
import numpy as np
import rembg
import torch.nn.functional as F
from gs_renderer import Renderer

Dummy Gaussians-Klasse

class Gaussians:
def init(self):
self.points = None
self.colors = None

def extract_mesh(self, path, density_thresh):
    print(f"[INFO] Extracting mesh and saving to {path} with density {density_thresh}")
    # Hier Mesh speichern oder simulieren

class GUI:
def init(self, opt):
self.opt = opt
self.W = opt.W
self.H = opt.H
self.device = torch.device("cpu") # CPU-only
self.renderer = Renderer(sh_degree=self.opt.sh_degree)

    self.input_img = None
    self.input_mask = None
    self.input_img_torch = None
    self.input_mask_torch = None

    # **Wichtig**: Initialisiere bg_remover hier
    self.bg_remover = rembg.new_session()  # Initialize the background remover

    # Bild laden und vorbereiten
    if self.opt.input is not None:
        self.load_input(self.opt.input)

    # Initialisiere Rendering
    self.renderer.initialize(num_pts=self.opt.num_pts)

def load_input(self, file):
    print(f'[INFO] Load image from {file}...')
    img = cv2.imread(file, cv2.IMREAD_UNCHANGED)
    if img.shape[-1] == 3:
        # Der Hintergrund-Entferner wird hier verwendet
        img = rembg.remove(img, session=self.bg_remover)

    img = cv2.resize(img, (self.W, self.H), interpolation=cv2.INTER_AREA)
    img = img.astype(np.float32) / 255.0

    self.input_mask = img[..., 3:]
    self.input_img = img[..., :3] * self.input_mask + (1 - self.input_mask)
    self.input_img = self.input_img[..., ::-1].copy()

    # Falls die _caption.txt nicht existiert, setze einen Standardtext
    self.prompt = "Default description or prompt text"

def save_output(self, output_path):
    # Stelle sicher, dass der Ordner existiert
    os.makedirs(output_path, exist_ok=True)

    # 3D Modell speichern
    if self.renderer.gaussians:  # Falls `gaussians` existiert
        mesh_output_path = os.path.join(output_path, "generated_model.obj")
        self.renderer.gaussians.extract_mesh(mesh_output_path, self.opt.density_thresh)
        print(f"[INFO] Model saved to {mesh_output_path}.")
    else:
        print("[ERROR] gaussians not initialized properly.")

def process_image(self):
    # Bildverarbeitung durchführen
    self.load_input(self.opt.input)

    # Ergebnis im gewünschten Ordner speichern
    self.save_output(self.opt.outdir)

Beispiel Aufruf:

if name == "main":
class Opt:
# Beispiel-Parameter, du kannst sie nach deinen Bedürfnissen anpassen
input = r"C:\Users\emina\OneDrive\Dokumente\dreamgaussian-app\static\uploads\EliteDeals_Banner_mit_Blauakzenten.png"
outdir = r"C:\Users\emina\OneDrive\Dokumente\dreamgaussian-app\static\models"
W = 512
H = 512
num_pts = 100000
sh_degree = 2
density_thresh = 0.01

# GUI Instanz erstellen und Bild verarbeiten
opt = Opt()
gui = GUI(opt)
gui.process_image()

is my used code

brisk loom
#

firstly you want to format your code better

#

did you try pip install torch

gleaming wigeonBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.