davanstrien HF Staff commited on
Commit
67f6a39
·
1 Parent(s): 2def010

Refactor process_batch to return detections in list format for improved consistency

Browse files
Files changed (1) hide show
  1. detect-objects.py +14 -24
detect-objects.py CHANGED
@@ -251,20 +251,16 @@ def process_batch(
251
 
252
  except Exception as e:
253
  logger.warning(f"⚠️ Failed to process batch: {e}")
254
- # Return empty detections for all images in batch (dict-of-lists format)
255
- num_images = len(pil_images)
256
  return {
257
- "objects": {
258
- "bbox": [[] for _ in range(num_images)],
259
- "category": [[] for _ in range(num_images)],
260
- "score": [[] for _ in range(num_images)],
261
- }
262
  }
263
 
264
- # Convert to HuggingFace object detection format (dict-of-lists)
265
- batch_bboxes = []
266
- batch_categories = []
267
- batch_scores = []
268
 
269
  for result in results:
270
  boxes = result.get("boxes", torch.tensor([]))
@@ -272,9 +268,7 @@ def process_batch(
272
 
273
  # Handle empty results
274
  if len(boxes) == 0:
275
- batch_bboxes.append([])
276
- batch_categories.append([])
277
- batch_scores.append([])
278
  continue
279
 
280
  # Build lists for this image
@@ -291,17 +285,13 @@ def process_batch(
291
  image_categories.append(0) # Single class, always index 0
292
  image_scores.append(float(score))
293
 
294
- batch_bboxes.append(image_bboxes)
295
- batch_categories.append(image_categories)
296
- batch_scores.append(image_scores)
 
 
297
 
298
- return {
299
- "objects": {
300
- "bbox": batch_bboxes,
301
- "category": batch_categories,
302
- "score": batch_scores,
303
- }
304
- }
305
 
306
 
307
  def main():
 
251
 
252
  except Exception as e:
253
  logger.warning(f"⚠️ Failed to process batch: {e}")
254
+ # Return empty detections for all images in batch
 
255
  return {
256
+ "objects": [
257
+ {"bbox": [], "category": [], "score": []}
258
+ for _ in range(len(pil_images))
259
+ ]
 
260
  }
261
 
262
+ # Convert to HuggingFace object detection format (dict-of-lists per image)
263
+ batch_objects = []
 
 
264
 
265
  for result in results:
266
  boxes = result.get("boxes", torch.tensor([]))
 
268
 
269
  # Handle empty results
270
  if len(boxes) == 0:
271
+ batch_objects.append({"bbox": [], "category": [], "score": []})
 
 
272
  continue
273
 
274
  # Build lists for this image
 
285
  image_categories.append(0) # Single class, always index 0
286
  image_scores.append(float(score))
287
 
288
+ batch_objects.append({
289
+ "bbox": image_bboxes,
290
+ "category": image_categories,
291
+ "score": image_scores,
292
+ })
293
 
294
+ return {"objects": batch_objects}
 
 
 
 
 
 
295
 
296
 
297
  def main():