Alessio Grancini commited on
Commit
7ac5451
·
verified ·
1 Parent(s): 3ece440

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -47
app.py CHANGED
@@ -148,69 +148,65 @@ def get_camera_matrix(depth_estimator):
148
 
149
 
150
  @spaces.GPU
151
- def get_detection_data(image):
152
  """Get structured detection data with depth information, using Base64 image encoding."""
 
 
 
 
 
 
 
 
 
153
 
154
- def decode_base64_image(base64_string):
155
- """Decodes Base64 string into a NumPy image."""
156
  try:
157
- print(f"🔍 Received Base64 String (Truncated): {base64_string[:50]}...") # Debugging
158
- img_data = base64.b64decode(base64_string)
159
  img = Image.open(BytesIO(img_data))
160
  img = np.array(img)
161
- return cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # Convert to BGR for OpenCV
162
  except Exception as e:
163
- print(f"🚨 Error decoding base64 image: {e}")
164
- return None
165
-
166
- def encode_base64_image(image):
167
- """Encodes a NumPy image into a Base64 string."""
168
- try:
169
- _, buffer = cv2.imencode('.png', image)
170
- return base64.b64encode(buffer).decode("utf-8")
171
- except Exception as e:
172
- print(f"🚨 Error encoding image to Base64: {e}")
173
- return None
174
-
175
- try:
176
- if not isinstance(image, str):
177
- print("🚨 Error: Expected Base64 string but received:", type(image))
178
- return {"error": "Invalid input format. Expected Base64-encoded image."}
179
 
180
- image = decode_base64_image(image)
181
- if image is None:
182
- return {"error": "Base64 decoding failed. Ensure correct encoding."}
183
-
184
- # Resize image
185
- image = utils.resize(image)
186
-
187
- # Extract dimensions
188
- height, width = image.shape[:2]
189
-
190
- # Get detections and depth
191
  image_segmentation, objects_data = img_seg.predict(image)
192
  depthmap, depth_colormap = depth_estimator.make_prediction(image)
193
 
194
- # Encode results as Base64
195
- segmentation_b64 = encode_base64_image(image_segmentation)
196
- depth_b64 = encode_base64_image(depth_colormap)
197
-
198
- if segmentation_b64 is None or depth_b64 is None:
199
- return {"error": "Failed to encode output images."}
200
-
201
- return {
202
- "detections": objects_data, # Keeping as original
203
- "depth_map": depth_b64,
204
- "segmentation": segmentation_b64,
205
- "image_size": {"width": width, "height": height},
 
 
 
 
 
 
 
 
 
 
 
 
 
206
  }
207
 
 
 
208
  except Exception as e:
209
  print(f"🚨 Error in get_detection_data: {str(e)}")
210
  return {"error": str(e)}
211
 
212
-
213
-
214
 
215
  def cancel():
216
  CANCEL_PROCESSING = True
 
148
 
149
 
150
  @spaces.GPU
151
+ def get_detection_data(image_data):
152
  """Get structured detection data with depth information, using Base64 image encoding."""
153
+ try:
154
+ # Handle both string and dict input formats
155
+ if isinstance(image_data, dict):
156
+ image = image_data.get('data', '')
157
+ else:
158
+ image = image_data
159
+
160
+ if not isinstance(image, str):
161
+ return {"error": f"Invalid input format. Expected string or dict with 'data' key, got {type(image)}"}
162
 
163
+ # Decode base64 image
 
164
  try:
165
+ img_data = base64.b64decode(image)
 
166
  img = Image.open(BytesIO(img_data))
167
  img = np.array(img)
168
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
169
  except Exception as e:
170
+ return {"error": f"Base64 decoding failed: {str(e)}"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
+ # Process image
173
+ image = utils.resize(img)
 
 
 
 
 
 
 
 
 
174
  image_segmentation, objects_data = img_seg.predict(image)
175
  depthmap, depth_colormap = depth_estimator.make_prediction(image)
176
 
177
+ # Prepare structured response with spatial data
178
+ processed_objects = []
179
+ for obj in objects_data:
180
+ cls_id, cls_name, center, mask, color = obj
181
+ depth_value = depth_at_center(depthmap, [center[0]-10, center[1]-10, center[0]+10, center[1]+10])
182
+
183
+ processed_objects.append({
184
+ "class_id": int(cls_id),
185
+ "class_name": cls_name,
186
+ "center": {"x": float(center[0]), "y": float(center[1])},
187
+ "depth": float(depth_value),
188
+ "color": [int(c) for c in color]
189
+ })
190
+
191
+ # Encode results
192
+ response = {
193
+ "detections": processed_objects,
194
+ "depth_map": encode_base64_image(depth_colormap),
195
+ "segmentation": encode_base64_image(image_segmentation),
196
+ "camera_matrix": {
197
+ "fx": depth_estimator.fx_depth,
198
+ "fy": depth_estimator.fy_depth,
199
+ "cx": depth_estimator.cx_depth,
200
+ "cy": depth_estimator.cy_depth
201
+ }
202
  }
203
 
204
+ return response
205
+
206
  except Exception as e:
207
  print(f"🚨 Error in get_detection_data: {str(e)}")
208
  return {"error": str(e)}
209
 
 
 
210
 
211
  def cancel():
212
  CANCEL_PROCESSING = True