from PIL import Image, ImageDraw
def draw_bboxes(image, bboxes, color="red", thickness=2):
draw = ImageDraw.Draw(image)
for bbox in bboxes:
bbox = tuple(map(int, bbox)) # 将浮点数类型的坐标转换为整数类型
draw.rectangle(bbox, outline=color, width=thickness)
del draw
# 示例用法
image_path = "D:/CodeProject/CLIP+SAM/datasets/Objects365/Images/train/obj365_train_000000000003.jpg" # 原始图像路径
bboxes = [
(46, 318, 400, 656), (0, 0, 330, 359), (373, 268, 393, 283), (73, 322, 98, 374), (280, 269, 307, 306), (40, 305, 69, 353), \
(243, 260, 268, 294), (54, 364, 168, 414), (279, 305, 352, 330), (26, 346, 48, 396), (12, 331, 76, 356), (201, 285, 260, 305), \
(0, 270, 106, 327)
] # 边界框坐标信息
image = Image.open(image_path).convert("RGB")
draw_bboxes(image, bboxes)
# 显示绘制边界框后的图像
image.show()
这行代码定义了一个名为 draw_bboxes 的函数,它接受三个必需参数和两个可选参数:
执行以下操作:
将边界框坐标的浮点数类型转换为整数类型,以便绘制函数接受整数坐标。
调用 draw.rectangle 方法,在图像上绘制矩形边界框,使用指定的颜色和线条粗细。
综合起来,这个函数的目的是在图像上绘制给定边界框列表的矩形框。
需要注意的是,该函数使用了 ImageDraw 类,它是 PIL(Python Imaging Library)中的一个图像绘制工具类。所以在使用该函数之前,你需要确保已经导入了相应的模块,例如 from PIL import ImageDraw。
我们展示一下输出结果为:
def draw_bboxes(image, bboxes, color = "red", thickness = 2):
draw = ImageDraw.Draw(image)
for bbox in bboxes:
bbox = tuple(map(int, bbox)) # 将浮点数类型的坐标转换为整数类型
draw.rectangle(bbox, outline=color, width=thickness)
del draw
def draw_bboxes_2(image, bboxes, color = "blue", thickness = 1):
draw = ImageDraw.Draw(image)
for bbox in bboxes:
bbox = tuple(map(int, bbox)) # 将浮点数类型的坐标转换为整数类型
draw.rectangle(bbox, outline=color, width=thickness)
del draw
# 示例用法
image_path = "demo.jpg"
bboxes = [
(236, 41, 263, 180), (218, 174, 248, 207), (306, 10, 332, 175), (396, 0, 426, 162),
(189, 281, 528, 512), (255, 293, 388, 512), (193, 285, 302, 476),
(146, 280, 213, 445), (339, 113, 450, 229), (406, 49, 508, 177), (499, 82, 537, 229),
(521, 0, 683, 167), (5, 281, 88, 389), (162, 275, 212, 366), (10, 79, 67, 235),
(142, 97, 196, 234), (325, 275, 533, 411), (65, 82, 154, 165), (462, 251, 488, 285),
(422, 277, 502, 286), (501, 254, 512, 290), (511, 255, 521, 288), (532, 151, 676, 451),
(61, 171, 163, 198), (64, 268, 166, 291), (83, 291, 166, 377), (502, 254, 520, 283),
(11, 250, 32, 273), (12, 272, 34, 282), (288, 284, 332, 301)
]
image = Image.open(image_path).convert("RGB")
draw_bboxes(image, bboxes)
draw_bboxes_2(image, bbox_list)
image.show()
显示结果为:
def calculate_iou(bbox1, bbox2):
x1_min, y1_min, x1_max, y1_max = bbox1
x2_min, y2_min, x2_max, y2_max = bbox2
# 计算两个边界框的相交部分
x_min = max(x1_min, x2_min)
y_min = max(y1_min, y2_min)
x_max = min(x1_max, x2_max)
y_max = min(y1_max, y2_max)
intersection = max(0, x_max - x_min) * max(0, y_max - y_min)
# 计算两个边界框的面积
area1 = (x1_max - x1_min) * (y1_max - y1_min)
area2 = (x2_max - x2_min) * (y2_max - y2_min)
# 计算 IoU
iou = intersection / (area1 + area2 - intersection)
return iou
bboxes = [
(236, 41, 263, 180), (218, 174, 248, 207), (306, 10, 332, 175), (396, 0, 426, 162),
(189, 281, 528, 512), (255, 293, 388, 512), (193, 285, 302, 476),
(146, 280, 213, 445), (339, 113, 450, 229), (406, 49, 508, 177), (499, 82, 537, 229),
(521, 0, 683, 167), (5, 281, 88, 389), (162, 275, 212, 366), (10, 79, 67, 235),
(142, 97, 196, 234), (325, 275, 533, 411), (65, 82, 154, 165), (462, 251, 488, 285),
(422, 277, 502, 286), (501, 254, 512, 290), (511, 255, 521, 288), (532, 151, 676, 451),
(61, 171, 163, 198), (64, 268, 166, 291), (83, 291, 166, 377), (502, 254, 520, 283),
(11, 250, 32, 273), (12, 272, 34, 282), (288, 284, 332, 301)
]
selected_bboxes = []
for pred_bbox in bbox_list:
for gt_bbox in bboxes:
iou_scores = [calculate_iou(pred_bbox, gt_bbox) for gt_bbox in bboxes]
max_iou = max(iou_scores)
if max_iou > 0.4:
selected_bboxes.append(pred_bbox)
print(selected_bboxes)
输出的结果为:
[(534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (534, 22, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (535, 155, 676, 453), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (227, 287, 526, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (278, 319, 519, 509), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (537, 155, 675, 351), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (236, 297, 382, 510), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (416, 52, 530, 175), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (332, 165, 533, 397), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (4, 78, 67, 230), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (65, 87, 161, 194), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (6, 280, 158, 386), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (417, 53, 505, 161), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (342, 109, 421, 227), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (74, 270, 164, 375), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (6, 282, 85, 386), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (197, 288, 271, 475), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (327, 274, 533, 407), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (86, 291, 162, 375), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (143, 98, 196, 229), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (151, 279, 209, 445), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (25, 285, 82, 373), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (87, 291, 162, 350), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (507, 83, 535, 226), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (144, 143, 195, 229), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (65, 164, 161, 194), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (400, 0, 425, 162), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (308, 12, 331, 173), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (73, 270, 163, 292), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (241, 41, 260, 180), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 222), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (219, 175, 247, 207), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (281, 282, 332, 300), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (286, 286, 331, 299), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (463, 252, 487, 283), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280), (14, 272, 33, 280)]
将其同时进行可视化:
def draw_bboxes(image, bboxes, color = "red", thickness = 2):
draw = ImageDraw.Draw(image)
for bbox in bboxes:
bbox = tuple(map(int, bbox)) # 将浮点数类型的坐标转换为整数类型
draw.rectangle(bbox, outline=color, width=thickness)
del draw
def draw_bboxes_2(image, bboxes, color = "blue", thickness = 1):
draw = ImageDraw.Draw(image)
for bbox in bboxes:
bbox = tuple(map(int, bbox)) # 将浮点数类型的坐标转换为整数类型
draw.rectangle(bbox, outline=color, width=thickness)
del draw
# 示例用法
image_path = "demo.jpg"
bboxes = [
(236, 41, 263, 180), (218, 174, 248, 207), (306, 10, 332, 175), (396, 0, 426, 162),
(189, 281, 528, 512), (255, 293, 388, 512), (193, 285, 302, 476),
(146, 280, 213, 445), (339, 113, 450, 229), (406, 49, 508, 177), (499, 82, 537, 229),
(521, 0, 683, 167), (5, 281, 88, 389), (162, 275, 212, 366), (10, 79, 67, 235),
(142, 97, 196, 234), (325, 275, 533, 411), (65, 82, 154, 165), (462, 251, 488, 285),
(422, 277, 502, 286), (501, 254, 512, 290), (511, 255, 521, 288), (532, 151, 676, 451),
(61, 171, 163, 198), (64, 268, 166, 291), (83, 291, 166, 377), (502, 254, 520, 283),
(11, 250, 32, 273), (12, 272, 34, 282), (288, 284, 332, 301)
]
image = Image.open(image_path).convert("RGB")
draw_bboxes(image, bboxes)
draw_bboxes_2(image, selected_bboxes)
image.show()
输出结果为: