import cv2 import gradio as gr import numpy as np import os import utils import plotly.graph_objects as go import spaces import torch from image_segmenter import ImageSegmenter from monocular_depth_estimator import MonocularDepthEstimator from point_cloud_generator import display_pcd # params CANCEL_PROCESSING = False # Initialize classes without loading models img_seg = None depth_estimator = None def initialize_models(): global img_seg, depth_estimator if img_seg is None: img_seg = ImageSegmenter(model_type="yolov8s-seg") if depth_estimator is None: depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256") def safe_gpu_decorator(func): """Custom decorator to handle GPU operations safely""" def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except RuntimeError as e: if "cudaGetDeviceCount" in str(e): print("GPU initialization failed, falling back to CPU") # Set environment variable to force CPU os.environ['CUDA_VISIBLE_DEVICES'] = '' return func(*args, **kwargs) raise return wrapper @safe_gpu_decorator def process_image(image): try: print("Starting image processing") initialize_models() image = utils.resize(image) image_segmentation, objects_data = img_seg.predict(image) depthmap, depth_colormap = depth_estimator.make_prediction(image) dist_image = utils.draw_depth_info(image, depthmap, objects_data) objs_pcd = utils.generate_obj_pcd(depthmap, objects_data) plot_fig = display_pcd(objs_pcd) return image_segmentation, depth_colormap, dist_image, plot_fig except Exception as e: print(f"Error in process_image: {str(e)}") import traceback print(traceback.format_exc()) raise @safe_gpu_decorator def test_process_img(image): initialize_models() image = utils.resize(image) image_segmentation, objects_data = img_seg.predict(image) depthmap, depth_colormap = depth_estimator.make_prediction(image) return image_segmentation, objects_data, depthmap, depth_colormap @safe_gpu_decorator def process_video(vid_path=None): try: initialize_models() vid_cap = cv2.VideoCapture(vid_path) while vid_cap.isOpened(): ret, frame = vid_cap.read() if ret: print("making predictions ....") frame = utils.resize(frame) image_segmentation, objects_data = img_seg.predict(frame) depthmap, depth_colormap = depth_estimator.make_prediction(frame) dist_image = utils.draw_depth_info(frame, depthmap, objects_data) yield cv2.cvtColor(image_segmentation, cv2.COLOR_BGR2RGB), depth_colormap, cv2.cvtColor(dist_image, cv2.COLOR_BGR2RGB) vid_cap.release() return None except Exception as e: print(f"Error in process_video: {str(e)}") import traceback print(traceback.format_exc()) raise def update_segmentation_options(options): initialize_models() img_seg.is_show_bounding_boxes = True if 'Show Boundary Box' in options else False img_seg.is_show_segmentation = True if 'Show Segmentation Region' in options else False img_seg.is_show_segmentation_boundary = True if 'Show Segmentation Boundary' in options else False def update_confidence_threshold(thres_val): initialize_models() img_seg.confidence_threshold = thres_val/100 @safe_gpu_decorator def model_selector(model_type): global img_seg, depth_estimator if "Small - Better performance and less accuracy" == model_type: midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg" elif "Medium - Balanced performance and accuracy" == model_type: midas_model, yolo_model = "dpt_hybrid_384", "yolov8m-seg" elif "Large - Slow performance and high accuracy" == model_type: midas_model, yolo_model = "dpt_large_384", "yolov8l-seg" else: midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg" img_seg = ImageSegmenter(model_type=yolo_model) depth_estimator = MonocularDepthEstimator(model_type=midas_model) def cancel(): global CANCEL_PROCESSING CANCEL_PROCESSING = True if __name__ == "__main__": # Try to initialize CUDA early to catch any issues try: if torch.cuda.is_available(): print("CUDA is available. Using GPU.") # Test CUDA initialization torch.cuda.init() device = torch.device("cuda") else: print("CUDA is not available. Using CPU.") os.environ['CUDA_VISIBLE_DEVICES'] = '' device = torch.device("cpu") except RuntimeError as e: print(f"CUDA initialization failed: {e}") print("Falling back to CPU mode") os.environ['CUDA_VISIBLE_DEVICES'] = '' device = torch.device("cpu") with gr.Blocks() as my_app: # title gr.Markdown("