使用Python根据JSON将图像分类为文件夹

发布于 2025-02-09 07:51:40 字数 1273 浏览 0 评论 0原文

我有一个图像数据集,我需要将其转换为TensorFlow格式进行图像分类。数据集JSON元数据格式如下:

{
  "0": {
    "image_filepath": "images/0.jpg",
    "anomaly_class": "A"
  },
  "1": {
    "image_filepath": "images/1.jpg",
    "anomaly_class": "B"
  },
  "2": {
    "image_filepath": "images/2.jpg",
    "anomaly_class": "C"
  },
  "3": {
    "image_filepath": "images/3.jpg",
    "anomaly_class": "D"
  },
  "4": {
    "image_filepath": "images/4.jpg",
    "anomaly_class": "E"
  },
  "5": {
    "image_filepath": "images/5.jpg",
    "anomaly_class": "F"
  },
  "6": {
    "image_filepath": "images/6.jpg",
    "anomaly_class": "G"
  },
  "7": {
    "image_filepath": "images/7.jpg",
    "anomaly_class": "H"
  },
  "8": {
    "image_filepath": "images/8.jpg",
    "anomaly_class": "I"
  },
  "9": {
    "image_filepath": "images/9.jpg",
    "anomaly_class": "J"
  },
  "10": {
    "image_filepath": "images/10.jpg",
    "anomaly_class": "K"
  },
  "11": {
    "image_filepath": "images/11.jpg",
    "anomaly_class": "L"
  },
  "12": {
    "image_filepath": "images/12.jpg",
    "anomaly_class": "M"
  },
.
.
.

"1000": {
    "image_filepath": "images/1000.jpg",
    "anomaly_class": "A"
  }
}

我想编写一个Python脚本,该脚本使用键“ Anomaly_class”的值创建文件夹名称,然后将图像分类/将图像移动到其各自的文件夹中。

有帮助吗?

I have an image dataset which I need to convert to Tensorflow format for image classification. The dataset JSON metadata format is shown below:

{
  "0": {
    "image_filepath": "images/0.jpg",
    "anomaly_class": "A"
  },
  "1": {
    "image_filepath": "images/1.jpg",
    "anomaly_class": "B"
  },
  "2": {
    "image_filepath": "images/2.jpg",
    "anomaly_class": "C"
  },
  "3": {
    "image_filepath": "images/3.jpg",
    "anomaly_class": "D"
  },
  "4": {
    "image_filepath": "images/4.jpg",
    "anomaly_class": "E"
  },
  "5": {
    "image_filepath": "images/5.jpg",
    "anomaly_class": "F"
  },
  "6": {
    "image_filepath": "images/6.jpg",
    "anomaly_class": "G"
  },
  "7": {
    "image_filepath": "images/7.jpg",
    "anomaly_class": "H"
  },
  "8": {
    "image_filepath": "images/8.jpg",
    "anomaly_class": "I"
  },
  "9": {
    "image_filepath": "images/9.jpg",
    "anomaly_class": "J"
  },
  "10": {
    "image_filepath": "images/10.jpg",
    "anomaly_class": "K"
  },
  "11": {
    "image_filepath": "images/11.jpg",
    "anomaly_class": "L"
  },
  "12": {
    "image_filepath": "images/12.jpg",
    "anomaly_class": "M"
  },
.
.
.

"1000": {
    "image_filepath": "images/1000.jpg",
    "anomaly_class": "A"
  }
}

I want to write a Python script that creates folder names using value of the key "anomaly_class" and sorts/moves the images into their respective folders.

Any help please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悍妇囚夫 2025-02-16 07:51:40

这应该有效:

import os

json_object = {
  "0": {
    "image_filepath": "images/0.jpg",
    "anomaly_class": "A"
  },
  "1": {
    "image_filepath": "images/1.jpg",
    "anomaly_class": "B"
  },
  "2": {
    "image_filepath": "images/2.jpg",
    "anomaly_class": "C"
  }
}

# Parent Directory path
parent_dir = "D:/PyProject/"

for key in json_object:
    anomaly_class_folder_name = json_object[key]['anomaly_class']
    image_fp = json_object[key]['image_filepath']

    # Strip path from image file
    file_name = os.path.basename(image_fp)

    # Create Folder
    path = os.path.join(parent_dir, anomaly_class_folder_name)
    os.mkdir(path)

    # Move file to target directory
    os.replace(image_fp, path + "/" + file_name)

This should work:

import os

json_object = {
  "0": {
    "image_filepath": "images/0.jpg",
    "anomaly_class": "A"
  },
  "1": {
    "image_filepath": "images/1.jpg",
    "anomaly_class": "B"
  },
  "2": {
    "image_filepath": "images/2.jpg",
    "anomaly_class": "C"
  }
}

# Parent Directory path
parent_dir = "D:/PyProject/"

for key in json_object:
    anomaly_class_folder_name = json_object[key]['anomaly_class']
    image_fp = json_object[key]['image_filepath']

    # Strip path from image file
    file_name = os.path.basename(image_fp)

    # Create Folder
    path = os.path.join(parent_dir, anomaly_class_folder_name)
    os.mkdir(path)

    # Move file to target directory
    os.replace(image_fp, path + "/" + file_name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文