当前位置:网站首页>plt.gca()画框及打标签

plt.gca()画框及打标签

2022-06-25 11:46:00 论搬砖的艺术

画一个框及标签

主要就是从 json文件中,获取目标物的坐标位置。
对图片画好框的json文件信息。
在这里插入图片描述
代码

import json
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np

jsonpath = '罗.json'  # 加载json数据信息
with open(jsonpath,encoding = 'utf-8') as f:
    data=json.load(f)


image = '罗.jpg'
img = Image.open(image)
np_img = np.array(img)
print(np_img.shape)
plt.figure(figsize=(15,9))
plt.imshow(img)

current_axis = plt.gca() # 画框对象

xmin = data['outputs']['object'][0]['bndbox']['xmin']
ymin = data['outputs']['object'][0]['bndbox']['ymin']
xmax = data['outputs']['object'][0]['bndbox']['xmax']
ymax = data['outputs']['object'][0]['bndbox']['ymax']
current_axis.add_patch(plt.Rectangle((xmin, ymin), xmax-xmin, ymax-ymin, color='green', fill=False, linewidth=2))
current_axis.text(xmin, ymin, 'c luo', size='x-large', color='white', bbox={
    'facecolor':'green', 'alpha':1.0})#其中label是想要打的标签名

在这里插入图片描述

画多个框及标签

打好标签的图片json文件信息
在这里插入图片描述
代码:

import json
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np

jsonpath = '梅.json'  # 加载json数据信息
with open(jsonpath,encoding = 'utf-8') as f:
    data=json.load(f)


image = '梅.jpg'
img = Image.open(image)
np_img = np.array(img)
print(np_img.shape)
plt.figure(figsize=(15,9))
plt.imshow(img)

current_axis = plt.gca() # 画框对象
data = data['outputs']['object']
for i in data:
    print(i)
    name = i['name']
    xmin = i['bndbox']['xmin']
    ymin = i['bndbox']['ymin']
    xmax = i['bndbox']['xmax']
    ymax = i['bndbox']['ymax']
    current_axis.add_patch(plt.Rectangle((xmin, ymin), xmax-xmin, ymax-ymin, color='green', fill=False, linewidth=2))
    current_axis.text(xmin, ymin, name, size='x-large', color='white', bbox={
    'facecolor':'green', 'alpha':1.0})#其中label是想要打的标签名

在这里插入图片描述

原网站

版权声明
本文为[论搬砖的艺术]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_53817374/article/details/125444983