欢迎访问宙启技术站
智能推送

使用Python生成20个 边界框数据集(unique_boxes)

发布时间:2023-12-11 04:28:33

生成 边界框数据集可以通过多种方式实现。下面是一个使用Python生成20个随机 边界框数据集的示例代码。

import random

# 定义边界框类
class BoundingBox:
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2

    def __str__(self):
        return f'({self.x1}, {self.y1}) - ({self.x2}, {self.y2})'

    def __repr__(self):
        return str(self)

# 生成      边界框数据集
def generate_unique_boxes(num_boxes):
    unique_boxes = set()

    while len(unique_boxes) < num_boxes:
        x1 = random.randint(0, 100)
        y1 = random.randint(0, 100)
        x2 = random.randint(0, 100)
        y2 = random.randint(0, 100)

        # 确保生成的边界框不与现有的边界框重叠
        new_box = BoundingBox(x1, y1, x2, y2)
        is_unique = all(not new_box.intersects(box) for box in unique_boxes)

        if is_unique:
            unique_boxes.add(new_box)

    return unique_boxes

# 检查边界框是否相交
def intersects(self, other):
    return not (self.x2 < other.x1 or self.x1 > other.x2 or self.y2 < other.y1 or self.y1 > other.y2)

# 使用示例
if __name__ == '__main__':
    boxes = generate_unique_boxes(20)
    for i, box in enumerate(boxes):
        print(f'Box {i+1}: {box}')

上述代码中,我们首先定义了一个 BoundingBox 类来表示边界框对象。然后,在 generate_unique_boxes 函数中,我们使用一个 while 循环来生成随机的边界框,直到生成指定数量的 边界框为止。为了确保生成的边界框是 的,我们在每次生成新的边界框之后,通过检查其与现有边界框是否相交来判断。

最后,在示例中,我们调用 generate_unique_boxes 函数来生成20个随机 边界框,并输出每个边界框的信息。

这是一个基本的例子,你可以根据自己的需求进行修改和扩展。