This script allows to generate two groups of images of the Corel DataSet (train y test).
The Corel DataSet can be downloaded from the following link: 1.000 images
The code to generate two folders (train y test) containing the images of given folder is: Shuffle. This programme was tested on Windows 7 with CodeBlocks 13.12 and relies on three files: main, Mezclador.cc and Mezclador.hpp. The GNU-LINUX version can be found here: Shuffle-GNU-Linux. The code of each file is described below:
Mezclador.hpp
#include <iostream> #include <sstream> #include <fstream> #include <vector> #include <string.h> #include <dirent.h> #include <time.h> #include <stdlib.h> #include <direct.h> using namespace std; class Mezclador{ private: string ruta; int porcentaje; public: Mezclador(string,int); bool mezclar(); bool checarExt(string,string); };
Mezclador.cc
#include "Mezclador.hpp" Mezclador::Mezclador(string ruta,int porcentaje){ this->ruta=ruta; this->porcentaje=porcentaje; } bool Mezclador::mezclar(){ bool r=true; DIR *pDIR; string nombre=""; struct dirent *dir_ent; ofstream listadoTrain; ofstream listadoTest; listadoTrain.open("train.txt",ios::out); listadoTest.open("test.txt",ios::out); vector<string> nombres[10]; int puntero=0; int id=-1; srand(time(NULL)); if((pDIR=opendir(ruta.c_str()))!=NULL){ while((dir_ent=readdir(pDIR))!=NULL){ nombre=dir_ent->d_name; if(checarExt(nombre,"jpg")){ stringstream ss; ss << (nombre.substr(0,nombre.find("."))); ss >> id; puntero=((id)/100); //cout << "[ " << id << "|" << ((id+1)/100) << "] "; nombres[puntero].push_back(nombre); } } if(mkdir("train")==0 && mkdir("test")==0){ cout << endl << "Directorios 'train' y 'test' creados con exito!!!" << endl; }else{ cout << endl << "No se pueden crear los directorios 'train' y 'test' porque ya existen, borrelos primero x favor..." << endl; return false; } for(int i=0;i<10;i++){ while(nombres[i].size()>0){ if(nombres[i].size()>porcentaje){ puntero=rand()%nombres[i].size(); listadoTest << nombres[i][puntero] << endl; ifstream origen((ruta+"/"+nombres[i][puntero]).c_str(),ios::in|ios::binary); ofstream destino(("test/"+nombres[i][puntero]).c_str(),ios::out|ios::binary); cout << "Copiando Fichero " << (ruta+"/"+nombres[i][puntero]).c_str() << endl; destino << origen.rdbuf(); destino.close(); origen.close(); nombres[i].erase(nombres[i].begin()+puntero); }else{ listadoTrain << nombres[i][0] << endl; ifstream origen((ruta+"/"+nombres[i][0]).c_str(),ios::in|ios::binary); ofstream destino(("train/"+nombres[i][0]).c_str(),ios::out|ios::binary); cout << "Copiando Fichero " << (ruta+"/"+nombres[i][0]).c_str() << endl; destino << origen.rdbuf(); destino.close(); origen.close(); nombres[i].erase(nombres[i].begin()+0); } } } closedir(pDIR); }else{ cout << "No se puede abrir el directorio..." << endl; } listadoTest.close(); listadoTrain.close(); return r; } bool Mezclador::checarExt(string nombre,string ext){ if(nombre.length()>=ext.length()){ return ((nombre.compare(nombre.length()-ext.length(),ext.length(),ext))==0); } return false; }
main:
#include "Mezclador.hpp" /** author vlarobbyk */ int main() { Mezclador *m=new Mezclador("C:\\Users\\vlarob\\Desktop\\Shuffle\\bin\\Debug\\imagenes",70); // Indicamos la ruta donde estan las 1.000 imagenes y el porcentaje que se tomara para train (el resto es para test) cout << "Instancia de la clase generada...." << endl; bool r=m->mezclar(); m->checarExt("abc.jpg","jpg"); cout << "Resultado de la operacion: " << r << endl; delete m; return 0; }