You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

parse_bbox.py 1.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """TODO: Add docstring."""
  2. import json
  3. import os
  4. import numpy as np
  5. import pyarrow as pa
  6. from dora import Node
  7. node = Node()
  8. IMAGE_RESIZE_RATIO = float(os.getenv("IMAGE_RESIZE_RATIO", "1.0"))
  9. def extract_bboxes(json_text):
  10. """Extract bounding boxes from a JSON string with markdown markers and return them as a NumPy array.
  11. Parameters
  12. ----------
  13. json_text : str
  14. JSON string containing bounding box data, including ```json markers.
  15. Returns
  16. -------
  17. np.ndarray: NumPy array of bounding boxes.
  18. """
  19. # Ensure all lines are stripped of whitespace and markers
  20. lines = json_text.strip().splitlines()
  21. # Filter out lines that are markdown markers
  22. clean_lines = [line for line in lines if not line.strip().startswith("```")]
  23. # Join the lines back into a single string
  24. clean_text = "\n".join(clean_lines)
  25. # Parse the cleaned JSON text
  26. try:
  27. data = json.loads(clean_text)
  28. # Extract bounding boxes
  29. bboxes = [item["bbox_2d"] for item in data]
  30. labels = [item["label"] for item in data]
  31. return np.array(bboxes), np.array(labels)
  32. except Exception as _e: # noqa
  33. pass
  34. return None, None
  35. for event in node:
  36. if event["type"] == "INPUT":
  37. text = event["value"][0].as_py()
  38. image_id = event["metadata"]["image_id"]
  39. bboxes, labels = extract_bboxes(text)
  40. if bboxes is not None and len(bboxes) > 0:
  41. bboxes = bboxes * int(1 / IMAGE_RESIZE_RATIO)
  42. node.send_output(
  43. "bbox",
  44. pa.array(bboxes.ravel()),
  45. metadata={"encoding": "xyxy", "image_id": image_id},
  46. )