Alessio Grancini commited on
Commit
8569624
·
verified ·
1 Parent(s): 7a68aca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -34
app.py CHANGED
@@ -13,23 +13,31 @@ from point_cloud_generator import display_pcd
13
 
14
 
15
 
16
- import torch
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,15 +47,17 @@ CANCEL_PROCESSING = False
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)
46
- depthmap, depth_colormap = depth_estimator.make_prediction(image)
47
- dist_image = utils.draw_depth_info(image, depthmap, objects_data)
48
- objs_pcd = utils.generate_obj_pcd(depthmap, objects_data)
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):
@@ -56,19 +66,20 @@ def test_process_img(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():
63
- ret, frame = vid_cap.read()
64
- if ret:
65
- print("making predictions ....")
66
- frame = utils.resize(frame)
67
- image_segmentation, objects_data = img_seg.predict(frame)
68
- depthmap, depth_colormap = depth_estimator.make_prediction(frame)
69
- dist_image = utils.draw_depth_info(frame, depthmap, objects_data)
70
- yield cv2.cvtColor(image_segmentation, cv2.COLOR_BGR2RGB), depth_colormap, cv2.cvtColor(dist_image, cv2.COLOR_BGR2RGB)
71
-
 
72
  return None
73
 
74
 
 
13
 
14
 
15
 
16
+ import spaces # Required for ZeroGPU
 
 
17
 
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 that ZeroGPU assigns a GPU before using CUDA"""
23
+ global device
24
+ try:
25
+ with spaces.GPU(): # Ensures GPU allocation
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)}")
30
+ torch.cuda.empty_cache() # Clear memory
31
+ else:
32
+ print("❌ No GPU detected after ZeroGPU allocation.")
33
+ device = torch.device("cpu")
34
+ except Exception as e:
35
+ print(f"🚨 GPU initialization failed: {e}")
36
+ device = torch.device("cpu")
37
+
38
+ # Run GPU initialization before using CUDA
39
+ initialize_gpu()
40
 
 
 
 
41
 
42
 
43
 
 
47
  img_seg = ImageSegmenter(model_type="yolov8s-seg")
48
  depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256")
49
 
50
+ @spaces.GPU # Ensures GPU is allocated before running
51
  def process_image(image):
52
+ with spaces.GPU(): # Explicitly allocate a GPU
53
+ image = utils.resize(image)
54
+ image_segmentation, objects_data = img_seg.predict(image)
55
+ depthmap, depth_colormap = depth_estimator.make_prediction(image)
56
+ dist_image = utils.draw_depth_info(image, depthmap, objects_data)
57
+ objs_pcd = utils.generate_obj_pcd(depthmap, objects_data)
58
+ plot_fig = display_pcd(objs_pcd)
59
+ return image_segmentation, depth_colormap, dist_image, plot_fig
60
+
61
 
62
  @spaces.GPU # Requests GPU for depth estimation
63
  def test_process_img(image):
 
66
  depthmap, depth_colormap = depth_estimator.make_prediction(image)
67
  return image_segmentation, objects_data, depthmap, depth_colormap
68
 
69
+ @spaces.GPU
70
  def process_video(vid_path=None):
71
+ with spaces.GPU():
72
+ vid_cap = cv2.VideoCapture(vid_path)
73
+ while vid_cap.isOpened():
74
+ ret, frame = vid_cap.read()
75
+ if ret:
76
+ print("making predictions ....")
77
+ frame = utils.resize(frame)
78
+ image_segmentation, objects_data = img_seg.predict(frame)
79
+ depthmap, depth_colormap = depth_estimator.make_prediction(frame)
80
+ dist_image = utils.draw_depth_info(frame, depthmap, objects_data)
81
+ yield cv2.cvtColor(image_segmentation, cv2.COLOR_BGR2RGB), depth_colormap, cv2.cvtColor(dist_image, cv2.COLOR_BGR2RGB)
82
+
83
  return None
84
 
85