visual genome数据集本地标注

代码

from visual_genome import local
import matplotlib.pyplot as plt
import requests
from PIL import Image
from io import BytesIO
from matplotlib.patches import Rectangle

data_dir = 'E:\pythonProject1'
save_dir = 'E:/pythonProject1/'
image_id = 1
save_path = save_dir + str(image_id) + '.png'
print(save_path)


ids = local.get_all_image_data(data_dir)
image = ids[image_id-1]
print(image)
regions = local.get_all_region_descriptions(data_dir)
regions = regions[image_id-1]
print(regions)

fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)


def visualize_regions(image, regions):
    print(image.url)
    response = requests.get(image.url)

    img = Image.open(BytesIO(response.content))
    # 显示图像
    plt.imshow(img)
    ax = plt.gca()

    # 假设 regions 是一个包含矩形区域信息的列表,每个区域有 x, y, width, height, 和 phrase 属性
    for region in regions:
        # 计算矩形的左下角坐标(matplotlib的坐标系统以图像的左下角为原点)
        left = region.x
        bottom = region.y
        width = region.width
        height = region.height

        # 绘制矩形框
        rect = Rectangle((left, bottom), width, height, fill=False, edgecolor='red', linewidth=3)
        ax.add_patch(rect)

        # 在矩形区域中添加文本
        ax.text(left, bottom, region.phrase, style='italic',
                bbox={'facecolor': 'white', 'alpha': 0.7, 'pad': 10},
                verticalalignment='top', horizontalalignment='left')

    # 关闭x和y轴的刻度
    plt.tick_params(labelbottom='off', labelleft='off')

    # 保存路径
    save_path = save_dir + str(image_id) + '.png'

    # 保存图像
    plt.savefig(save_path, bbox_inches='tight')  # bbox_inches='tight' 可以确保保存的图像不包含额外的空白

    # 显示图像
    # plt.show()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值