Alessio Grancini commited on
Commit
7a68aca
Β·
verified Β·
1 Parent(s): 512f124

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
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
- gpu_check = subprocess.run(["nvidia-smi"], capture_output=True, text=True)
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
- # Check if Hugging Face assigned a GPU
37
- hf_gpu_env = os.getenv("CUDA_VISIBLE_DEVICES")
38
- print(f"πŸ” Hugging Face CUDA_VISIBLE_DEVICES: {hf_gpu_env}")
 
 
 
 
 
 
 
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"