Skip to content
Snippets Groups Projects
Commit a2a126e7 authored by mhby1g21's avatar mhby1g21
Browse files

dynamic dir changes and no need to use path.txt no more, added new folder to gitignore

parent ace4a01e
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ data/matbase/masks/* ...@@ -6,7 +6,7 @@ data/matbase/masks/*
# intermediate files # intermediate files
output/ output/
split_output/ cubemap_faces/
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/
......
import numpy as np import numpy as np
import imageio import imageio
import os import os
import sys
from ui import get_res from ui import get_res
#makes sure value of x stays within the range of min and max value to prevent out of bound values accses for the image #makes sure value of x stays within the range of min and max value to prevent out of bound values accses for the image
...@@ -9,29 +10,11 @@ def clamp(x, min_val, max_val): ...@@ -9,29 +10,11 @@ def clamp(x, min_val, max_val):
#clamps the floating point coordinates to nearest integer value, copies pixel value from the image to the nearest int coord #clamps the floating point coordinates to nearest integer value, copies pixel value from the image to the nearest int coord
def nearest_neighbour_interpolation(img, x, y): def nearest_neighbour_interpolation(img, x, y):
h, w, _ = img.shape # Modified to handle both 2D and 3D images
h, w = img.shape[:2]
x, y = clamp(int(x), 0, w-1), clamp(int(y), 0, h-1) x, y = clamp(int(x), 0, w-1), clamp(int(y), 0, h-1)
return img[y, x] return img[y, x]
# def orientation_to_face(x, y, z):
# abs_x, abs_y, abs_z = abs(x), abs(y), abs(z)
# if abs_x >= abs_y and abs_x >= abs_z:
# if x > 0:
# return 'front', -y / abs_x, -z / abs_x
# else:
# return 'back', y / abs_x, -z / abs_x
# elif abs_y >= abs_x and abs_y >= abs_z:
# if y > 0:
# return 'right', -x / abs_y, -z / abs_y
# else:
# return 'left', x / abs_y, -z / abs_y
# else:
# if z > 0:
# return 'top', x / abs_z, y / abs_z
# else:
# return 'bottom', -x / abs_z, y / abs_z
#maps the 3d coords for cube faces to 2d coords on that cube face #maps the 3d coords for cube faces to 2d coords on that cube face
#finds out which cube face corresponds to the current pixel for which the 3d coords are calculated and calcs the normalised 2d coords on that face #finds out which cube face corresponds to the current pixel for which the 3d coords are calculated and calcs the normalised 2d coords on that face
def orientation_to_face(x, y, z): def orientation_to_face(x, y, z):
...@@ -54,6 +37,13 @@ def orientation_to_face(x, y, z): ...@@ -54,6 +37,13 @@ def orientation_to_face(x, y, z):
#converts cube maps into omnidirectional image #converts cube maps into omnidirectional image
def cubemap_to_omnidirectional(cube_faces, out_width, out_height): def cubemap_to_omnidirectional(cube_faces, out_width, out_height):
# Determine if the input is grayscale or RGB
is_grayscale = len(cube_faces[list(cube_faces.keys())[0]].shape) == 2
# Create the appropriate output array based on input type
if is_grayscale:
omnidirectional = np.zeros((out_height, out_width), dtype=np.uint8)
else:
omnidirectional = np.zeros((out_height, out_width, 3), dtype=np.uint8) omnidirectional = np.zeros((out_height, out_width, 3), dtype=np.uint8)
#iterates through the pixels in o/p image, for each pixel calulates spherical coord used to map 2d pixel loc to 3d points on a sphere #iterates through the pixels in o/p image, for each pixel calulates spherical coord used to map 2d pixel loc to 3d points on a sphere
...@@ -79,23 +69,27 @@ def cubemap_to_omnidirectional(cube_faces, out_width, out_height): ...@@ -79,23 +69,27 @@ def cubemap_to_omnidirectional(cube_faces, out_width, out_height):
return omnidirectional return omnidirectional
# if __name__ == "__main__": if __name__ == "__main__":
# # Load the cubemap images # Get the directory of the current script
# cube_faces_dir = input("Enter the directory containing the cubemap images: ").strip() script_dir = os.path.dirname(os.path.abspath(__file__))
# faces = ["right", "left", "top", "bottom", "front", "back"]
# cube_faces = {} # Default paths relative to the script directory
default_cube_faces_dir = os.path.join(script_dir, "output", "cubemap_faces")
# for face in faces: default_output_dir = os.path.join(script_dir, "..", "edgenet-360", "Data", "Input")
# cube_faces[face] = imageio.imread(os.path.join(cube_faces_dir, f"{face}.jpg")) rgb_png_path = os.path.join(script_dir, "..", "scripts", "360monodepthexecution", "rgb.jpg")
# Use command-line arguments for custom paths if provided
if len(sys.argv) > 2:
cube_faces_dir = sys.argv[1]
output_dir = sys.argv[2]
else:
cube_faces_dir = default_cube_faces_dir
output_dir = default_output_dir
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
if __name__ == "__main__":
# Load the cubemap images # Load the cubemap images
#cube_faces_dir = input("Enter the directory containing the cubemap images: ").strip()
cube_faces_dir = "C:\Project\AVVR-Pipeline-Internship\material_recognition\Dynamic-Backward-Attention-Transformer\output\split_output"
#faces = ["right", "left", "top", "bottom", "front", "back"]
faces = ["rightrgb", "leftrgb", "toprgb", "bottomrgb", "frontrgb", "backrgb"] faces = ["rightrgb", "leftrgb", "toprgb", "bottomrgb", "frontrgb", "backrgb"]
cube_faces = {} cube_faces = {}
...@@ -103,39 +97,23 @@ if __name__ == "__main__": ...@@ -103,39 +97,23 @@ if __name__ == "__main__":
image_path = os.path.join(cube_faces_dir, f"{face}.png") image_path = os.path.join(cube_faces_dir, f"{face}.png")
image_data = imageio.imread(image_path) image_data = imageio.imread(image_path)
#rotate top and bottom face by 90 deg
# if face in ["top", "bottom"]:
# image_data = np.rot90(image_data, 1)
# #flip the top, bottom, front and back faces in horizontal direction
# if face not in ["left", "right"]:
# image_data = image_data[:, ::-1]
if face in ["toprgb", "bottomrgb"]: if face in ["toprgb", "bottomrgb"]:
image_data = np.rot90(image_data, 1) image_data = np.rot90(image_data, 1)
if face not in ["leftrgb", "rightrgb"]: if face not in ["leftrgb", "rightrgb"]:
image_data = image_data[:, ::-1] image_data = image_data[:, ::-1]
cube_faces[face] = image_data cube_faces[face] = image_data
# Use the rgb.png file from edgenet-360 folder for dimensions
# output_width = int(input("Enter output omnidirectional width: ")) height, width = get_res(rgb_png_path)
# output_height = int(input("Enter output omnidirectional height: ")) print(f"Using dimensions from {rgb_png_path}: {width}x{height}")
with open('path.txt', 'r') as file:
input_path = file.readline()
print(f'path = {input_path}')
os.remove('path.txt')
height, width = get_res(input_path)
print(height, width)
output_width = width output_width = width
output_height = height output_height = height
#print(f"height: {height}, width: {width}")
omnidirectional_img = cubemap_to_omnidirectional(cube_faces, output_width, output_height) omnidirectional_img = cubemap_to_omnidirectional(cube_faces, output_width, output_height)
output_path = "C:\Project\AVVR-Pipeline-Internship\edgenet360\Data\Input\material.png" output_path = os.path.join(output_dir, "material.png")
imageio.v2.imsave(output_path, omnidirectional_img) imageio.v2.imsave(output_path, omnidirectional_img)
print(f"Omnidirectional image saved to {output_path}") print(f"Omnidirectional image saved to {output_path}")
###### This code has been Referenced from: https://github.com/jaxry/panorama-to-cubemap/blob/gh-pages/convert.js ###### This code has been Referenced from: https://github.com/jaxry/panorama-to-cubemap/blob/gh-pages/convert.js
import numpy as np import numpy as np
import imageio import imageio
import os import os
...@@ -16,7 +15,7 @@ def mod(x, n): ...@@ -16,7 +15,7 @@ def mod(x, n):
return ((x % n) + n) % n return ((x % n) + n) % n
#clamps the floating point coordinates to nearest integer value, copies pixel value from the image to the nearest int coord #clamps the floating point coordinates to nearest integer value, copies pixel value from the image to the nearest int coord
def nearest_neigbour_interpolation(img, x, y): def nearest_neighbour_interpolation(img, x, y):
h, w, _ = img.shape h, w, _ = img.shape
x, y = clamp(int(x), 0, w-1), clamp(int(y), 0, h-1) x, y = clamp(int(x), 0, w-1), clamp(int(y), 0, h-1)
return img[y, x] return img[y, x]
...@@ -49,56 +48,45 @@ def face_rendering(img, face, face_size): ...@@ -49,56 +48,45 @@ def face_rendering(img, face, face_size):
longitude = mod(np.arctan2(out[1], out[0]), 2 * np.pi) longitude = mod(np.arctan2(out[1], out[0]), 2 * np.pi)
latitude = np.arccos(out[2] / r) latitude = np.arccos(out[2] / r)
s_x, s_y = img.shape[1] * longitude / (2 * np.pi) - 0.5, img.shape[0] * latitude / np.pi - 0.5 s_x, s_y = img.shape[1] * longitude / (2 * np.pi) - 0.5, img.shape[0] * latitude / np.pi - 0.5
out_face[y, x] = nearest_neigbour_interpolation(img, s_x, s_y) out_face[y, x] = nearest_neighbour_interpolation(img, s_x, s_y)
return out_face return out_face
#generates 6 cube faces #generates 6 cube faces
def generate_cube_faces(input_path, output_path="cube_faces_output"): def generate_cube_faces(input_path, output_path):
img = imageio.imread(input_path) img = imageio.imread(input_path)
face_size = 512 #each face o/p image will be 512x512 face_size = 512 #each face o/p image will be 512x512
faces = ["right", "left", "top", "bottom", "front", "back"] faces = ["right", "left", "top", "bottom", "front", "back"]
results = {}
for face in faces: for face in faces:
results[face] = face_rendering(img, face, face_size) face_img = face_rendering(img, face, face_size)
face_output_path = os.path.join(output_path, f"{face}.png") face_output_path = os.path.join(output_path, f"{face}.png")
imageio.imsave(face_output_path, results[face]) imageio.imsave(face_output_path, face_img)
print(f"Saved {face} face to {face_output_path}") print(f"Saved {face} face to {face_output_path}")
if __name__ == "__main__":
# Get the directory of the current script
script_dir = os.path.dirname(os.path.abspath(__file__))
# Default output path relative to the script directory
default_output_path = os.path.join(script_dir, "cubemap_faces")
# if __name__ == "__main__": # Use command-line arguments if provided
# input_path = select_image() if len(sys.argv) > 1:
# height, width = get_res(input_path)
# print("width: , Height:", width, height)
# #output_path = input("Enter output directory: ").strip()
# #if not output_path:
# output_path = "C:\Project\AVVR-Pipeline-Internship\material_recognition\Dynamic-Backward-Attention-Transformer\split_output"
# if not os.path.exists(output_path):
# os.makedirs(output_path)
# generate_cube_faces(input_path, output_path)
#input_path = select_image()
input_path = sys.argv[1] input_path = sys.argv[1]
with open('path.txt', 'w') as file: if len(sys.argv) > 2:
file.write(input_path) output_path = sys.argv[2]
file.close() else:
output_path = default_output_path
#output_path = input("Enter output directory: ").strip() else:
#if not output_path: input_path = select_image()
output_path = "C:\Project\AVVR-Pipeline-Internship\material_recognition\Dynamic-Backward-Attention-Transformer\split_output" output_path = default_output_path
if not os.path.exists(output_path):
os.makedirs(output_path) # Ensure output directory exists
os.makedirs(output_path, exist_ok=True)
# Generate cube faces
generate_cube_faces(input_path, output_path) generate_cube_faces(input_path, output_path)
print(f"Cubemap faces saved to: {output_path}")
\ No newline at end of file
import tkinter as tk import tkinter as tk
from tkinter import filedialog from tkinter import filedialog
import cv2 import cv2
import os
def select_image(): def select_image():
root = tk.Tk() root = tk.Tk()
root.withdraw() root.withdraw()
...@@ -16,7 +16,15 @@ def select_image(): ...@@ -16,7 +16,15 @@ def select_image():
def get_res(path): def get_res(path):
img = cv2.imread(path) img = cv2.imread(path)
height, width, _ = img.shape height, width, _ = img.shape
cv2.imwrite('C:\Project\AVVR-Pipeline-Internship\edgenet360\Data\Input\\rgb.png', img)
# Construct the output path dynamically
script_dir = os.path.dirname(os.path.abspath(__file__))
output_dir = os.path.join(script_dir, "..", "edgenet-360", "Data", "Input")
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, "rgb.png")
cv2.imwrite(output_path, img)
print(f"RGB image saved to: {output_path}")
return height, width return height, width
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment