首页 > 解决方案 > 如何根据 txt 文件上的描述对图像进行分类?

问题描述

我有一个包含大约 10,000 张图像的文件夹,我有一个巨大的*.txt文件,如下所示。txt 文件有 30,000 行。每个图像有三行,第 (1) 行包含图像名称,例如“04406_8_074402.jpeg”。第 (2) 行包含图像类别,在这种情况下,它是一只猫,并且 --- 将该信息与下一行分开。它包含图像文件名/路径和图像类别:

例如:

Analysing Image: /path to image folder/images/04406_8_074402.jpeg
Object Class Presented: cat
----------------------------------
Analysing Image: /path to image folder/images/00009_8_071203.jpeg
Object Class Presented: dog
----------------------------------
Analysing Image: /path to image folder/images/04440_8_045244.jpeg
Object Class Presented: box
----------------------------------
Analysing Image: /path to image folder/images/00045_8_051505.jpeg
Object Class Presented: unclassified
.
.
.
.
.
----------------------------------
Analysing Image: /path to image folder/images/02290_8_073302.jpeg
Object Class Presented: panda 
----------------------------------

我需要根据它们的类名将这些图像分类到不同的文件夹中。我知道我可以使用以下方法读取 txt 文件:

with open('file.txt') as f:
    line = f.readline()
    while line:
        line = f.readline()
        print(line)

我的问题是如何根据它们的类名将这些图像放入不同的文件夹中?  

标签: pythonimageopencv-pythontxt

解决方案


我已经在这段代码中编译了所有讨论。请相应地更改路径。在我的代码..stack\main文件夹中包含所有图像,我使用下面的代码来读取..stack\file.txt将基于类的文件移动到..\stack\main\cat等等..\stack\main\dog

import os
import shutil
directory= r"C:\Users\VIDYA\Desktop\stack\main"

with open('file.txt') as f:
    line = f.readline()
    while line:
        line = f.readline()
        if line.startswith("Analysing Image:"):
            length = len(line)
            # Get image file name
            image=line[length -20 :-1]
            continue
        if line.startswith("Object Class Presented:"):
            word_list = line.split()  # list of words
            # get class name
            class_name=word_list[-1]
            new_folder=os.path.join(directory,class_name)
            os.makedirs(new_folder,exist_ok=True) #makes new class folder if it doesn't already exist
            source=os.path.join(directory,image)
            destination =os.path.join(directory,class_name)
            # Move the file from source to destination
            dest = shutil.move(source, destination)

推荐阅读