Alessio Grancini
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,6 +24,14 @@ from point_cloud_generator import display_pcd
|
|
| 24 |
|
| 25 |
device = torch.device("cpu") # Start in CPU mode
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def initialize_gpu():
|
| 28 |
"""Ensure ZeroGPU assigns a GPU before initializing CUDA"""
|
| 29 |
global device
|
|
@@ -91,13 +99,34 @@ def update_segmentation_options(options):
|
|
| 91 |
img_seg.is_show_segmentation = True if 'Show Segmentation Region' in options else False
|
| 92 |
img_seg.is_show_segmentation_boundary = True if 'Show Segmentation Boundary' in options else False
|
| 93 |
|
| 94 |
-
def update_confidence_threshold(thres_val):
|
| 95 |
-
img_seg.confidence_threshold = thres_val/100
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
if "Small - Better performance and less accuracy" == model_type:
|
| 102 |
midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg"
|
| 103 |
elif "Medium - Balanced performance and accuracy" == model_type:
|
|
@@ -107,8 +136,10 @@ def model_selector(model_type):
|
|
| 107 |
else:
|
| 108 |
midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg"
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# START
|
| 114 |
# added for lens studio
|
|
@@ -228,6 +259,8 @@ def get_bbox_from_mask(mask):
|
|
| 228 |
|
| 229 |
@spaces.GPU
|
| 230 |
def get_detection_data(image_data):
|
|
|
|
|
|
|
| 231 |
try:
|
| 232 |
if isinstance(image_data, dict):
|
| 233 |
nested_dict = image_data.get("image", {}).get("image", {})
|
|
@@ -263,7 +296,7 @@ def get_detection_data(image_data):
|
|
| 263 |
print(f"Debug - Original image shape: {image.shape}")
|
| 264 |
|
| 265 |
# Dynamically update model size and confidence threshold
|
| 266 |
-
model_selector(model_size, img_seg, depth_estimator)
|
| 267 |
update_confidence_threshold(confidence_threshold, img_seg)
|
| 268 |
|
| 269 |
image_segmentation, objects_data = img_seg.predict(resized_image)
|
|
|
|
| 24 |
|
| 25 |
device = torch.device("cpu") # Start in CPU mode
|
| 26 |
|
| 27 |
+
# Global instances (can be reinitialized dynamically)
|
| 28 |
+
img_seg = ImageSegmenter(model_type="yolov8s-seg")
|
| 29 |
+
depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256")
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
def initialize_gpu():
|
| 36 |
"""Ensure ZeroGPU assigns a GPU before initializing CUDA"""
|
| 37 |
global device
|
|
|
|
| 99 |
img_seg.is_show_segmentation = True if 'Show Segmentation Region' in options else False
|
| 100 |
img_seg.is_show_segmentation_boundary = True if 'Show Segmentation Boundary' in options else False
|
| 101 |
|
| 102 |
+
#def update_confidence_threshold(thres_val):
|
| 103 |
+
# img_seg.confidence_threshold = thres_val/100
|
| 104 |
|
| 105 |
+
def update_confidence_threshold(thres_val, img_seg_instance):
|
| 106 |
+
"""Update confidence threshold in ImageSegmenter"""
|
| 107 |
+
img_seg_instance.confidence_threshold = thres_val
|
| 108 |
+
print(f"Confidence threshold updated to: {thres_val}")
|
| 109 |
+
|
| 110 |
+
#@spaces.GPU # Ensures YOLO + MiDaS get GPU access
|
| 111 |
+
#def model_selector(model_type):
|
| 112 |
+
# global img_seg, depth_estimator
|
| 113 |
+
|
| 114 |
+
# if "Small - Better performance and less accuracy" == model_type:
|
| 115 |
+
# midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg"
|
| 116 |
+
# elif "Medium - Balanced performance and accuracy" == model_type:
|
| 117 |
+
# midas_model, yolo_model = "dpt_hybrid_384", "yolov8m-seg"
|
| 118 |
+
# elif "Large - Slow performance and high accuracy" == model_type:
|
| 119 |
+
# midas_model, yolo_model = "dpt_large_384", "yolov8l-seg"
|
| 120 |
+
# else:
|
| 121 |
+
# midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg"
|
| 122 |
+
#
|
| 123 |
+
# img_seg = ImageSegmenter(model_type=yolo_model)
|
| 124 |
+
# depth_estimator = MonocularDepthEstimator(model_type=midas_model)
|
| 125 |
|
| 126 |
+
|
| 127 |
+
# Updated model_selector to accept img_seg and depth_estimator instances
|
| 128 |
+
@spaces.GPU # Ensures YOLO + MiDaS get GPU access
|
| 129 |
+
def model_selector(model_type, img_seg_instance, depth_estimator_instance):
|
| 130 |
if "Small - Better performance and less accuracy" == model_type:
|
| 131 |
midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg"
|
| 132 |
elif "Medium - Balanced performance and accuracy" == model_type:
|
|
|
|
| 136 |
else:
|
| 137 |
midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg"
|
| 138 |
|
| 139 |
+
# Reinitialize the provided instances with the selected model types
|
| 140 |
+
img_seg_instance.__init__(model_type=yolo_model)
|
| 141 |
+
depth_estimator_instance.__init__(model_type=midas_model)
|
| 142 |
+
print(f"Model updated: YOLO={yolo_model}, MiDaS={midas_model}")
|
| 143 |
|
| 144 |
# START
|
| 145 |
# added for lens studio
|
|
|
|
| 259 |
|
| 260 |
@spaces.GPU
|
| 261 |
def get_detection_data(image_data):
|
| 262 |
+
global img_seg, depth_estimator # Still reference global instances
|
| 263 |
+
|
| 264 |
try:
|
| 265 |
if isinstance(image_data, dict):
|
| 266 |
nested_dict = image_data.get("image", {}).get("image", {})
|
|
|
|
| 296 |
print(f"Debug - Original image shape: {image.shape}")
|
| 297 |
|
| 298 |
# Dynamically update model size and confidence threshold
|
| 299 |
+
model_selector(model_size, img_seg, depth_estimator) # Pass the global instances
|
| 300 |
update_confidence_threshold(confidence_threshold, img_seg)
|
| 301 |
|
| 302 |
image_segmentation, objects_data = img_seg.predict(resized_image)
|