|
@@ -0,0 +1,129 @@
|
|
|
+package com.fdkankan.fusion.common.ai;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+/**
|
|
|
+ * @author Xiewj
|
|
|
+ * @date 2025/4/15
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+public class BBoxHierarchy {
|
|
|
+
|
|
|
+ // 定义一个类表示 bbox 和其他属性
|
|
|
+ static class Item {
|
|
|
+ int[] bbox; // [x, y, w, h]
|
|
|
+ String name;
|
|
|
+ String category;
|
|
|
+
|
|
|
+ public Item(int[] bbox, String name, String category) {
|
|
|
+ this.bbox = bbox;
|
|
|
+ this.name = name;
|
|
|
+ this.category = category;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断 bbox1 是否包含 bbox2
|
|
|
+ public static boolean contains(int[] bbox1, int[] bbox2) {
|
|
|
+ int x1 = bbox1[0], y1 = bbox1[1], w1 = bbox1[2], h1 = bbox1[3];
|
|
|
+ int x2 = bbox2[0], y2 = bbox2[1], w2 = bbox2[2], h2 = bbox2[3];
|
|
|
+
|
|
|
+ // 计算 bbox1 的右下角坐标
|
|
|
+ int x1Max = x1 + w1;
|
|
|
+ int y1Max = y1 + h1;
|
|
|
+
|
|
|
+ // 计算 bbox2 的右下角坐标
|
|
|
+ int x2Max = x2 + w2;
|
|
|
+ int y2Max = y2 + h2;
|
|
|
+
|
|
|
+ // 判断 bbox2 是否完全在 bbox1 内部
|
|
|
+ return x1 <= x2 && y1 <= y2 && x1Max >= x2Max && y1Max >= y2Max;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建父子层级关系
|
|
|
+ public static ObjectNode buildHierarchy(List<Item> items) {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ ObjectNode root = mapper.createObjectNode();
|
|
|
+ ArrayNode children = mapper.createArrayNode();
|
|
|
+
|
|
|
+ // 遍历每个 item,寻找其父节点
|
|
|
+ for (Item item : items) {
|
|
|
+ ObjectNode node = mapper.createObjectNode();
|
|
|
+ node.put("name", item.name);
|
|
|
+ node.put("category", item.category);
|
|
|
+ node.putArray("bbox").add(item.bbox[0]).add(item.bbox[1]).add(item.bbox[2]).add(item.bbox[3]);
|
|
|
+ node.set("children", mapper.createArrayNode());
|
|
|
+
|
|
|
+ boolean hasParent = false;
|
|
|
+
|
|
|
+ for (Item parent : items) {
|
|
|
+ if (!item.equals(parent) && contains(parent.bbox, item.bbox)) {
|
|
|
+ // 如果找到父节点,将当前节点加入父节点的 children
|
|
|
+ ((ArrayNode) findNode(children, parent).get("children")).add(node);
|
|
|
+ hasParent = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!hasParent) {
|
|
|
+ // 如果没有父节点,直接加入根节点的 children
|
|
|
+ children.add(node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ root.set("children", children);
|
|
|
+ return root;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找指定节点
|
|
|
+ private static ObjectNode findNode(ArrayNode nodes, Item target) {
|
|
|
+ for (int i = 0; i < nodes.size(); i++) {
|
|
|
+ ObjectNode node = (ObjectNode) nodes.get(i);
|
|
|
+ int[] bbox = new int[]{
|
|
|
+ node.get("bbox").get(0).asInt(),
|
|
|
+ node.get("bbox").get(1).asInt(),
|
|
|
+ node.get("bbox").get(2).asInt(),
|
|
|
+ node.get("bbox").get(3).asInt()
|
|
|
+ };
|
|
|
+
|
|
|
+ if (bbox[0] == target.bbox[0] && bbox[1] == target.bbox[1] &&
|
|
|
+ bbox[2] == target.bbox[2] && bbox[3] == target.bbox[3]) {
|
|
|
+ return node;
|
|
|
+ }
|
|
|
+
|
|
|
+ ObjectNode found = findNode((ArrayNode) node.get("children"), target);
|
|
|
+ if (found != null) {
|
|
|
+ return found;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ // 输入数据
|
|
|
+ List<Item> items = new ArrayList<>();
|
|
|
+ items.add(new Item(new int[]{399, 370, 549, 503}, "卧室", "Tag_bedroom"));
|
|
|
+ items.add(new Item(new int[]{184, 221, 307, 275}, "组合沙发", "CombinationSofa"));
|
|
|
+ items.add(new Item(new int[]{132, 169, 190, 218}, "组合沙发", "CombinationSofa"));
|
|
|
+ items.add(new Item(new int[]{156, 423, 364, 588}, "卧室", "Tag_bedroom"));
|
|
|
+ items.add(new Item(new int[]{452, 390, 528, 503}, "双人床", "DoubleBed"));
|
|
|
+ items.add(new Item(new int[]{408, 204, 464, 242}, "书桌", "Desk"));
|
|
|
+ items.add(new Item(new int[]{296, 171, 320, 195}, "椅子", "Chair"));
|
|
|
+ items.add(new Item(new int[]{70, 104, 619, 279}, "客厅", "Tag_livingroom"));
|
|
|
+ items.add(new Item(new int[]{499, 197, 633, 309}, "厨房", "Tag_kitchenroom"));
|
|
|
+ items.add(new Item(new int[]{411, 181, 447, 203}, "椅子", "Chair"));
|
|
|
+ items.add(new Item(new int[]{206, 467, 300, 537}, "双人床", "DoubleBed"));
|
|
|
+ items.add(new Item(new int[]{534, 517, 543, 553}, "窗户", "Window"));
|
|
|
+ items.add(new Item(new int[]{76, 107, 84, 302}, "窗户", "Window"));
|
|
|
+
|
|
|
+ // 构建层级关系
|
|
|
+ ObjectNode hierarchy = buildHierarchy(items);
|
|
|
+
|
|
|
+ // 输出结果
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(hierarchy));
|
|
|
+ }
|
|
|
+}
|