Skip to content

Commit

Permalink
update saving to svo file using video converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Overbeek committed Jan 31, 2025
1 parent 963c6e4 commit b9ba05e
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 8 deletions.
4 changes: 2 additions & 2 deletions configurations/sam2_zed_small.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ grounding_dino:
nms_threshold: 0.6
# config_path: "/home/nakama/Documents/TychoMSC/models/sam2_track_test/segment-anything-2-real-time/GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py"
config_path: "./GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py"
# checkpoint_path: "/home/nakama/Documents/TychoMSC/models/sam2_track_test/segment-anything-2-real-time/GroundingDINO/weights/groundingdino_swint_ogc.pth"
checkpoint_path: "./GroundingDINO/weights/groundingdino_swint_ogc.pth"

sam2:
checkpoint: "./checkpoints/sam2_hiera_small.pt"
model_cfg: "sam2_configs/sam2_hiera_s.yaml"

camera:
connection_type: "id"
connection_type: "svo"

depth:
refine_depth: true
max_occlusion_percentage: 0.6

results:
output_dir: "output"
Expand Down
Binary file added output.svo2
Binary file not shown.
Binary file removed output/left_img_og.png
Binary file not shown.
Binary file modified output/norm_depth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed output/norm_depth_heatmap.jpg
Binary file not shown.
Binary file modified output/output.mp4
Binary file not shown.
Binary file modified output/output_depth.mp4
Binary file not shown.
Binary file modified output/refined_depth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified output/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions scripts/sam2_track_zed.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
from utils.sam2prty import Sam2PromptType


depth_refinement_enabled = False


def run(cfg, sam2_prompt: Sam2PromptType) -> None:
global depth_refinement_enabled
depth_refinement_enabled = cfg.depth.refine_depth

caption_queue = queue.Queue()

Log.info("Initializing the pipeline...", tag="pipeline_init")
Expand Down Expand Up @@ -246,7 +246,7 @@ def run(cfg, sam2_prompt: Sam2PromptType) -> None:


left_image_rgb = cv2.addWeighted(left_image_rgb, 1, all_mask, 0.5, 0)
if framecount < 500:
if framecount < 5000:
# left_image_rgb = np.array(left_image_rgb)
# norm_depth_map = np.array(norm_depth_map)
# matr = np.ones((1440,1280,4), dtype='uint8')
Expand All @@ -270,7 +270,7 @@ def run(cfg, sam2_prompt: Sam2PromptType) -> None:
# *Refine the depth mask using masks
if depth_refinement_enabled:
# !NOTE: plane fiting best one so far
refined_depth_map = depth_refinement_RANSAC_plane_fitting(depth_map, obj_masks, max_occ_percentage=0.7)
refined_depth_map = depth_refinement_RANSAC_plane_fitting(depth_map, obj_masks, max_occ_percentage=cfg.depth.max_occlusion_percentage)
refined_depth_map = cv2.normalize(refined_depth_map, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
# previous_depth = norm_depth_map
cv2.imwrite(os.path.join(output_dir, 'refined_depth.png'), refined_depth_map)
Expand Down Expand Up @@ -300,7 +300,7 @@ def run(cfg, sam2_prompt: Sam2PromptType) -> None:

with initialize(config_path="../configurations"):
cfg = compose(config_name="sam2_zed_small")
sam2_prompt = Sam2PromptType('g_dino_bbox',user_caption='apple')
sam2_prompt = Sam2PromptType('g_dino_bbox',user_caption='keyboard')


# point_coords = [(390, 200)]
Expand Down
121 changes: 121 additions & 0 deletions scripts/sam2_track_zed_video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from utils.logger import Log
import os,sys
import torch
import numpy as np
import random
import cv2
import time
from hydra import initialize, compose
from hydra.core.global_hydra import GlobalHydra
import imageio

import threading
import queue
from utils.sam2prty import Sam2PromptType
sys.path.insert(0, os.getcwd())


# Initialize GPU settings
if torch.cuda.get_device_properties(0).major >= 8:
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
torch.backends.cudnn.benchmark = True
torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()


from wrappers.pyzed_wrapper import pyzed_wrapper as pw
from utils.utils import *
from scripts.utils.depth_utils import (
depth_refinement_RANSAC_plane_fitting,
)


def run(cfg, sam2_prompt: Sam2PromptType, record: bool = False, svo_file: str = None) -> None:
depth_refinement_enabled = cfg.depth.refine_depth
caption_queue = queue.Queue()

Log.info("Initializing the pipeline...", tag="pipeline_init")

# Build needed models
Log.info("Building models...", tag="building_models")
# try:
# sam2_predictor, grounding_dino_model = build_models(cfg)
# Log.info("Models successfully built and loaded.", tag="model_building")
# except Exception as e:
# Log.error(f"Failed to build models: {e}", tag="model_build_error")
# return

# Initialize ZED camera: Live Mode or SVO Playback Mode
Log.info("Initializing ZED camera...", tag="zed_camera_init")
wrapper = pw.Wrapper("svo" if svo_file else cfg.camera.connection_type)

# If using playback, set SVO file path
if svo_file:
wrapper.input_parameters.svo_input_filename = svo_file

try:
wrapper.open_input_source()
Log.info("ZED camera initialized.", tag="camera_init")
except Exception as e:
Log.error(f"Failed to initialize ZED camera: {e}", tag="camera_init_error")
return

# Start recording if enabled
if record:
wrapper.start_recording("./output/output.svo")
Log.info("Recording started.", tag="recording")

wrapper.start_stream()
Log.info('Camera stream started.', tag = "camera_stream")

framecount = 0

try:
while True:
ts = time.time()

if wrapper.retrieve(is_image=True, is_measure=True):
# Extract images
left_image = wrapper.output_image
depth_map = wrapper.output_measure

norm_depth_map = cv2.normalize(depth_map, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
left_image_rgb = cv2.cvtColor(left_image, cv2.COLOR_RGBA2RGB)

# # Display and save
# cv2.imshow("ZED Left", left_image_rgb)
# cv2.imshow("Depth Map", norm_depth_map)

# cv2.imwrite(f"output/frame_{framecount}.png", left_image_rgb)
# cv2.imwrite(f"output/depth_{framecount}.png", norm_depth_map)

if framecount > 100:
break

framecount += 1

except Exception as e:
Log.error(f"An error occurred: {e}", tag="runtime_error")

finally:
# Stop recording if enabled
if record:
wrapper.stop_recording()
Log.info("Recording stopped.", tag="recording_stop")

Log.info("Shutting down and closing stream...", tag='Close_stream')
wrapper.stop_stream()
wrapper.close_input_source()
cv2.destroyAllWindows()

if __name__ == "__main__":
# Ensure GlobalHydra is properly cleared before initializing
if GlobalHydra.instance().is_initialized():
GlobalHydra.instance().clear()

with initialize(config_path="../configurations"):
cfg = compose(config_name="sam2_zed_small")
sam2_prompt = Sam2PromptType('g_dino_bbox', user_caption='keyboard')

# Run after Hydra is fully initialized
run(cfg, sam2_prompt, record=True)
2 changes: 1 addition & 1 deletion scripts/wrappers/pyzed_wrapper/wrapper_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, type="id"):
elif self.type == self.SERIAL:
self.serial_number = 0
elif self.type == self.SVO:
self.svo_input_filename = "./output.svo"
self.svo_input_filename = "./output/output.svo2"


elif self.type == self.STREAM:
Expand Down

0 comments on commit b9ba05e

Please sign in to comment.