代码
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()