如何根据txt文件上的描述对图像进行分类?



我有一个大约10000张图片的文件夹,我有一个巨大的*.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)

我的问题是我如何把这些图像到不同的文件夹基于他们的类名?

我在这段代码中编译了这里的所有讨论。请更改相应的路径。在我的代码中,..stackmain文件夹包含所有图像,我使用下面的代码读取..stackfile.txt,将基于类的文件移动到..stackmaincat..stackmaindog等。

import os
import shutil
directory= r"C:UsersVIDYADesktopstackmain"
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)

下面这几行有点像python的伪代码:

while not done
read line from file
if line starts with "Analysing Image:"
save image name
else if line starts with "Object Class"
save object class
create directory named per object class suppressing error messages if it already exists
move this image to that directory

最新更新