栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Python】猫狗图像训练集文件夹下猫狗图片文件分别移动至各自文件夹

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Python】猫狗图像训练集文件夹下猫狗图片文件分别移动至各自文件夹

这个标题有点拗口,下面描述一下这篇文章做了什么事情:

下载Kaggle的猫与狗图像分类数据集,解压后的train文件夹内存在cat.xx.jpg和dog.xx.jpg两类图片,先需要将其分别移动至train/cat和train/dog文件夹下,需要我们去编程自动化这个过程。

下载后解压的目录结构是这样的:

  • test
    • ️xxx.jpg
  • train
    • ️cat.xxx.jpg
    • ️dog.xxx.jpg
  • ️sample_submission.csv

test和train下都无文件夹,全是图片文件。

为了编码方便,我们需要把train下猫和狗的图片分别归类至自身的文件夹:

  • test
    • ️xxx.jpg
  • train
    • cat
      • ️cat.xxx.jpg
    • dog
      • ️dog.xxx.jpg
  • ️sample_submission.csv

Python编码如下:

import os
import shutil


# 没有文件夹则需要创建文件夹
cat_path: str = "./data/train/cat"
if not os.path.exists(cat_path):
    os.makedirs(cat_path)
dog_path: str = "./data/train/dog"
if not os.path.exists(dog_path):
    os.makedirs(dog_path)

# 遍历文件夹下文件并分别归类至对应的目录
image_path: str = "./data/train"
images: list = os.listdir(image_path)
for image in images:
    current_image_path: str = os.path.join(image_path, image)
    new_cat_path: str = os.path.join(cat_path, image)
    new_dog_path: str = os.path.join(dog_path, image)
    if "cat" in image and not os.path.isdir(current_image_path):
        shutil.move(current_image_path, new_cat_path)
    if "dog" in image and not os.path.isdir(current_image_path):
        shutil.move(current_image_path, new_dog_path)

文件移动的步骤,用的是shutil库:shutil.move(source_path, target_path)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/858530.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号