Skip to content
Snippets Groups Projects
Select Git revision
  • 982dfe05afe21874bba70a098015d63f4f122985
  • master default protected
  • results
  • GDP_4.4.7
  • GDP_4.2.1
  • GDP_4.2.6
6 results

GUI.py

Blame
  • user avatar
    tee1g21 authored
    982dfe05
    History
    GUI.py 6.91 KiB
    import tkinter as tk
    import tkinter.filedialog
    import subprocess
    import sys
    import time
    from threading import Thread
    import shutil
    import os
    
    # Get the directory of the current script
    SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
    # Get the root directory (AVVR-Pipeline-Internship)
    ROOT_DIR = os.path.dirname(SCRIPT_DIR)
    
    file_path = None
    createDepth = "0"
    
    def shift_image_selection():
        # This function can be used if you want to perform any action when the checkbox is clicked
        pass
    
    def copy_intermediary_outputs():
        source_folder = os.path.join(ROOT_DIR, "edgenet-360", "Data", "Input")
        destination_folder = os.path.join(ROOT_DIR, "edgenet-360", "Output")
        files_to_copy = ["depth_e.png", "enhanced_depth_e.png", "material.png", "rgb.png"]
        
        for file_name in files_to_copy:
            source_path = os.path.join(source_folder, file_name)
            destination_path = os.path.join(destination_folder, file_name)
            try:
                shutil.copy(source_path, destination_path)
                print(f"Copied {file_name} to {destination_folder}")
            except FileNotFoundError:
                print(f"Warning: {file_name} not found in {source_folder}")
    
    def select_Image(event):
        global file_path 
        file_path = tkinter.filedialog.askopenfilename()
        file_path = os.path.normpath(file_path)
        select_button.configure(text="Selected", bg="red")
        label.configure(text="Image is selected. Press run to create scene.")
    
    def depthmap_creation():
        print("Manually upload depth map: ", uploadDepthCheck.get())
    
        if uploadDepthCheck.get() == 1: # if manually upload checked
            check.set(0) # disable auto generation of depth map        
            upload_depth_path = tkinter.filedialog.askopenfilename(title="Select a depth map", filetypes=[("All files", "*.*")]) #dialog box to upload dpeth map
            
            if upload_depth_path:
                print(f"Uploaded depth map: {upload_depth_path}")            
                #TODO implement Mona's monodepth upload 
            else:
                print("No depth map selected")
                check.set(1) # if no depth map selected, enable auto generation of depth map  
                depth_check.deselect() # uncheck the depth map check box
        else:
            check.set(1) # if manually upload unchecked, enable auto generation of depth map 
            upload_depth_path = None 
            print("Removed uploaded depth map")     
            
    def stanfordRoom_selection():
        if checkStanford.get() == 1:
            global stanford_frame
            stanford_frame = tk.Frame(window)
            stanford_frame.pack(fill=tk.X, padx=5, pady=5)
            global labelRoomArea
            labelRoomArea = tk.Label(stanford_frame, text="Please Input Room Area: ")
            labelRoomArea.pack(side="left")
            global stanford_text
            stanford_text = tk.Entry(stanford_frame)
            stanford_text.pack(side="left", fill=tk.X, expand=True)
        else:
            stanford_frame.pack_forget()
    
        select_button.pack(side="top", fill=tk.X, expand=True, padx=5, pady=5)
        run_button.pack(side="top", fill=tk.X, expand=True, padx=5, pady=5)
    
    def run_Image(event):
        if checkStanford.get() == 0:
            label.configure(text="Pipeline is running. Creating scene...", height=15)
        else:
            label.configure(text="Pipeline is running for Stanford2D3D dataset. Creating scene...", height=15)
            labelRoomArea.configure(text="Room Area Running : ")
            stanford_text.configure(state="disabled")
    
        select_button.pack_forget()
        run_button.pack_forget()
        depth_check.pack_forget()
        include_top_check.pack_forget()
        stanford_check.pack_forget()
        shift_image_check.pack_forget()
        threading()
    
    def runProcess():
        global file_path
        include_top_option = "y" if include_top.get() == 1 else ""
        shift_image_option = "y" if shift_image.get() == 1 else ""
        
        try:
            if checkStanford.get() == 0:
                combined_bat = os.path.join(SCRIPT_DIR, "combined.bat")
                print(f"Attempting to run: {combined_bat}")
                print(f"With arguments: {file_path}, {str(check.get())}, {include_top_option}, {shift_image_option}")
                
                # depth map check
                if check.get() == 1: 
                    print("Auto depth map")
                else: 
                    print("Manual depth map")
                
                p = subprocess.Popen(
                    [combined_bat, file_path, str(check.get()), include_top_option, shift_image_option],
                    stdout=sys.stdout)
                p.communicate()
    
            else:
                temp = os.path.split(file_path)
                suffices = temp[-1].split("_")
                camera_pos = str(suffices[1])
                room_name = suffices[2] + "_" + suffices[3]
                room_area = stanford_text.get()
    
                print(room_area, room_name, camera_pos)
                combined_stanford_bat = os.path.join(SCRIPT_DIR, "combined_stanford.bat")
                p = subprocess.Popen( 
                    [combined_stanford_bat, file_path, camera_pos, str(room_area), room_name],
                    stdout=sys.stdout)
                p.communicate()
    
            copy_intermediary_outputs()
            label.configure(text="Pipeline execution complete, check output folder.")
        
        except Exception as e:
            print(f"An error occurred: {e}")
            label.configure(text=f"An error occurred: {e}")
    
        try:
            labelRoomArea.pack_forget()
            stanford_text.pack_forget()
        except Exception as e:
            print(e)
    
    def threading():
        thread1 = Thread(target=runProcess)
        thread1.start()
    
    window = tk.Tk()
    window.title("Immersive VR scene creator")
    
    check = tk.IntVar()
    check.set(1) #automatically generate depth map as default
    uploadDepthCheck = tk.IntVar() # added uploadDepthCheck vaiable: 0 = automatically upload depth map, 1 = manually upload depth map
    
    checkStanford = tk.IntVar()
    include_top = tk.IntVar()
    shift_image = tk.IntVar()
    label = tk.Label(
        text="Please Input a RGB image for scene creation",
        foreground="black", 
        background="white",
        width=50,
        height=10,
    )
    
    select_button = tk.Button(
        text="Select",
        width=50,
        height=5,
        bg="green",
        fg="white",
    )
    
    run_button = tk.Button(
        text="Run",
        width=50,
        height=5,
        bg="green",
        fg="white",
    )
    depth_check = tk.Checkbutton(window, text='Upload a depth map(360 MonoDepth)',variable=uploadDepthCheck, onvalue=1, offvalue=0, command=depthmap_creation)
    stanford_check = tk.Checkbutton(window, text='Run for stanford2D3D dataset',variable=checkStanford, onvalue=1, offvalue=0,command=stanfordRoom_selection )
    include_top_check = tk.Checkbutton(window, text='Include Top in Mesh', variable=include_top, onvalue=1, offvalue=0)
    shift_image_check = tk.Checkbutton(window, text='Shift input image', variable=shift_image, onvalue=1, offvalue=0, command=shift_image_selection)
    label.pack()
    depth_check.pack()
    stanford_check.pack()
    include_top_check.pack()
    shift_image_check.pack()
    select_button.pack()
    run_button.pack()
    
    select_button.bind('<Button-1>', select_Image)
    run_button.bind('<Button-1>', run_Image)
    
    window.mainloop()