处理所有基本图形(SAM3结果 + CV补充检测),生成DrawIO XML。 :param image: 原始图像 (BGR) :param sam3_elements: SAM3提取的元素字典 :return: 格式化的XML字符串
(image: np.ndarray, sam3_elements: dict)
| 1599 | |
| 1600 | # ======================== 独立处理函数 ======================== |
| 1601 | def process_basic_shapes(image: np.ndarray, sam3_elements: dict) -> str: |
| 1602 | """ |
| 1603 | 处理所有基本图形(SAM3结果 + CV补充检测),生成DrawIO XML。 |
| 1604 | |
| 1605 | :param image: 原始图像 (BGR) |
| 1606 | :param sam3_elements: SAM3提取的元素字典 |
| 1607 | :return: 格式化的XML字符串 |
| 1608 | """ |
| 1609 | h, w = image.shape[:2] |
| 1610 | |
| 1611 | # 运行CV补充检测 |
| 1612 | cv_results = detect_rectangles_robust(image, sam3_elements, { |
| 1613 | "min_area_ratio": 0.07, |
| 1614 | "max_area_ratio": 0.95 |
| 1615 | }) |
| 1616 | |
| 1617 | # 收集所有需要绘制的元素 |
| 1618 | containers_list = [] |
| 1619 | shapes_list = [] |
| 1620 | |
| 1621 | # 来自 SAM3 的 container |
| 1622 | if "container" in sam3_elements: |
| 1623 | for item in sam3_elements["container"]: |
| 1624 | item_copy = item.copy() |
| 1625 | item_copy["_type"] = "container" |
| 1626 | item_copy["_source"] = "sam3" |
| 1627 | containers_list.append(item_copy) |
| 1628 | |
| 1629 | # 来自 CV 检测的 containers |
| 1630 | for item in cv_results["containers"]: |
| 1631 | item_copy = item.copy() |
| 1632 | item_copy["_type"] = "container" |
| 1633 | item_copy["_source"] = "cv" |
| 1634 | containers_list.append(item_copy) |
| 1635 | |
| 1636 | # 来自 SAM3 的其他形状 |
| 1637 | for key, items in sam3_elements.items(): |
| 1638 | if key in VECTOR_TYPES and key != "container": |
| 1639 | for item in items: |
| 1640 | item_copy = item.copy() |
| 1641 | item_copy["_type"] = key |
| 1642 | item_copy["_source"] = "sam3" |
| 1643 | shapes_list.append(item_copy) |
| 1644 | |
| 1645 | # 来自 CV 检测的 rectangles |
| 1646 | for item in cv_results["rectangles"]: |
| 1647 | item_copy = item.copy() |
| 1648 | item_copy["_type"] = "rectangle" |
| 1649 | item_copy["_source"] = "cv" |
| 1650 | shapes_list.append(item_copy) |
| 1651 | |
| 1652 | # 排序:面积大的在底层 |
| 1653 | def calculate_element_area(bbox): |
| 1654 | return (bbox[2] - bbox[0]) * (bbox[3] - bbox[1]) |
| 1655 | |
| 1656 | containers_list.sort(key=lambda x: calculate_element_area(x["bbox"]), reverse=True) |
| 1657 | shapes_list.sort(key=lambda x: calculate_element_area(x["bbox"]), reverse=True) |
| 1658 |
nothing calls this directly
no test coverage detected