统计多个json文件中某个标签的数量(Python和powershell实现)

需求:
统计用Labelme标注工具保存的多个JSON文件中的不同的Label标签数量,比如标注了十张图片我们需要统计这些图片中纸屑有多少个?

数据格式如下:

{
  "version": "5.3.0a0",
  "flags": {},
  "shapes": [
    {
      "label": "垃圾桶满意",
      "points": [
        [
          1672.439024390244,
          655.6097560975609
        ],
        [
          2113.9024390243903,
          584.8780487804878
        ],
        [
          2065.121951219512,
          894.6341463414633
        ],
        [
          1791.9512195121952,
          897.0731707317073
        ]
      ],
      "group_id": null,
      "description": "",
      "shape_type": "polygon",
      "flags": {}
    }
  ],
  "imagePath": "..\\巡查102201\\2023-10-22-10-46-55-20.jpg",
  "imageData": null,
  "imageHeight": 2160,
  "imageWidth": 3840
}

Python代码:

import os
import json
from collections import Counter

# 指定目录路径
directory_path = "data"  # 替换为你的目录路径

# 创建一个字典来存储label的计数
label_count = Counter()

# 列出目录中的JSON文件
for filename in os.listdir(directory_path):
    if filename.endswith(".json"):
        file_path = os.path.join(directory_path, filename)

        # 读取JSON文件使用UTF-8编码
        with open(file_path, "r", encoding="utf-8") as json_file:
            data = json.load(json_file)

            # 获取label属性的值
            labels = [shape["label"] for shape in data.get("shapes", [])]

            # 更新label的计数
            label_count.update(labels)

# 打印每个label的重复次数
for label, count in label_count.items():
    print(f"Label: {label}, Count: {count}")


运行结果:

powershell:

$directoryPath = "./"

# 创建一个哈希表来存储label的计数
$labelCount = @{}

# 列出目录中的JSON文件
Get-ChildItem -Path $directoryPath -Filter *.json | ForEach-Object {
    $jsonContent = Get-Content -Path $_.FullName -Raw -Encoding UTF8 | ConvertFrom-Json


    # 获取JSON中的label属性并更新计数
    foreach ($shape in $jsonContent.shapes) {
        $label = $shape.label
        if ($labelCount.ContainsKey($label)) {
            $labelCount[$label]++
        } else {
            $labelCount[$label] = 1
        }
    }
}

# 打印每个label的重复次数
$labelCount.GetEnumerator() | ForEach-Object {
    Write-Host "Label: $($_.Key), Count: $($_.Value)"
}

执行脚本:.\count_labels.ps1

Label: 垃圾桶满意, Count: 5
Label: 果皮菜叶, Count: 1

你可能感兴趣的:(python,json)