import argparse
import glob
import multiprocessing as mp
import os
import time
import cv2
import tqdm
import numpy as np
import gradio as gr
from tools.util import *
from detectron2.config import get_cfg
from detectron2.projects.deeplab import add_deeplab_config
from detectron2.data.detection_utils import read_image
from detectron2.utils.logger import setup_logger
from open_vocab_seg import add_ovseg_config
from open_vocab_seg.utils import VisualizationDemo, VisualizationDemoIndoor
constants
WINDOW_NAME = "Open vocabulary segmentation"
def setup_cfg(args):
# load config from file and command-line arguments
cfg = get_cfg()
# for poly lr schedule
add_deeplab_config(cfg)
add_ovseg_config(cfg)
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
return cfg
def get_parser():
parser = argparse.ArgumentParser(description="Detectron2 demo for open vocabulary segmentation")
parser.add_argument(
"--config-file",
default="configs/ovseg_swinB_vitL_demo.yaml",
metavar="FILE",
help="path to config file",
)
parser.add_argument(
"--input",
default=["/mnt/lustre/jkyang/PSG4D/sailvos3d/downloads/sailvos3d/trevor_1_int/images/000160.bmp"],
nargs="+",
help="A list of space separated input images; "
"or a single glob pattern such as 'directory/*.jpg'",
)
parser.add_argument(
"--class-names",
default=["person", "car", "motorcycle", "truck", "bird", "dog", "handbag", "suitcase", "bottle", "cup", "bowl", "chair", "potted plant", "bed", "dining table", "tv", "laptop", "cell phone", "bag", "bin", "box", "door", "road barrier", "stick", "lamp", "floor", "wall"],
nargs="+",
help="A list of user-defined class_names"
)
parser.add_argument(
"--output",
default = "./pred",
help="A file or directory to save output visualizations. "
"If not given, will show output in an OpenCV window.",
)
parser.add_argument(
"--opts",
help="Modify config options using the command-line 'KEY VALUE' pairs",
default=["MODEL.WEIGHTS", "ovseg_swinbase_vitL14_ft_mpt.pth"],
nargs=argparse.REMAINDER,
)
return parser
To create a test code that runs the inference without using Gradio UI, we can simplify the workflow. The test code will directly call the functions greet_sailvos3d or greet_scannet with predefined inputs, capture their outputs, and save the resulting images without relying on the UI components.
Below is an example of how to write a testing script. This script bypasses the UI, directly loads input images and parameters, and calls the necessary functions:
python
import os
import numpy as np
from pathlib import Path
from tools.util import two_image_to_gif # Assuming this function is defined elsewhere
from test_code import greet_sailvos3d, greet_scannet # Assuming the functions are imported from the main code
# Define the paths to the input files
sailvos3d_rgb_input = "UI/sailvos3d/ex1/inputs/rgb_000160.bmp"
sailvos3d_depth_map_input = Path("UI/sailvos3d/ex1/inputs/depth_000160.npy")
sailvos3d_rage_matrices_input = Path("UI/sailvos3d/ex1/inputs/rage_matrices_000160.npz")
sailvos3d_class_candidates = 'person, car, motorcycle, truck, bird, dog, handbag, suitcase, bottle, cup, bowl, chair, potted plant, bed, dining table, tv, laptop, cell phone, bag, bin, box, door, road barrier, stick, lamp, floor, wall'
scannet_rgb_input = "UI/scannetv2/examples/scene0000_00/color/1660.jpg"
scannet_depth_map_input = Path("UI/scannetv2/examples/scene0000_00/depth/1660.png")
scannet_class_candidates = 'wall, floor, cabinet, bed, chair, sofa, table, door, window, bookshelf, picture, counter, desk, curtain, refrigerator, shower curtain, toilet, sink, bathtub, other furniture'
def main():
# Sailvos3D test
print("Running Sailvos3D Test...")
rgb_sam_2d, rgb_sam_mask_gif, depth_map, depth_sam_2d, depth_sam_mask_gif = greet_sailvos3d(
rgb_input=sailvos3d_rgb_input,
depth_map_input=sailvos3d_depth_map_input,
rage_matrices_input=sailvos3d_rage_matrices_input,
class_candidates=sailvos3d_class_candidates
)
print("Sailvos3D Outputs:")
print(f"RGB Semantic SAM 2D: {rgb_sam_2d}")
print(f"RGB Semantic SAM Mask GIF: {rgb_sam_mask_gif}")
print(f"Depth Map: {depth_map}")
print(f"Depth Semantic SAM 2D: {depth_sam_2d}")
print(f"Depth Semantic SAM Mask GIF: {depth_sam_mask_gif}")
# Save or process the outputs as needed
# Scannet test
print("Running ScanNet Test...")
rgb_sam_2d_scannet, rgb_sam_mask_gif_scannet, depth_map_scannet, depth_sam_2d_scannet, depth_sam_mask_gif_scannet = greet_scannet(
rgb_input=scannet_rgb_input,
depth_map_input=scannet_depth_map_input,
class_candidates=scannet_class_candidates
)
print("ScanNet Outputs:")
print(f"RGB Semantic SAM 2D: {rgb_sam_2d_scannet}")
print(f"RGB Semantic SAM Mask GIF: {rgb_sam_mask_gif_scannet}")
print(f"Depth Map: {depth_map_scannet}")
print(f"Depth Semantic SAM 2D: {depth_sam_2d_scannet}")
print(f"Depth Semantic SAM Mask GIF: {depth_sam_mask_gif_scannet}")
# Save or process the outputs as needed
if __name__ == "__main__":
# Ensure the output directories exist
Path("outputs").mkdir(parents=True, exist_ok=True)
main()
How to Use the Test Code:
Ensure that your original code is in a file (like test_code.py) and accessible.
Adjust the paths for your input RGB images, depth maps, and rage matrices according to your directory structure.
Run the test code in your environment, making sure all required modules and dependencies are installed.
The output files will be saved in the outputs directory, or additional processing can be done as needed.
Notes:
The test script separates the Sailvos3D and ScanNet implementations into distinct sections, capturing the outputs from both.
If the original function creates directories for outputs, ensure that the directory management is done appropriately to avoid any FileNotFoundError.
The script is set to print the paths of the output images. Ensure that you have the supporting utility methods available in your codebase.