当前位置:网站首页>plt. GCA () picture frame and label

plt. GCA () picture frame and label

2022-06-25 12:01:00 On the art of moving bricks

Draw a box and label

Mainly from json In file , Get the coordinate position of the target .
Frame the picture json file information .
 Insert picture description here
Code

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

jsonpath = ' ROM. .json'  #  load json Data and information 
with open(jsonpath,encoding = 'utf-8') as f:
    data=json.load(f)


image = ' ROM. .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() #  Frame object 

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})# among label Is the tag name you want to type 

 Insert picture description here

Draw multiple boxes and labels

Tagged picture json file information
 Insert picture description here
Code :

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

jsonpath = ' mei .json'  #  load json Data and information 
with open(jsonpath,encoding = 'utf-8') as f:
    data=json.load(f)


image = ' mei .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() #  Frame object 
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})# among label Is the tag name you want to type 

 Insert picture description here

原网站

版权声明
本文为[On the art of moving bricks]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251145411964.html