Я пишу код для загрузки изображений и изменения их размера, после чего я изменил этот код, разделив эти изображения с измененными размерами на три категории;поезд, тестирование и валидация (согласно соотношениям).Затем я получил эту ошибку «TypeError: int() argument must be a string, a bytes-like object or a number, not 'Image
» после добавления этой строки (15): img.load()
для устранения ошибки: ValueError: seek of closed file
.
это код.
from os import listdir
from PIL import Image as PImage
import split_folders
import os, os.path
import numpy as np
import shutil
from scipy.misc import imresize
def loadImages(path):
imagesList = listdir(path)
loadedImages = []
for image in imagesList:
with open(os.path.join(path, image), 'rb') as i:
img = PImage.open(i)
img.load()
loadedImages.append(img)
return loadedImages
path = "./Inputs/"
imgs = loadImages(path)
#resizing
imgs = [img.resize((160,80), PImage.ANTIALIAS) for img in imgs]
print(imgs)
# split folders
np.random.shuffle(imgs) # now this list is shuffled
train, validate, test = np.split(imgs, [int(.7*len(imgs)), int(.85*len(imgs))])
print("loading images to Train folder")
print(train)
destination = "./Outputs/train/*.png"
# shutil.copy(np.array(train),destination)
# a = PImage.fromarray(train)
# a.save(destination,".png")
# train = PImage.fromarray(train).convert('RGB')
# train.save(destination,'PNG')
print("loading images to Test folder")
print(test)
print("loading images to Validation folder")
print(validate)
это трассировка:
Traceback (most recent call last):
File "/home/thisuri/.local/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line51, in _wrapfunc
return getattr(obj, method)(*args, **kwds)
AttributeError: 'list' object has no attribute 'swapaxes'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/thisuri/Documents/Re-Train_OCR/retrain_script.py", line 28, in <module>
train, validate, test = np.split(imgs, [int(.7*len(imgs)), int(.85*len(imgs))])
File "/home/thisuri/.local/lib/python3.6/site-packages/numpy/lib/shape_base.py", line 785, in split
res = array_split(ary, indices_or_sections, axis)
File "/home/thisuri/.local/lib/python3.6/site-packages/numpy/lib/shape_base.py", line 702, in array_split
sary = _nx.swapaxes(ary, axis, 0)
File "/home/thisuri/.local/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line549, in swapaxes
return _wrapfunc(a, 'swapaxes', axis1, axis2)
File "/home/thisuri/.local/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line61, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
File "/home/thisuri/.local/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line41, in _wrapit
result = getattr(asarray(obj), method)(*args, **kwds)
File "/home/thisuri/.local/lib/python3.6/site-packages/numpy/core/numeric.py", line 501, in asarray
return array(a, dtype, copy=False, order=order)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Image'
Любое решение?