在这里我写了一个代码来加载图像,然后我该怎么做将那些图像分成三个文件夹;分别使用70%,15%和15%的比率进行培训,测试和验证。
from os import listdir
from PIL import Image as PImage
import split_folders
import os, os.path
import numpy as np
#imgs.append(Image.open(os.path.join(path,image))
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)
loadedImages.append(img)
return loadedImages
path = "./Inputs/"
imgs = loadImages(path)
for img in imgs:
print(img)
train, validate, test = np.split(imgs.sample(frac=1), [int(.7*len(imgs)), int(.85*len(imgs))])
这是错误的,因为它不支持列表对象
有什么解决办法吗?
答案 0 :(得分:1)
据我了解,函数loadImages
返回python list
对象。由于没有list.sample()
方法,因此字符串
train, validate, test = np.split(imgs.sample(frac=1), [int(.7*len(imgs)), int(.85*len(imgs))])
无效。因此,有一些方法可以混洗所有数据。例如,您可以使用numpy.random.shuffle
来对列表值进行重新排序:
np.random.shuffle(imgs) # now this list is shuffled
train, validate, test = np.split(imgs, [int(.7*len(imgs)), int(.85*len(imgs))])
请注意,np.random.shuffle会使对象的类型保持完整(如果是列表)。我猜代码的其他部分是正确的。
lst = [1,2,3]
type(lst)
Out:
<class'list'>
np.random.shuffle(lst)
type(lst)
Out:
<class'list'>
此外,sample
方法是数据重采样的pandas
包默认方法之一,但我想这里不需要pandas
答案 1 :(得分:0)
要将文件复制到文件夹,我们可以使用class SamplePagerAdapter extends PagerAdapter {
private String stringDefinitionGemstonesPhoto = ("https://geology.com/minerals/photos/calcite-marble-127.jpg, https://geology.com/minerals/photos/calcite-concrete.jpg");
private String[] strAry = stringDefinitionGemstonesPhoto.split(",");
@Override
public int getCount() {
return strAry.length;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
PhotoView photoView = new PhotoView(container.getContext());
try {
new DownloadImageTask(photoView)
.execute(strAry[position]);
} catch (Exception ex) {
Log.e("Error", ex.getMessage());
}
container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return photoView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private ImageView bmImage;
DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urlDisplay = urls[0];
Bitmap myImage = null;
try {
InputStream in = new java.net.URL(urlDisplay).openStream();
myImage = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return myImage;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
的{{1}}方法:
下面的代码是这个问题的答案; (获得列表图像文件名而不是图像列表;
shutil.copyfile(os.path.join(resized_dir,x),os.path.join(train_dir,x))