我有一个“鸟”文件夹,其中包含 11345 个鸟类图像,分别命名为 1.jpg、2.jpg、3.jpg......11344.jpg、11345.jpg。我需要将这些鸟类图像保存为“filenames.pickle”,以便在进一步的机器学习模块中使用它。数据应该这样排列:dataset/train/filenames.pickle, dataset/test/filenames.pickle
我需要创建一个单一的泡菜文件 filenames.pickle 来获取所有 11345 鸟图像。我非常困惑如何将这些图像添加到pickle中,以便我的代码获取pickle文件,但最终到达这些图像以训练机器学习模型。
from PIL import Image
import pickle
'''
I am just trying to convert one image into pickle to get an idea.
if is succefully convert into pickle then I will read all the
images inside the "bird" folder and convert all of them into one
single pickle file
'''
# converting an image into pickle
img = Image.open('059.jpg')
with open('059.pickle', 'wb') as f:
pickle.dump(img, f)
## read the pickle file
with open('059.pickle','rb') as f:
file = pickle.load(f)
print(file)
# after reading 059.pickle file :
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=500x375 at 0x2115BE59190>
# I dont want ( <PIL.JpegImagePlugin.JpegImageFile image mode=RGB
size=500x375 at 0x2115BE59190>) this result into pickle file.
# I want pickle file to save result like this: ['59.jpg'].
## to convert whole images inside bird folder
## input folder = bird\\images\\all_images_in_jpg_format
image = "bird\\images\\"
fout = open("bird\\filenames.pickle",'wb')
pickle.dump(image,fout)
fout.close()
with open("bird\\filenames.pickle",'rb') as f:
file = pickle.load(f)
print(file)
# output : bird\images\
## the above output is wrong
'''
becasue when I am done reading all the images and create one
pickle file as "filenames.pickle:, it should save images like
this:
['01.jpg','0342.jpg','06762.jpg', '06752.jpg', '05122.jpg',
'05144.jpg', '06635.jpg','06638.jpg',
'05632.jpg',......'11345.jpg']
and after reading this pickle file, somehow model will
automatcally read the images via pickle file.
'''
我对泡菜文件及其格式不太熟悉。任何人都可以帮助我或给我一些建议我应该如何解决这个问题并解决它?模型将如何通过 pickle 文件读取图像?pickle 文件包含什么(图像数据和像素信息或只是图像文件的名称)以便模型可以获取 pickle 文件并在训练时学习图像?