Alessio Grancini
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import os
|
|
| 5 |
import utils
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
import spaces
|
|
|
|
| 8 |
|
| 9 |
from image_segmenter import ImageSegmenter
|
| 10 |
from monocular_depth_estimator import MonocularDepthEstimator
|
|
@@ -24,7 +25,21 @@ def initialize_models():
|
|
| 24 |
if depth_estimator is None:
|
| 25 |
depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256")
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def process_image(image):
|
| 29 |
try:
|
| 30 |
print("Starting image processing")
|
|
@@ -43,7 +58,7 @@ def process_image(image):
|
|
| 43 |
print(traceback.format_exc())
|
| 44 |
raise
|
| 45 |
|
| 46 |
-
@
|
| 47 |
def test_process_img(image):
|
| 48 |
initialize_models()
|
| 49 |
image = utils.resize(image)
|
|
@@ -51,7 +66,7 @@ def test_process_img(image):
|
|
| 51 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
| 52 |
return image_segmentation, objects_data, depthmap, depth_colormap
|
| 53 |
|
| 54 |
-
@
|
| 55 |
def process_video(vid_path=None):
|
| 56 |
try:
|
| 57 |
initialize_models()
|
|
@@ -66,6 +81,7 @@ def process_video(vid_path=None):
|
|
| 66 |
dist_image = utils.draw_depth_info(frame, depthmap, objects_data)
|
| 67 |
yield cv2.cvtColor(image_segmentation, cv2.COLOR_BGR2RGB), depth_colormap, cv2.cvtColor(dist_image, cv2.COLOR_BGR2RGB)
|
| 68 |
|
|
|
|
| 69 |
return None
|
| 70 |
except Exception as e:
|
| 71 |
print(f"Error in process_video: {str(e)}")
|
|
@@ -83,7 +99,7 @@ def update_confidence_threshold(thres_val):
|
|
| 83 |
initialize_models()
|
| 84 |
img_seg.confidence_threshold = thres_val/100
|
| 85 |
|
| 86 |
-
@
|
| 87 |
def model_selector(model_type):
|
| 88 |
global img_seg, depth_estimator
|
| 89 |
|
|
@@ -104,6 +120,23 @@ def cancel():
|
|
| 104 |
CANCEL_PROCESSING = True
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
with gr.Blocks() as my_app:
|
| 108 |
# title
|
| 109 |
gr.Markdown("<h1><center>Simultaneous Segmentation and Depth Estimation</center></h1>")
|
|
@@ -134,7 +167,6 @@ if __name__ == "__main__":
|
|
| 134 |
dist_img_output = gr.Image(height=300, label="Distance")
|
| 135 |
pcd_img_output = gr.Plot(label="Point Cloud")
|
| 136 |
|
| 137 |
-
gr.Markdown("## Sample Images")
|
| 138 |
gr.Examples(
|
| 139 |
examples=[os.path.join(os.path.dirname(__file__), "assets/images/baggage_claim.jpg"),
|
| 140 |
os.path.join(os.path.dirname(__file__), "assets/images/kitchen_2.png"),
|
|
@@ -172,7 +204,6 @@ if __name__ == "__main__":
|
|
| 172 |
with gr.Row():
|
| 173 |
dist_vid_output = gr.Image(height=300, label="Distance")
|
| 174 |
|
| 175 |
-
gr.Markdown("## Sample Videos")
|
| 176 |
gr.Examples(
|
| 177 |
examples=[os.path.join(os.path.dirname(__file__), "assets/videos/input_video.mp4"),
|
| 178 |
os.path.join(os.path.dirname(__file__), "assets/videos/driving.mp4"),
|
|
|
|
| 5 |
import utils
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
import spaces
|
| 8 |
+
import torch
|
| 9 |
|
| 10 |
from image_segmenter import ImageSegmenter
|
| 11 |
from monocular_depth_estimator import MonocularDepthEstimator
|
|
|
|
| 25 |
if depth_estimator is None:
|
| 26 |
depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256")
|
| 27 |
|
| 28 |
+
def safe_gpu_decorator(func):
|
| 29 |
+
"""Custom decorator to handle GPU operations safely"""
|
| 30 |
+
def wrapper(*args, **kwargs):
|
| 31 |
+
try:
|
| 32 |
+
return func(*args, **kwargs)
|
| 33 |
+
except RuntimeError as e:
|
| 34 |
+
if "cudaGetDeviceCount" in str(e):
|
| 35 |
+
print("GPU initialization failed, falling back to CPU")
|
| 36 |
+
# Set environment variable to force CPU
|
| 37 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
| 38 |
+
return func(*args, **kwargs)
|
| 39 |
+
raise
|
| 40 |
+
return wrapper
|
| 41 |
+
|
| 42 |
+
@safe_gpu_decorator
|
| 43 |
def process_image(image):
|
| 44 |
try:
|
| 45 |
print("Starting image processing")
|
|
|
|
| 58 |
print(traceback.format_exc())
|
| 59 |
raise
|
| 60 |
|
| 61 |
+
@safe_gpu_decorator
|
| 62 |
def test_process_img(image):
|
| 63 |
initialize_models()
|
| 64 |
image = utils.resize(image)
|
|
|
|
| 66 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
| 67 |
return image_segmentation, objects_data, depthmap, depth_colormap
|
| 68 |
|
| 69 |
+
@safe_gpu_decorator
|
| 70 |
def process_video(vid_path=None):
|
| 71 |
try:
|
| 72 |
initialize_models()
|
|
|
|
| 81 |
dist_image = utils.draw_depth_info(frame, depthmap, objects_data)
|
| 82 |
yield cv2.cvtColor(image_segmentation, cv2.COLOR_BGR2RGB), depth_colormap, cv2.cvtColor(dist_image, cv2.COLOR_BGR2RGB)
|
| 83 |
|
| 84 |
+
vid_cap.release()
|
| 85 |
return None
|
| 86 |
except Exception as e:
|
| 87 |
print(f"Error in process_video: {str(e)}")
|
|
|
|
| 99 |
initialize_models()
|
| 100 |
img_seg.confidence_threshold = thres_val/100
|
| 101 |
|
| 102 |
+
@safe_gpu_decorator
|
| 103 |
def model_selector(model_type):
|
| 104 |
global img_seg, depth_estimator
|
| 105 |
|
|
|
|
| 120 |
CANCEL_PROCESSING = True
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
+
# Try to initialize CUDA early to catch any issues
|
| 124 |
+
try:
|
| 125 |
+
if torch.cuda.is_available():
|
| 126 |
+
print("CUDA is available. Using GPU.")
|
| 127 |
+
# Test CUDA initialization
|
| 128 |
+
torch.cuda.init()
|
| 129 |
+
device = torch.device("cuda")
|
| 130 |
+
else:
|
| 131 |
+
print("CUDA is not available. Using CPU.")
|
| 132 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
| 133 |
+
device = torch.device("cpu")
|
| 134 |
+
except RuntimeError as e:
|
| 135 |
+
print(f"CUDA initialization failed: {e}")
|
| 136 |
+
print("Falling back to CPU mode")
|
| 137 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
| 138 |
+
device = torch.device("cpu")
|
| 139 |
+
|
| 140 |
with gr.Blocks() as my_app:
|
| 141 |
# title
|
| 142 |
gr.Markdown("<h1><center>Simultaneous Segmentation and Depth Estimation</center></h1>")
|
|
|
|
| 167 |
dist_img_output = gr.Image(height=300, label="Distance")
|
| 168 |
pcd_img_output = gr.Plot(label="Point Cloud")
|
| 169 |
|
|
|
|
| 170 |
gr.Examples(
|
| 171 |
examples=[os.path.join(os.path.dirname(__file__), "assets/images/baggage_claim.jpg"),
|
| 172 |
os.path.join(os.path.dirname(__file__), "assets/images/kitchen_2.png"),
|
|
|
|
| 204 |
with gr.Row():
|
| 205 |
dist_vid_output = gr.Image(height=300, label="Distance")
|
| 206 |
|
|
|
|
| 207 |
gr.Examples(
|
| 208 |
examples=[os.path.join(os.path.dirname(__file__), "assets/videos/input_video.mp4"),
|
| 209 |
os.path.join(os.path.dirname(__file__), "assets/videos/driving.mp4"),
|