首页 > 解决方案 > 如何在python中使用json文件可视化图像

问题描述

我有一个带注释的 JSON 文件,如下所示,我想在 JSON 文件 python 中用不同的对象绘制带注释的图像。我该如何进行

我正在尝试使用 JSON 文件在图像周围绘制边界框。我怎样才能在python中进行

{"objects": [{"label": "object", "position": [[0, "car", [396.5, 770.25, 43.0, 76.5]], [1, "car", [1045.5, 816.25, 34.0, 76.5]], [2, "car", [32.0, 641.0, 64.0, 35.0]], [3, "car", [286.0, 778.25, 41.0, 70.5]], [4, "car", [1091.25, 820.5, 33.5, 76.0]], [5, "car", [896.5, 63.0, 34.0, 84.0]], [6, "car", [857.75, 84.0, 31.5, 59.0]], [7, "car", [764.0, 570.25, 70.0, 39.5]], [8, "car", [323.25, 765.75, 39.5, 78.5]], [9, "car", [1100.75, 979.5, 33.5, 78.0]], [10, "car", [1052.5, 1044.0, 36.0, 72.0]], [11, "etc vehicle", [1109.0, 39.75, 45.0, 79.5]], [12, "truck", [213.0, 769.0, 89.0, 68.0]], [13, "truck", [346.75, 612.5, 80.5, 33.0]], [14, "bus", [509.25, 632.25, 169.5, 53.5]], [15, "car", [438.5, 346.0, 63.0, 32.0]], [16, "bus", [931.25, 438.25, 149.5, 147.5]], [17, "person", [1117.25, 874.25, 8.5, 12.5]], [18, "person", [1174.25, 188.75, 8.5, 13.5]], [19, "person", [682.75, 710.5, 11.5, 10.0]], [20, "person", [635.0, 509.0, 11.0, 9.0]], [21, "person", [1866.75, 195.0, 22.5, 12.0]], [22, "car", [37.75, 345.5, 75.5, 35.0]]]}], "imagePath": "img01.jpg"}

标签: pythonjsondatabaseimage-processing

解决方案


Python 可以使用不同的模块来显示图像:matplotlibpillowcv2、GUI 框架(TkinterPyQtPyGTK等)或 Web 框架(FlaskDjango等)或其他模块 - 即。PyGame, Pyglet, 等等。所以首先你应该决定使用什么模块。不同的模块可以在不同的情况下使用。一切都取决于您已经使用的模块以及运行代码的位置。您没有添加这些有问题的详细信息,所以我选择了pillow.


这是具有Draw.rectangleDraw.text的枕头示例

对于测试,我使用了图像lenna.png(维基百科:Lenna

from PIL import Image, ImageDraw

data = {
    "objects": [
        {
            "label": "object",
            "position": [
                [0, "car", [396.5, 770.25, 43.0, 76.5]],
                [1, "car", [1045.5, 816.25, 34.0, 76.5]],
                [2, "car", [32.0, 641.0, 64.0, 35.0]],
                [3, "car", [286.0, 778.25, 41.0, 70.5]],
                [4, "car", [1091.25, 820.5, 33.5, 76.0]],
                [5, "car", [896.5, 63.0, 34.0, 84.0]],
                [6, "car", [857.75, 84.0, 31.5, 59.0]],
                [7, "car", [764.0, 570.25, 70.0, 39.5]],
                [8, "car", [323.25, 765.75, 39.5, 78.5]],
                [9, "car", [1100.75, 979.5, 33.5, 78.0]],
                [10, "car", [1052.5, 1044.0, 36.0, 72.0]],
                [11, "etc vehicle", [1109.0, 39.75, 45.0, 79.5]],
                [12, "truck", [213.0, 769.0, 89.0, 68.0]],
                [13, "truck", [346.75, 612.5, 80.5, 33.0]],
                [14, "bus", [509.25, 632.25, 169.5, 53.5]],
                [15, "car", [438.5, 346.0, 63.0, 32.0]],
                [16, "bus", [931.25, 438.25, 149.5, 147.5]],
                [17, "person", [1117.25, 874.25, 8.5, 12.5]],
                [18, "person", [1174.25, 188.75, 8.5, 13.5]],
                [19, "person", [682.75, 710.5, 11.5, 10.0]],
                [20, "person", [635.0, 509.0, 11.0, 9.0]],
                [21, "person", [1866.75, 195.0, 22.5, 12.0]],            
                [22, "car", [37.75, 345.5, 75.5, 35.0]]
            ]
        }
    ],
    "imagePath": "img01.jpg"
}

data["imagePath"] = 'lenna.png'       # my image - only for tests

img = Image.open(data["imagePath"])
img = img.resize((1900, 1200))        # resize - only for tests

draw = ImageDraw.Draw(img)

for item in data["objects"][0]["position"]:
    text = item[1]
    x, y, w, h  = item[2]
    draw.rectangle([x, y, x+w, y+h], outline='green', width=2)
    draw.text([x, y-10], text)
    
img.show()
img.save('result.jpg')

lenna.png(512x512)

在此处输入图像描述

result.jpg(1900x1200)

在此处输入图像描述


推荐阅读