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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -20
app.py CHANGED
@@ -149,37 +149,57 @@ def get_camera_matrix(depth_estimator):
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,
@@ -188,7 +208,6 @@ def get_detection_data(image_data):
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),
@@ -200,7 +219,6 @@ def get_detection_data(image_data):
200
  "cy": depth_estimator.cy_depth
201
  }
202
  }
203
-
204
  return response
205
 
206
  except Exception as e:
 
149
 
150
  @spaces.GPU
151
  def get_detection_data(image_data):
152
+ """
153
+ Get structured detection data with depth information, using a nested JSON + Base64 image.
154
+ Expects Lens Studio to send:
155
+ {
156
+ "image": {
157
+ "image": {
158
+ "data": "data:image/png;base64,<BASE64>"
159
+ }
160
+ }
161
+ }
162
+ or just a direct string.
163
+ """
164
  try:
165
+ # 1) Extract the nested "data" string if it's a dict
166
  if isinstance(image_data, dict):
167
+ # For the structure: {"image": {"image": {"data": "data:image/png;base64,..."}}}
168
+ nested_dict = image_data.get("image", {}).get("image", {})
169
+ full_data_url = nested_dict.get("data", "")
170
  else:
171
+ # If not a dict, assume it's a direct string
172
+ full_data_url = image_data
173
+
174
+ if not full_data_url:
175
+ return {"error": "No base64 data found in input."}
176
 
177
+ # 2) Strip the "data:image/..." prefix if present
178
+ if full_data_url.startswith("data:image"):
179
+ # split once on comma => ["data:image/png;base64", "<BASE64>"]
180
+ _, b64_string = full_data_url.split(",", 1)
181
+ else:
182
+ b64_string = full_data_url
183
 
184
+ # 3) Decode base64 -> PIL -> OpenCV
185
+ img_data = base64.b64decode(b64_string)
186
+ img = Image.open(BytesIO(img_data))
187
+ img = np.array(img)
188
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
 
 
 
189
 
190
+ # 4) Process image
191
  image = utils.resize(img)
192
  image_segmentation, objects_data = img_seg.predict(image)
193
  depthmap, depth_colormap = depth_estimator.make_prediction(image)
194
 
195
+ # 5) Prepare structured response
196
  processed_objects = []
197
  for obj in objects_data:
198
  cls_id, cls_name, center, mask, color = obj
199
+ depth_value = depth_at_center(
200
+ depthmap,
201
+ [center[0] - 10, center[1] - 10, center[0] + 10, center[1] + 10]
202
+ )
203
  processed_objects.append({
204
  "class_id": int(cls_id),
205
  "class_name": cls_name,
 
208
  "color": [int(c) for c in color]
209
  })
210
 
 
211
  response = {
212
  "detections": processed_objects,
213
  "depth_map": encode_base64_image(depth_colormap),
 
219
  "cy": depth_estimator.cy_depth
220
  }
221
  }
 
222
  return response
223
 
224
  except Exception as e: