用coco格式标签在图像上绘制box

 

import json
import os
from PIL import Image, ImageDraw

def draw_bbox_on_image(image_path, bbox_list, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)

    for bbox in bbox_list:
        x, y, w, h = bbox
        draw.rectangle([x, y, x + w, y + h], outline="red", width=10)

    image.save(output_path)

def process_coco_json(json_file, image_folder, output_folder):
    with open(json_file, "r") as f:
        data = json.load(f)

    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    image_id_to_bbox_list = {}
    for annotation in data["annotations"]:
        image_id = annotation["image_id"]
        bbox = annotation["bbox"]
        if image_id not in image_id_to_bbox_list:
            image_id_to_bbox_list[image_id] = []
        image_id_to_bbox_list[image_id].append(bbox)

    for image in data["images"]:
        image_id = image["id"]
        image_path = os.path.join(image_folder, image["file_name"])
        output_path = os.path.join(output_folder, image["file_name"])

        if image_id in image_id_to_bbox_list:
            draw_bbox_on_image(image_path, image_id_to_bbox_list[image_id], output_path)


train_json = "./6354/val_6354.json"# coco标签
train_folder = "./6354/image"# 要使用的绘制图像的文件夹
train_output_folder = "./6354/label_vis" # 输出文件夹


process_coco_json(train_json, train_folder, train_output_folder)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值