Alessio Grancini
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -18,12 +18,12 @@ import spaces # Required for ZeroGPU
|
|
18 |
# Ensure CUDA is NOT initialized before ZeroGPU assigns a device
|
19 |
torch.backends.cudnn.enabled = False # Prevents CUDA errors on first GPU allocation
|
20 |
|
|
|
21 |
def initialize_gpu():
|
22 |
-
"""Ensure
|
23 |
global device
|
24 |
try:
|
25 |
-
with spaces.GPU(): # Ensures GPU
|
26 |
-
torch.cuda.init()
|
27 |
if torch.cuda.is_available():
|
28 |
device = torch.device("cuda")
|
29 |
print(f"✅ GPU initialized: {torch.cuda.get_device_name(0)}")
|
@@ -35,6 +35,9 @@ def initialize_gpu():
|
|
35 |
print(f"🚨 GPU initialization failed: {e}")
|
36 |
device = torch.device("cpu")
|
37 |
|
|
|
|
|
|
|
38 |
# Run GPU initialization before using CUDA
|
39 |
initialize_gpu()
|
40 |
|
@@ -47,16 +50,16 @@ CANCEL_PROCESSING = False
|
|
47 |
img_seg = ImageSegmenter(model_type="yolov8s-seg")
|
48 |
depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256")
|
49 |
|
50 |
-
@spaces.GPU # Ensures
|
51 |
def process_image(image):
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
|
62 |
@spaces.GPU # Requests GPU for depth estimation
|
@@ -68,21 +71,20 @@ def test_process_img(image):
|
|
68 |
|
69 |
@spaces.GPU
|
70 |
def process_video(vid_path=None):
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
yield cv2.cvtColor(image_segmentation, cv2.COLOR_BGR2RGB), depth_colormap, cv2.cvtColor(dist_image, cv2.COLOR_BGR2RGB)
|
82 |
-
|
83 |
return None
|
84 |
|
85 |
|
|
|
86 |
def update_segmentation_options(options):
|
87 |
img_seg.is_show_bounding_boxes = True if 'Show Boundary Box' in options else False
|
88 |
img_seg.is_show_segmentation = True if 'Show Segmentation Region' in options else False
|
|
|
18 |
# Ensure CUDA is NOT initialized before ZeroGPU assigns a device
|
19 |
torch.backends.cudnn.enabled = False # Prevents CUDA errors on first GPU allocation
|
20 |
|
21 |
+
|
22 |
def initialize_gpu():
|
23 |
+
"""Ensure ZeroGPU assigns a GPU before initializing CUDA"""
|
24 |
global device
|
25 |
try:
|
26 |
+
with spaces.GPU(): # Ensures GPU is allocated
|
|
|
27 |
if torch.cuda.is_available():
|
28 |
device = torch.device("cuda")
|
29 |
print(f"✅ GPU initialized: {torch.cuda.get_device_name(0)}")
|
|
|
35 |
print(f"🚨 GPU initialization failed: {e}")
|
36 |
device = torch.device("cpu")
|
37 |
|
38 |
+
# ✅ Don't call CUDA here!
|
39 |
+
device = torch.device("cpu") # Default to CPU
|
40 |
+
|
41 |
# Run GPU initialization before using CUDA
|
42 |
initialize_gpu()
|
43 |
|
|
|
50 |
img_seg = ImageSegmenter(model_type="yolov8s-seg")
|
51 |
depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256")
|
52 |
|
53 |
+
@spaces.GPU # Ensures ZeroGPU assigns a GPU
|
54 |
def process_image(image):
|
55 |
+
image = utils.resize(image)
|
56 |
+
image_segmentation, objects_data = img_seg.predict(image)
|
57 |
+
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
58 |
+
dist_image = utils.draw_depth_info(image, depthmap, objects_data)
|
59 |
+
objs_pcd = utils.generate_obj_pcd(depthmap, objects_data)
|
60 |
+
plot_fig = display_pcd(objs_pcd)
|
61 |
+
return image_segmentation, depth_colormap, dist_image, plot_fig
|
62 |
+
|
63 |
|
64 |
|
65 |
@spaces.GPU # Requests GPU for depth estimation
|
|
|
71 |
|
72 |
@spaces.GPU
|
73 |
def process_video(vid_path=None):
|
74 |
+
vid_cap = cv2.VideoCapture(vid_path)
|
75 |
+
while vid_cap.isOpened():
|
76 |
+
ret, frame = vid_cap.read()
|
77 |
+
if ret:
|
78 |
+
print("making predictions ....")
|
79 |
+
frame = utils.resize(frame)
|
80 |
+
image_segmentation, objects_data = img_seg.predict(frame)
|
81 |
+
depthmap, depth_colormap = depth_estimator.make_prediction(frame)
|
82 |
+
dist_image = utils.draw_depth_info(frame, depthmap, objects_data)
|
83 |
+
yield cv2.cvtColor(image_segmentation, cv2.COLOR_BGR2RGB), depth_colormap, cv2.cvtColor(dist_image, cv2.COLOR_BGR2RGB)
|
|
|
|
|
84 |
return None
|
85 |
|
86 |
|
87 |
+
|
88 |
def update_segmentation_options(options):
|
89 |
img_seg.is_show_bounding_boxes = True if 'Show Boundary Box' in options else False
|
90 |
img_seg.is_show_segmentation = True if 'Show Segmentation Region' in options else False
|