Alessio Grancini
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -17,25 +17,19 @@ import torch
|
|
17 |
import subprocess
|
18 |
import os
|
19 |
|
20 |
-
# Check if CUDA is available in PyTorch
|
21 |
-
print(f"π PyTorch CUDA Available: {torch.cuda.is_available()}")
|
22 |
-
|
23 |
-
# Check available CUDA devices
|
24 |
-
if torch.cuda.is_available():
|
25 |
-
print(f"β
GPU detected: {torch.cuda.get_device_name(0)}")
|
26 |
-
else:
|
27 |
-
print("β No GPU detected by PyTorch!")
|
28 |
-
|
29 |
-
# Check `nvidia-smi` output (if NVIDIA GPU is assigned)
|
30 |
try:
|
31 |
-
|
32 |
-
print(f"π₯οΈ nvidia-smi output:\n{gpu_check.stdout}")
|
33 |
-
except FileNotFoundError:
|
34 |
-
print("β nvidia-smi not found! GPU might not be assigned.")
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
|
41 |
|
@@ -45,6 +39,7 @@ CANCEL_PROCESSING = False
|
|
45 |
img_seg = ImageSegmenter(model_type="yolov8s-seg")
|
46 |
depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256")
|
47 |
|
|
|
48 |
def process_image(image):
|
49 |
image = utils.resize(image)
|
50 |
image_segmentation, objects_data = img_seg.predict(image)
|
@@ -54,12 +49,14 @@ def process_image(image):
|
|
54 |
plot_fig = display_pcd(objs_pcd)
|
55 |
return image_segmentation, depth_colormap, dist_image, plot_fig
|
56 |
|
|
|
57 |
def test_process_img(image):
|
58 |
image = utils.resize(image)
|
59 |
image_segmentation, objects_data = img_seg.predict(image)
|
60 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
61 |
return image_segmentation, objects_data, depthmap, depth_colormap
|
62 |
|
|
|
63 |
def process_video(vid_path=None):
|
64 |
vid_cap = cv2.VideoCapture(vid_path)
|
65 |
while vid_cap.isOpened():
|
@@ -74,6 +71,7 @@ def process_video(vid_path=None):
|
|
74 |
|
75 |
return None
|
76 |
|
|
|
77 |
def update_segmentation_options(options):
|
78 |
img_seg.is_show_bounding_boxes = True if 'Show Boundary Box' in options else False
|
79 |
img_seg.is_show_segmentation = True if 'Show Segmentation Region' in options else False
|
@@ -82,7 +80,9 @@ def update_segmentation_options(options):
|
|
82 |
def update_confidence_threshold(thres_val):
|
83 |
img_seg.confidence_threshold = thres_val/100
|
84 |
|
|
|
85 |
def model_selector(model_type):
|
|
|
86 |
|
87 |
if "Small - Better performance and less accuracy" == model_type:
|
88 |
midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg"
|
|
|
17 |
import subprocess
|
18 |
import os
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
try:
|
21 |
+
import spaces # Required for ZeroGPU
|
|
|
|
|
|
|
22 |
|
23 |
+
if torch.cuda.is_available():
|
24 |
+
print(f"β
GPU detected: {torch.cuda.get_device_name(0)}")
|
25 |
+
device = torch.device("cuda")
|
26 |
+
else:
|
27 |
+
print("β No GPU detected initially. ZeroGPU should allocate one dynamically.")
|
28 |
+
device = torch.device("cpu") # Default to CPU until ZeroGPU assigns GPU
|
29 |
+
|
30 |
+
except Exception as e:
|
31 |
+
print(f"π¨ Error checking CUDA: {e}")
|
32 |
+
device = torch.device("cpu")
|
33 |
|
34 |
|
35 |
|
|
|
39 |
img_seg = ImageSegmenter(model_type="yolov8s-seg")
|
40 |
depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256")
|
41 |
|
42 |
+
@spaces.GPU # Ensures GPU is allocated when this function runs
|
43 |
def process_image(image):
|
44 |
image = utils.resize(image)
|
45 |
image_segmentation, objects_data = img_seg.predict(image)
|
|
|
49 |
plot_fig = display_pcd(objs_pcd)
|
50 |
return image_segmentation, depth_colormap, dist_image, plot_fig
|
51 |
|
52 |
+
@spaces.GPU # Requests GPU for depth estimation
|
53 |
def test_process_img(image):
|
54 |
image = utils.resize(image)
|
55 |
image_segmentation, objects_data = img_seg.predict(image)
|
56 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
57 |
return image_segmentation, objects_data, depthmap, depth_colormap
|
58 |
|
59 |
+
@spaces.GPU # Ensures GPU is used for video processing
|
60 |
def process_video(vid_path=None):
|
61 |
vid_cap = cv2.VideoCapture(vid_path)
|
62 |
while vid_cap.isOpened():
|
|
|
71 |
|
72 |
return None
|
73 |
|
74 |
+
|
75 |
def update_segmentation_options(options):
|
76 |
img_seg.is_show_bounding_boxes = True if 'Show Boundary Box' in options else False
|
77 |
img_seg.is_show_segmentation = True if 'Show Segmentation Region' in options else False
|
|
|
80 |
def update_confidence_threshold(thres_val):
|
81 |
img_seg.confidence_threshold = thres_val/100
|
82 |
|
83 |
+
@spaces.GPU # Ensures YOLO + MiDaS get GPU access
|
84 |
def model_selector(model_type):
|
85 |
+
global img_seg, depth_estimator
|
86 |
|
87 |
if "Small - Better performance and less accuracy" == model_type:
|
88 |
midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg"
|