Alessio Grancini
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -143,32 +143,56 @@ def get_camera_matrix(depth_estimator):
|
|
| 143 |
"cy": depth_estimator.cy_depth
|
| 144 |
}
|
| 145 |
|
|
|
|
| 146 |
@spaces.GPU
|
| 147 |
def get_detection_data(image):
|
| 148 |
-
"""Get structured detection data with depth information"""
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
try:
|
| 152 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
image = utils.resize(image)
|
| 154 |
|
| 155 |
-
#
|
| 156 |
if hasattr(image, "shape"):
|
| 157 |
-
height, width = image.shape[:2]
|
| 158 |
|
| 159 |
# Get detections and depth
|
| 160 |
image_segmentation, objects_data = img_seg.predict(image)
|
| 161 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
| 162 |
|
| 163 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
detections = []
|
| 165 |
for data in objects_data:
|
| 166 |
cls_id, cls_name, cls_center, cls_mask, cls_clr = data
|
| 167 |
-
|
| 168 |
-
# Get masked depth for this object
|
| 169 |
masked_depth, mean_depth = utils.get_masked_depth(depthmap, cls_mask)
|
| 170 |
|
| 171 |
-
# Get bounding box from mask
|
| 172 |
y_indices, x_indices = np.where(cls_mask > 0)
|
| 173 |
if len(x_indices) > 0 and len(y_indices) > 0:
|
| 174 |
x1, x2 = np.min(x_indices), np.max(x_indices)
|
|
@@ -192,44 +216,35 @@ def get_detection_data(image):
|
|
| 192 |
float(cls_center[1] / height),
|
| 193 |
],
|
| 194 |
"bbox": bbox_normalized,
|
| 195 |
-
"depth": float(mean_depth * 10), # Convert to meters
|
| 196 |
"color": [float(c / 255) for c in cls_clr],
|
| 197 |
"mask": cls_mask.tolist(),
|
| 198 |
-
"confidence": 1.0, #
|
| 199 |
}
|
| 200 |
detections.append(detection)
|
| 201 |
|
| 202 |
-
#
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
}
|
| 210 |
-
except AttributeError:
|
| 211 |
-
print("⚠️ Camera parameters are not properly set in depth_estimator.")
|
| 212 |
-
camera_params = {"fx": 0, "fy": 0, "cx": width // 2, "cy": height // 2}
|
| 213 |
-
|
| 214 |
-
# Generate point cloud data if needed
|
| 215 |
-
point_clouds = utils.generate_obj_pcd(depthmap, objects_data)
|
| 216 |
-
pcd_data = [
|
| 217 |
-
{"points": np.asarray(pcd.points).tolist(), "color": [float(c / 255) for c in color]}
|
| 218 |
-
for pcd, color in point_clouds
|
| 219 |
-
]
|
| 220 |
|
| 221 |
return {
|
| 222 |
"detections": detections,
|
| 223 |
-
"depth_map":
|
|
|
|
| 224 |
"camera_params": camera_params,
|
| 225 |
"image_size": {"width": width, "height": height},
|
| 226 |
-
"point_clouds": pcd_data,
|
| 227 |
}
|
| 228 |
|
| 229 |
except Exception as e:
|
| 230 |
print(f"🚨 Error in get_detection_data: {str(e)}")
|
| 231 |
return {"error": str(e)}
|
| 232 |
|
|
|
|
|
|
|
| 233 |
|
| 234 |
def cancel():
|
| 235 |
CANCEL_PROCESSING = True
|
|
|
|
| 143 |
"cy": depth_estimator.cy_depth
|
| 144 |
}
|
| 145 |
|
| 146 |
+
|
| 147 |
@spaces.GPU
|
| 148 |
def get_detection_data(image):
|
| 149 |
+
"""Get structured detection data with depth information, using Base64 image encoding."""
|
| 150 |
+
|
| 151 |
+
def decode_base64_image(base64_string):
|
| 152 |
+
"""Decodes Base64 string into a NumPy image."""
|
| 153 |
+
try:
|
| 154 |
+
img_data = base64.b64decode(base64_string)
|
| 155 |
+
img = Image.open(BytesIO(img_data))
|
| 156 |
+
img = np.array(img)
|
| 157 |
+
return cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # Convert to BGR for OpenCV
|
| 158 |
+
except Exception as e:
|
| 159 |
+
print(f"🚨 Error decoding base64 image: {e}")
|
| 160 |
+
return None
|
| 161 |
+
|
| 162 |
+
def encode_base64_image(image):
|
| 163 |
+
"""Encodes a NumPy image into a Base64 string."""
|
| 164 |
+
_, buffer = cv2.imencode('.png', image)
|
| 165 |
+
return base64.b64encode(buffer).decode("utf-8")
|
| 166 |
+
|
| 167 |
+
width, height = 640, 480 # Default values
|
| 168 |
|
| 169 |
try:
|
| 170 |
+
if isinstance(image, str): # Ensure we're handling a Base64 string
|
| 171 |
+
image = decode_base64_image(image)
|
| 172 |
+
if image is None:
|
| 173 |
+
return {"error": "Invalid base64 image data"}
|
| 174 |
+
|
| 175 |
+
# Resize image
|
| 176 |
image = utils.resize(image)
|
| 177 |
|
| 178 |
+
# Extract dimensions
|
| 179 |
if hasattr(image, "shape"):
|
| 180 |
+
height, width = image.shape[:2]
|
| 181 |
|
| 182 |
# Get detections and depth
|
| 183 |
image_segmentation, objects_data = img_seg.predict(image)
|
| 184 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
| 185 |
|
| 186 |
+
# Encode results as Base64
|
| 187 |
+
segmentation_b64 = encode_base64_image(image_segmentation)
|
| 188 |
+
depth_b64 = encode_base64_image(depth_colormap)
|
| 189 |
+
|
| 190 |
+
# Process detections
|
| 191 |
detections = []
|
| 192 |
for data in objects_data:
|
| 193 |
cls_id, cls_name, cls_center, cls_mask, cls_clr = data
|
|
|
|
|
|
|
| 194 |
masked_depth, mean_depth = utils.get_masked_depth(depthmap, cls_mask)
|
| 195 |
|
|
|
|
| 196 |
y_indices, x_indices = np.where(cls_mask > 0)
|
| 197 |
if len(x_indices) > 0 and len(y_indices) > 0:
|
| 198 |
x1, x2 = np.min(x_indices), np.max(x_indices)
|
|
|
|
| 216 |
float(cls_center[1] / height),
|
| 217 |
],
|
| 218 |
"bbox": bbox_normalized,
|
| 219 |
+
"depth": float(mean_depth * 10), # Convert to meters
|
| 220 |
"color": [float(c / 255) for c in cls_clr],
|
| 221 |
"mask": cls_mask.tolist(),
|
| 222 |
+
"confidence": 1.0, # Placeholder confidence
|
| 223 |
}
|
| 224 |
detections.append(detection)
|
| 225 |
|
| 226 |
+
# Camera parameters
|
| 227 |
+
camera_params = {
|
| 228 |
+
"fx": getattr(depth_estimator, "fx_depth", 0),
|
| 229 |
+
"fy": getattr(depth_estimator, "fy_depth", 0),
|
| 230 |
+
"cx": getattr(depth_estimator, "cx_depth", width // 2),
|
| 231 |
+
"cy": getattr(depth_estimator, "cy_depth", height // 2),
|
| 232 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
return {
|
| 235 |
"detections": detections,
|
| 236 |
+
"depth_map": depth_b64, # Returning depth as Base64 image
|
| 237 |
+
"segmentation": segmentation_b64, # Returning segmentation as Base64 image
|
| 238 |
"camera_params": camera_params,
|
| 239 |
"image_size": {"width": width, "height": height},
|
|
|
|
| 240 |
}
|
| 241 |
|
| 242 |
except Exception as e:
|
| 243 |
print(f"🚨 Error in get_detection_data: {str(e)}")
|
| 244 |
return {"error": str(e)}
|
| 245 |
|
| 246 |
+
|
| 247 |
+
|
| 248 |
|
| 249 |
def cancel():
|
| 250 |
CANCEL_PROCESSING = True
|