我有一個“鳥”檔案夾,其中包含 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 檔案并在訓練時學習影像?
uj5u.com熱心網友回復:
修改我的原始答案。現在我在一個檔案中腌制檔案名,并將影像腌制到另一個檔案中。
from PIL import Image
import os
import pickle
from glob import glob
## to convert whole images inside bird folder
## input folder = bird\\images\\all_images_in_jpg_format
PICKLE_FILE = "bird\\filenames.pickle"
SOURCE_DIRECTORY = "bird\\images\\"
PICKLE_IMAGES = "bird\\images.pickle"
path_list = glob(os.path.join(SOURCE_DIRECTORY, "*.jpg"))
# pickle images into big pickle file
with open(PICKLE_IMAGES,"wb") as f:
for file_name in path_list:
pickle.dump(Image.open(file_name),f)
# get short names from the path list
file_list = list(
map(
lambda x: os.path.basename(x), path_list)
)
# pickle short name list
pickle.dump(file_list, open(PICKLE_FILE, 'wb'))
# test that we can reread the list
recovered_list = pickle.load(open(PICKLE_FILE,"rb"))
if file_list == recovered_list:
print("Lists Match!")
else:
print("Lists Don't Match!!!")
# read a couple images out of the image file:
display_count = 5
with open(PICKLE_IMAGES,"rb") as f:
while True:
try:
pickle.load(f).show()
display_count -= 1
if display_count <= 0:
break
except EOFerror as e:
break
可能仍然是您的培訓師想要單獨的腌制影像,或者它不喜歡 PIL 使用的影像格式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/415765.html
標籤:
