KSquare Utilities
FeatureFileIO.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdio.h>
3 #include <math.h>
4 #include <ctype.h>
5 #include <time.h>
6 #include <string>
7 #include <iostream>
8 #include <fstream>
9 #include <vector>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "MemoryDebug.h"
13 using namespace std;
14 
15 
16 #include "KKBaseTypes.h"
17 #include "DateTime.h"
18 #include "GoalKeeper.h"
19 #include "GlobalGoalKeeper.h"
20 #include "ImageIO.h"
21 #include "OSservices.h"
22 #include "RunLog.h"
23 #include "KKStr.h"
24 using namespace KKB;
25 
26 
27 #include "FeatureFileIO.h"
28 #include "FeatureFileIOArff.h"
29 #include "FeatureFileIOC45.h"
34 #include "FeatureFileIOUCI.h"
35 
36 #include "FactoryFVProducer.h"
38 #include "FileDesc.h"
39 #include "MLClass.h"
40 using namespace KKMLL;
41 
42 
43 
44 
45 void ReportError (RunLog& log,
46  const KKStr& fileName,
47  const KKStr& funcName,
48  kkint32 lineCount,
49  const KKStr& errorDesc
50  )
51 {
52  log.Level (-1) << endl
53  << funcName << " *** ERROR ***" << endl
54  << " File [" << fileName << "]" << endl
55  << " LineCount[" << lineCount << "]" << endl
56  << " Error [" << errorDesc << "]" << endl
57  << endl;
58 } /* ReportError */
59 
60 
61 
62 
63 
64 vector<FeatureFileIOPtr>* FeatureFileIO::registeredDrivers = NULL;
65 
66 
67 
68 std::vector<FeatureFileIOPtr>* FeatureFileIO::RegisteredDrivers ()
69 {
70  if (registeredDrivers == NULL)
71  {
72  RegisterAllDrivers ();
73  }
74 
75  return registeredDrivers;
76 }
77 
78 
80 {
82 
83  if (!registeredDrivers)
84  {
85  RegisterAllDrivers ();
86  registeredDrivers = new std::vector<FeatureFileIOPtr> ();
87  }
88 
89  registeredDrivers->push_back (_driver);
90 
92 } /* RegisterFeatureFileIODriver */
93 
94 
95 
96 
97 void FeatureFileIO::RegisterAllDrivers ()
98 {
100  if (registeredDrivers == NULL)
101  {
102  registeredDrivers = new std::vector<FeatureFileIOPtr> ();
103  registeredDrivers->push_back (new FeatureFileIOArff ());
104  registeredDrivers->push_back (new FeatureFileIOC45 ());
105  registeredDrivers->push_back (new FeatureFileIOColumn ());
106  registeredDrivers->push_back (new FeatureFileIODstWeb ());
107  //registeredDrivers->push_back (new FeatureFileIOKK ());
108  registeredDrivers->push_back (new FeatureFileIORoberts ());
109  registeredDrivers->push_back (new FeatureFileIOSparse ());
110  registeredDrivers->push_back (new FeatureFileIOUCI ());
111  atexit (FeatureFileIO::FinalCleanUp);
112  }
113 
115 } /* RegisterAllDrivers */
116 
117 
118 
119 
121 {
123 
124  FeatureFileIOPtr existingDriver = LookUpDriver (_driver->driverName);
125  if (existingDriver)
126  {
127  // We are trying to register two drivers with the same name; we can not do that.
128  cerr << endl << endl
129  << "FeatureFileIO::RegisterDriver ***ERROR***" << endl
130  << endl
131  << " trying to register more than one FeatureFileIO driver with " << endl
132  << " the same name[" << _driver->driverName << "]." << endl
133  << endl;
134  }
135  else
136  {
137  registeredDrivers->push_back (_driver);
138  }
139 
141 } /* RegisterDriver */
142 
143 
144 
145 
146 
147 
148 
149 /**
150  * @brief Before you terminate your application and after all FeatureFileIO activity is done
151  * call this method to destroy statically dynamically allocated data structures.
152  * @details This method undoes everything that 'RegisterAllDrivers' does. Its primary
153  * usefulness is aid in tracking down memory leaks. This way the '_CrtSetDbgFlag'
154  * debugging functionality will not report the memory allocated by the different
155  * feature FileIO drivers.
156  */
158 {
160 
161  if (registeredDrivers)
162  {
163  while (registeredDrivers->size () > 0)
164  {
165  FeatureFileIOPtr f = registeredDrivers->back ();
166  registeredDrivers->pop_back ();
167  delete f;
168  }
169 
170  delete registeredDrivers;
171  registeredDrivers = NULL;
172  }
173 
175 } /* CleanUpFeatureFileIO */
176 
177 
178 
179 
180 
181 FeatureFileIOPtr FeatureFileIO::LookUpDriver (const KKStr& _driverName)
182 {
183  vector<FeatureFileIOPtr>* drivers = RegisteredDrivers ();
184  KKStr driverNameLower = _driverName.ToLower ();
185  vector<FeatureFileIOPtr>::const_iterator idx;
186  for (idx = drivers->begin (); idx != drivers->end (); idx++)
187  {
188  if ((*idx)->driverNameLower == driverNameLower)
189  return *idx;
190  }
191  return NULL;
192 } /* LookUpDriver */
193 
194 
195 
196 
198 {
199  return LookUpDriver (_fileFormatStr);
200 }
201 
202 
203 
204 
206  bool _canRead,
207  bool _canWrite
208  )
209 {
210  FeatureFileIOPtr driver = LookUpDriver (_fileFormatStr);
211  if (!driver)
212  return NULL;
213 
214  if (_canRead && (!driver->CanRead ()))
215  return NULL;
216 
217  if (_canWrite && (!driver->CanWrite ()))
218  return NULL;
219 
220  return driver;
221 } /* FileFormatFromStr */
222 
223 
224 
225 
226 
228 {
229  vector<FeatureFileIOPtr>* drivers = RegisteredDrivers ();
230 
231  KKStr driversThatCanRead (128);
232 
233  vector<FeatureFileIOPtr>::const_iterator idx;
234  for (idx = drivers->begin (); idx != drivers->end (); idx++)
235  {
236  FeatureFileIOPtr driver = *idx;
237  if (driver->CanRead ())
238  {
239  if (!driversThatCanRead.Empty ())
240  driversThatCanRead << ", ";
241  driversThatCanRead << driver->DriverName ();
242  }
243  }
244  return driversThatCanRead;
245 } /* FileFormatsReadOptionsStr */
246 
247 
248 
250 {
251  vector<FeatureFileIOPtr>* drivers = RegisteredDrivers ();
252 
253  KKStr driversThatCanWrite (128);
254 
255  vector<FeatureFileIOPtr>::const_iterator idx;
256  for (idx = drivers->begin (); idx != drivers->end (); idx++)
257  {
258  FeatureFileIOPtr driver = *idx;
259  if (driver->CanWrite ())
260  {
261  if (!driversThatCanWrite.Empty ())
262  driversThatCanWrite << ", ";
263  driversThatCanWrite << driver->DriverName ();
264  }
265  }
266  return driversThatCanWrite;
267 } /* FileFormatsWrittenOptionsStr */
268 
269 
270 
271 
273 {
274  KKStr driversThatCanReadAndWrite (128);
275 
276  vector<FeatureFileIOPtr>* drivers = RegisteredDrivers ();
277 
278  vector<FeatureFileIOPtr>::const_iterator idx;
279  for (idx = drivers->begin (); idx != drivers->end (); idx++)
280  {
281  FeatureFileIOPtr driver = *idx;
282  if (driver->CanWrite () && driver->CanWrite ())
283  {
284  if (!driversThatCanReadAndWrite.Empty ())
285  driversThatCanReadAndWrite << ", ";
286  driversThatCanReadAndWrite << driver->DriverName ();
287  }
288  }
289  return driversThatCanReadAndWrite;
290 } /* FileFormatsReadAndWriteOptionsStr */
291 
292 
293 
294 
296  bool canWrite
297  )
298 {
299  vector<FeatureFileIOPtr>* drivers = RegisteredDrivers ();
300  VectorKKStr names;
301  vector<FeatureFileIOPtr>::iterator idx;
302 
303  for (idx = drivers->begin (); idx != drivers->end (); idx++)
304  {
305  FeatureFileIOPtr driver = *idx;
306  if (canRead && (!driver->CanRead ()))
307  continue;
308 
309  if (canWrite && (!driver->CanWrite ()))
310  continue;
311 
312  names.push_back (driver->DriverName ());
313  }
314 
315  return names;
316 } /* RegisteredDriverNames */
317 
318 
319 
320 
321 
322 FeatureFileIO::FeatureFileIO (const KKStr& _driverName,
323  bool _canRead,
324  bool _canWrite
325  ):
326  canRead (_canRead),
327  canWrite (_canWrite),
328  driverName (_driverName),
329  driverNameLower (_driverName.ToLower ())
330 {
331 }
332 
333 
334 
336 {
337 }
338 
339 
340 
341 void FeatureFileIO::GetLine (istream& _in,
342  KKStr& _line,
343  bool& _eof
344  )
345 {
346  _line = "";
347  if (_in.eof ())
348  {
349  _eof = true;
350  return;
351  }
352 
353  kkint32 ch = _in.peek ();
354  while ((ch != '\n') && (ch != '\r') && (!_in.eof ()))
355  {
356  ch = _in.get ();
357  _line.Append (ch);
358  ch = _in.peek ();
359  }
360 
361  if (!_in.eof ())
362  {
363  _in.get (); // Skip over end of line character.
364  if (ch == '\n')
365  {
366  ch = _in.peek ();
367  if (ch == '\r')
368  _in.get (); // line is terminated by LineFeed + CarrageReturn; we need to skip over both.
369  }
370  else if (ch == '\r')
371  {
372  ch = _in.peek ();
373  if (ch == '\n')
374  _in.get (); // line is terminated by CarrageReturn + LineFeed; we need to skip over both.
375  }
376  }
377 
378  _eof = false;
379 
380  return;
381 } /* GetLine */
382 
383 
384 
385 void FeatureFileIO::GetToken (istream& _in,
386  const char* _delimiters,
387  KKStr& _token,
388  bool& _eof,
389  bool& _eol
390  )
391 {
392  _token = "";
393  _eof = false;
394  _eol = false;
395 
396  if (_in.eof ())
397  {
398  _eof = true;
399  _eol = true;
400  return;
401  }
402 
403  kkint32 ch;
404 
405  // Skip past any leading white space.
406  ch = _in.peek ();
407  while ((ch == ' ') && (!_in.eof ()))
408  {
409  _in.get ();
410  ch = _in.peek ();
411  }
412 
413  if (_in.eof ())
414  {
415  _eof = true;
416  _eol = true;
417  return;
418  }
419 
420  if (ch == '\n')
421  {
422  _eol = true;
423  _in.get ();
424  if (_in.peek () == '\r')
425  _in.get ();
426  return;
427  }
428 
429  if (ch == '\r')
430  {
431  _eol = true;
432  _in.get ();
433  if (_in.peek () == '\n')
434  _in.get ();
435  return;
436  }
437 
438 
439  while ((!_in.eof ()) && (ch != '\n') && (ch != '\r') && (strchr (_delimiters, ch) == NULL))
440  {
441  _in.get ();
442  _token.Append ((char)ch);
443  ch = _in.peek ();
444  }
445 
446  if (strchr (_delimiters, ch) != NULL)
447  {
448  // the next character was a delimiter; in this case we want to remove from stream.
449  _in.get ();
450  }
451 
452 
453  return;
454 } /* GetToken */
455 
456 
457 
458 
459 
460 
461 
462 FeatureVectorListPtr FeatureFileIO::LoadFeatureFile
463  (const KKStr& _fileName,
464  MLClassList& _mlClasses,
465  kkint32 _maxCount,
466  VolConstBool& _cancelFlag, // will be monitored, if set to True Load will terminate.
467  bool& _successful,
468  bool& _changesMade,
469  RunLog& _log
470  )
471 {
472  _log.Level (10) << "LoadFeatureFile File[" << _fileName << "] FileFormat[" << driverName << "]" << endl;
473 
474  _changesMade = false;
475 
476  kkint32 estimatedNumOfDataItems = -1;
477 
478  _successful = true;
479 
480  ifstream in (_fileName.Str (), ios_base::in);
481  if (!in.is_open ())
482  {
483  _log.Level (-1) << "LoadFeatureFile ***ERROR*** Error Opening File[" << _fileName << "]." << endl;
484  _successful = false;
485  return NULL;
486  }
487 
488  KKStr errorMessage;
489 
490  FileDescPtr fileDescFromFile = GetFileDesc (_fileName, in, &_mlClasses, estimatedNumOfDataItems, errorMessage, _log);
491  if (fileDescFromFile == NULL)
492  {
493  _log.Level (-1) << endl << endl
494  << "FeatureFileIO::LoadFeatureFile ***ERROR*** Loading Feature File[" << _fileName << "]" << endl
495  << endl;
496  _successful = false;
497  return NULL;
498  }
499 
500  FileDescPtr fileDesc = FileDesc::GetExistingFileDesc (fileDescFromFile);
501 
502  in.clear ();
503  in.seekg (0, ios::beg);
504 
505  FeatureVectorListPtr examples = LoadFile (_fileName, fileDesc, _mlClasses, in, _maxCount, _cancelFlag, _changesMade, errorMessage, _log);
506  if (examples == NULL)
507  {
508  _successful = false;
509  }
510  else
511  {
512  _successful = true;
513  }
514 
515  in.close ();
516 
517  return examples;
518 } /* LoadFeatureFile */
519 
520 
521 
522 
523 void FeatureFileIO::AppendToFile (const KKStr& _fileName,
524  FeatureNumListConst& _selFeatures,
525  FeatureVectorList& _examples,
526  kkuint32& _numExamplesWritten,
527  VolConstBool& _cancelFlag,
528  bool& _successful,
529  RunLog& _log
530  )
531 {
532  _log.Level (10) << "FeatureFileIO::AppendToFile - File[" << _fileName << "]." << endl;
533 
534  _successful = true;
535 
536  FileDescPtr fileDesc = _examples.FileDesc ();
537 
538  ofstream out (_fileName.Str (), ios::app);
539 
540  if (!out.is_open())
541  {
542  KKStr err;
543  err << "AppendToFile Error Opening File[" << _fileName << "]";
544  _log.Level (-1) << endl
545  << "FeatureFileIO::AppendToFile ***ERROR***" << endl
546  << endl
547  << " " << err << endl
548  << endl;
549  osDisplayWarning (err);
550  _successful = false;
551  return;
552  }
553 
554  KKStr errorMessage;
555  SaveFile (_examples, _fileName, _selFeatures, out, _numExamplesWritten, _cancelFlag, _successful, errorMessage, _log);
556 
557  out.close ();
558 
559  return;
560 } /* AppendToFile */
561 
562 
563 
564 
565 void FeatureFileIO::SaveFeatureFile (const KKStr& _fileName,
566  FeatureNumListConst& _selFeatures,
567  FeatureVectorList& _examples,
568  kkuint32& _numExamplesWritten,
569  VolConstBool& _cancelFlag,
570  bool& _successful,
571  RunLog& _log
572  )
573 {
574  _log.Level (10) << "FeatureFileIO::SaveFeatureFile - File[" << _fileName << "]." << endl;
575 
576  ofstream out (_fileName.Str ());
577  if (!out.is_open())
578  {
579  _log.Level (-1) << "***ERROR***, SaveFeatureFile, Opening File[" << _fileName << "]" << endl;
580  _successful = false;
581  }
582 
583  out.precision (9);
584 
585  FileDescPtr fileDesc = _examples.FileDesc ();
586 
587  KKStr errorMessage;
588  SaveFile (_examples, _fileName, _selFeatures, out, _numExamplesWritten, _cancelFlag, _successful, errorMessage, _log);
589 
590  out.close ();
591 } /* SaveFeatureFile */
592 
593 
594 
595 
596 
598  FeatureNumListConst& _selFeatures,
599  FeatureVectorList& _examples,
600  VolConstBool& _cancelFlag,
601  bool& _successful,
602  RunLog& _log
603  )
604 {
605  kkuint32 numExamplesWritten = 0;
606  SaveFeatureFile (_fileName, _selFeatures, _examples, numExamplesWritten, _cancelFlag, _successful, _log);
607 
608  if (_cancelFlag || (!_successful))
609  return;
610 
611  if (_examples.QueueSize () > 64000)
612  {
613  kkint32 numPartsNeeded = (_examples.QueueSize () / 64000);
614  if ((_examples.QueueSize () % 64000) > 0)
615  numPartsNeeded++;
616 
617  kkint32 maxPartSize = (_examples.QueueSize () / numPartsNeeded) + 1;
618 
619  kkint32 partNum = 0;
620  FeatureVectorList::const_iterator idx = _examples.begin ();
621 
622  while ((idx != _examples.end ()) && (_successful) && (!_cancelFlag))
623  {
624  FeatureVectorListPtr part = _examples.ManufactureEmptyList (false);
625 
626  while ((idx != _examples.end ()) && (part->QueueSize () < maxPartSize))
627  {
628  part->PushOnBack (*idx);
629  idx++;
630  }
631 
632  KKStr partFileName = osRemoveExtension (_fileName) + "-" +
633  StrFormatInt (partNum, "00") + "." +
634  osGetFileExtension (_fileName);
635 
636  SaveFeatureFile (partFileName, _selFeatures, *part, numExamplesWritten, _cancelFlag, _successful, _log);
637 
638  partNum++;
639  delete part; part = NULL;
640  }
641  }
642 } /* SaveFeatureFileMultipleParts */
643 
644 
645 
646 
647 
648 FeatureVectorListPtr FeatureFileIO::LoadInSubDirectoryTree
649  (FactoryFVProducerPtr _fvProducerFactory,
650  KKStr _rootDir,
651  MLClassList& _mlClasses,
652  bool _useDirectoryNameForClassName,
653  VolConstBool& _cancelFlag,
654  bool _rewiteRootFeatureFile,
655  RunLog& _log
656  )
657 {
658  _log.Level (10) << "FeatureFileIO::LoadInSubDirectoryTree rootDir[" << _rootDir << "]." << endl;
659 
660  osAddLastSlash (_rootDir);
661 
662  KKStr featureFileName ("");
663  KKStr fullFeatureFileName ("");
664 
665  if (!_rootDir.Empty ())
666  {
667  featureFileName = osGetRootNameOfDirectory (_rootDir) + ".data";
668  fullFeatureFileName = _rootDir + featureFileName;
669  }
670  else
671  {
672  featureFileName = "Root.data";
673  fullFeatureFileName = "Root.data";
674  }
675 
676  MLClassPtr unKnownClass = _mlClasses.GetUnKnownClass ();
677  if (_useDirectoryNameForClassName)
678  {
679  KKStr className = MLClass::GetClassNameFromDirName (_rootDir);
680  unKnownClass = _mlClasses.GetMLClassPtr (className);
681  }
682 
683  bool changesMade = false;
684 
685  FeatureVectorListPtr dirImages = NULL;
686 
687  FileDescPtr fileDesc = _fvProducerFactory->FileDesc ();
688 
689  if (_rewiteRootFeatureFile)
690  {
691  DateTime timeStamp;
692  dirImages = FeatureDataReSink (_fvProducerFactory,
693  _rootDir,
694  featureFileName,
695  unKnownClass,
696  _useDirectoryNameForClassName,
697  _mlClasses,
698  _cancelFlag,
699  changesMade,
700  timeStamp,
701  _log
702  );
703  if (_useDirectoryNameForClassName)
704  {
705  FeatureVectorList::iterator idx;
706  for (idx = dirImages->begin (); idx != dirImages->end (); idx++)
707  {
708  if ((*idx)->MLClass () != unKnownClass)
709  {
710  (*idx)->MLClass (unKnownClass);
711  changesMade = true;
712  }
713  }
714 
715  if (changesMade)
716  {
717  KKStr fullFileName = osAddSlash (_rootDir) + featureFileName;
718  kkuint32 numExamplesWritten = 0;
719  bool cancel = false;
720  bool successful = false;
721  SaveFeatureFile (fullFileName,
722  dirImages->AllFeatures (),
723  *dirImages,
724  numExamplesWritten,
725  cancel,
726  successful,
727  _log
728  );
729  }
730  }
731  }
732  else
733  {
734  dirImages = _fvProducerFactory->ManufacturFeatureVectorList (true, _log);
735  }
736 
737  // Now that we have processed all image files in "rootDir",
738  // lets process any sub-directories.
739 
740  KKStr dirSearchPath = osAddSlash (_rootDir) + "*.*";
741 
742  KKStrListPtr subDirectories = osGetListOfDirectories (dirSearchPath);
743  if (subDirectories)
744  {
745  KKStrList::iterator idx;
746 
747  for (idx = subDirectories->begin (); (idx != subDirectories->end () && (!_cancelFlag)); idx++)
748  {
749  KKStr subDirName (**idx);
750  if (subDirName == "BorderImages")
751  {
752  // We ignore this director
753  continue;
754  }
755 
756  KKStr newDirPath = osAddSlash (_rootDir) + subDirName;
757 
758  FeatureVectorListPtr subDirImages = LoadInSubDirectoryTree (_fvProducerFactory,
759  newDirPath,
760  _mlClasses,
761  _useDirectoryNameForClassName,
762  _cancelFlag,
763  true, // true = ReWriteRootFeatureFile
764  _log
765  );
766  FeatureVectorPtr fv = NULL;
767 
768  osAddLastSlash (subDirName);
769 
770  // We want to add the directory path to the ExampleFileName so that we can later locate the source image.
771  FeatureVectorList::iterator idx = subDirImages->begin ();
772  for (idx = subDirImages->begin (); idx != subDirImages->end (); idx++)
773  {
774  fv = *idx;
775  KKStr newImageFileName = subDirName + fv->ExampleFileName ();
776  fv->ExampleFileName (newImageFileName);
777  }
778 
779  dirImages->AddQueue (*subDirImages);
780  subDirImages->Owner (false);
781  delete subDirImages;
782  }
783 
784  delete subDirectories; subDirectories = NULL;
785  }
786 
787  _log.Level (10) << "LoadInSubDirectoryTree - Done" << endl;
788 
789  return dirImages;
790 } /* LoadInSubDirectoryTree */
791 
792 
793 
794 
795 
796 
797 
798 
799 FeatureVectorListPtr FeatureFileIO::FeatureDataReSink (FactoryFVProducerPtr _fvProducerFactory,
800  const KKStr& _dirName,
801  const KKStr& _fileName,
802  MLClassPtr _unknownClass,
803  bool _useDirectoryNameForClassName,
804  MLClassList& _mlClasses,
805  VolConstBool& _cancelFlag,
806  bool& _changesMade,
807  KKB::DateTime& _timeStamp,
808  RunLog& _log
809  )
810 {
811  _changesMade = false;
812  _timeStamp = DateTime ();
813 
814  FileDescPtr fileDesc = _fvProducerFactory->FileDesc ();
815  if (_unknownClass == NULL)
816  _unknownClass = MLClass::GetUnKnownClassStatic ();
817 
818  KKStr className = _unknownClass->Name ();
819 
820  _log.Level (10) << "FeatureFileIO::FeatureDataReSink dirName: " << _dirName << endl
821  << " fileName: " << _fileName << " UnKnownClass: " << className << endl;
822 
823  KKStr fullFeatureFileName = osAddSlash (_dirName) + _fileName;
824 
825  bool successful = true;
826 
827  KKStr fileNameToOpen;
828  if (_dirName.Empty ())
829  fileNameToOpen = _fileName;
830  else
831  fileNameToOpen = osAddSlash (_dirName) + _fileName;
832 
833  bool versionsAreSame = false;
834 
835  FeatureVectorListPtr origFeatureVectorData
836  = LoadFeatureFile (fileNameToOpen, _mlClasses, -1, _cancelFlag, successful, _changesMade, _log);
837 
838  if (origFeatureVectorData == NULL)
839  {
840  successful = false;
841  origFeatureVectorData = _fvProducerFactory->ManufacturFeatureVectorList (true, _log);
842  }
843 
844  if (_cancelFlag)
845  {
846  delete origFeatureVectorData; origFeatureVectorData = NULL;
847  return _fvProducerFactory->ManufacturFeatureVectorList (true, _log);
848  }
849 
850  FeatureVectorListPtr origFeatureData = NULL;
851 
852  if (successful &&
853  (&typeid (*origFeatureVectorData) == _fvProducerFactory->FeatureVectorListTypeId ()) &&
854  ((*(origFeatureVectorData->FileDesc ())) == (*(_fvProducerFactory->FileDesc ())))
855  )
856  {
857  origFeatureData = origFeatureVectorData;
858  }
859  else
860  {
861  origFeatureData = _fvProducerFactory->ManufacturFeatureVectorList (true, _log);
862  delete origFeatureVectorData;
863  origFeatureVectorData = NULL;
864  }
865 
866  KKStr fileSpec = osAddSlash (_dirName) + "*.*";
867  KKStrListPtr fileNameList = osGetListOfFiles (fileSpec);
868 
869  if (!fileNameList)
870  {
871  // There are no Image Files, so we need to return a Empty List of Image Features.
872 
873  if (origFeatureData->QueueSize () > 0)
874  _changesMade = true;
875 
876  delete origFeatureData; origFeatureData = NULL;
877 
878  return _fvProducerFactory->ManufacturFeatureVectorList (true, _log);
879  }
880 
881  FeatureVectorProducerPtr fvProducer = _fvProducerFactory->ManufactureInstance (_log);
882 
883  if (successful)
884  {
885  if (origFeatureData->Version () == fvProducer->Version ())
886  {
887  versionsAreSame = true;
888  _timeStamp = osGetFileDateTime (fileNameToOpen);
889  }
890 
891  else
892  {
893  _changesMade = true;
894  }
895  }
896  else
897  {
898  delete origFeatureData;
899  origFeatureData = _fvProducerFactory->ManufacturFeatureVectorList (true, _log);
900  }
901 
902  origFeatureData->SortByRootName (false);
903 
904  FeatureVectorListPtr extractedFeatures = _fvProducerFactory->ManufacturFeatureVectorList (true, _log);
905  extractedFeatures->Version (fvProducer->Version ());
906 
907  fileNameList->Sort (false);
908 
909  KKStrList::iterator fnIDX;
910  fnIDX = fileNameList->begin (); // fileNameList
911 
912  KKStrPtr imageFileName;
913 
914  kkint32 numImagesFoundInOrigFeatureData = 0;
915  kkint32 numOfNewFeatureExtractions = 0;
916 
917  for (fnIDX = fileNameList->begin (); (fnIDX != fileNameList->end ()) && (!_cancelFlag); ++fnIDX)
918  {
919  imageFileName = *fnIDX;
920 
921  // pv414-_002_20140414-162243_02068814-1261.bmp
922  KKStr rootName = osGetRootName (*imageFileName);
923  if (rootName == "pv414-_002_20140414-162243_02068814-1261")
924  cout << "Stop Here." << endl;
925 
926  bool validImageFileFormat = SupportedImageFileFormat (*imageFileName);
927 
928  if (!validImageFileFormat)
929  continue;
930 
931  bool featureVectorCoputaionSuccessful = false;
932 
933  FeatureVectorPtr origFV = origFeatureData->BinarySearchByName (*imageFileName);
934  if (origFV)
935  numImagesFoundInOrigFeatureData++;
936 
937  if (origFV && versionsAreSame)
938  {
939  featureVectorCoputaionSuccessful = true;
940  if (_useDirectoryNameForClassName)
941  {
942  if (origFV->MLClass () != _unknownClass)
943  {
944  _changesMade = true;
945  origFV->MLClass (_unknownClass);
946  }
947  }
948 
949  else if ((origFV->MLClass ()->UnDefined ()) && (origFV->MLClass () != _unknownClass))
950  {
951  _changesMade = true;
952  origFV->MLClass (_unknownClass);
953  }
954 
955  extractedFeatures->PushOnBack (origFV);
956  origFeatureData->DeleteEntry (origFV);
957  }
958  else
959  {
960  // We either DON'T have an original image or versions are not the same.
961 
962  KKStr fullFileName = osAddSlash (_dirName) + (*imageFileName);
963  FeatureVectorPtr fv = NULL;
964  try
965  {
966  RasterPtr image = ReadImage (fullFileName);
967  if (image)
968  fv = fvProducer->ComputeFeatureVector (*image, _unknownClass, NULL, 1.0f, _log);
969  delete image;
970  image = NULL;
971  if (fv)
972  featureVectorCoputaionSuccessful = true;
973  else
974  featureVectorCoputaionSuccessful = false;
975  }
976  catch (...)
977  {
978  _log.Level (-1) << endl << endl
979  << "FeatureDataReSink ***ERROR***" << endl
980  << " Exception occurred calling constructor 'ComputeFeatureVector'." << endl
981  << endl;
982  featureVectorCoputaionSuccessful = false;
983  fv = NULL;
984  }
985 
986  if (!featureVectorCoputaionSuccessful)
987  {
988  _log.Level (-1) << " FeatureFileIOKK::FeatureDataReSink *** ERROR ***, Processing Image File["
989  << imageFileName << "]."
990  << endl;
991  delete fv;
992  fv = NULL;
993  }
994 
995  else
996  {
997  _changesMade = true;
998  fv->ExampleFileName (*imageFileName);
999  _log.Level (30) << fv->ExampleFileName () << " " << fv->OrigSize () << endl;
1000  extractedFeatures->PushOnBack (fv);
1001  numOfNewFeatureExtractions++;
1002 
1003  if ((numOfNewFeatureExtractions % 100) == 0)
1004  cout << numOfNewFeatureExtractions << " Images Extracted." << endl;
1005  }
1006  }
1007  }
1008 
1009  if (numImagesFoundInOrigFeatureData != extractedFeatures->QueueSize ())
1010  _changesMade = true;
1011 
1012  extractedFeatures->Version (fvProducer->Version ());
1013 
1014  if ((_changesMade) && (!_cancelFlag))
1015  {
1016  //extractedFeatures->WriteImageFeaturesToFile (fullFeatureFileName, RawFormat, FeatureNumList::AllFeatures (extractedFeatures->FileDesc ()));
1017 
1018  kkuint32 numExamplesWritten = 0;
1019 
1020  SaveFeatureFile (fullFeatureFileName,
1022  *extractedFeatures,
1023  numExamplesWritten,
1024  _cancelFlag,
1025  successful,
1026  _log
1027  );
1028 
1029  _timeStamp = osGetLocalDateTime ();
1030  }
1031 
1032  delete fvProducer; fvProducer = NULL;
1033  delete fileNameList; fileNameList = NULL;
1034  delete origFeatureData; origFeatureData = NULL;
1035 
1036  _log.Level (10) << "FeatureDataReSink Exiting Dir: " << _dirName << endl;
1037 
1038  return extractedFeatures;
1039 } /* FeatureDataReSink */
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
void ExampleFileName(const KKStr &_exampleFileName)
Name of source of feature vector, ex: file name of image that the feature vector was computed from...
Definition: FeatureVector.h:75
void PushOnBack(FeatureVectorPtr image)
Overloading the PushOnBack function in KKQueue so we can monitor the Version and Sort Order...
MLClass * MLClassPtr
Definition: MLClass.h:46
void GetToken(std::istream &_in, const char *_delimiters, KKStr &_token, bool &_eof, bool &_eol)
Will retrieve the next token from the input stream.
void AddQueue(const FeatureVectorList &examplesToAdd)
Add the contents of &#39;examplesToAdd&#39; to the end of this list.
Provides a detailed description of the attributes of a dataset.
Definition: FileDesc.h:72
static FileDescPtr GetExistingFileDesc(FileDescPtr fileDesc)
Returns a pointer to an existing instance of &#39;fileDesc&#39; if it exists, otherwise will use one being pa...
Definition: FileDesc.cpp:555
MLClassPtr GetUnKnownClass()
Return a pointer to the MLClass object that represents the unknown Class in the list.
Definition: MLClass.cpp:893
__int32 kkint32
Definition: KKBaseTypes.h:88
KKB::DateTime osGetLocalDateTime()
Returned the current local date and time.
FeatureVectorPtr BinarySearchByName(const KKStr &_imageFileName) const
Will search for the example with the same name as &#39;_imageFileName&#39;.
virtual kkint16 Version() const =0
Keeps track of selected features.
KKStr & operator=(const char *src)
Definition: KKStr.cpp:1442
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
FeatureNumList AllFeatures()
Will return a FeatureNumList instance with all features selected.
const FileDescPtr FileDesc() const
FeatureNumList const FeatureNumListConst
bool operator==(const char *rtStr) const
Definition: KKStr.cpp:1588
static KKStr GetClassNameFromDirName(const KKStr &subDir)
Definition: MLClass.cpp:372
bool UnDefined() const
Definition: MLClass.h:183
static MLClassPtr GetUnKnownClassStatic()
Definition: MLClass.cpp:261
KKStrListPtr osGetListOfFiles(const KKStr &fileSpec)
Returns a list of files that match the provided file specification.
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
void GetLine(std::istream &_in, KKStr &_line, bool &_eof)
bool SupportedImageFileFormat(const KKStr &imageFileName)
Definition: ImageIO.cpp:948
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
RasterPtr ReadImage(const KKStr &imageFileName)
Definition: ImageIO.cpp:188
bool operator==(const FileDesc &rightSize) const
Returns true if file description on the right size is identical.
Definition: FileDesc.cpp:443
FeatureVectorListPtr LoadInSubDirectoryTree(FactoryFVProducerPtr _fvProducerFactory, KKStr _rootDir, MLClassList &_mlClasses, bool _useDirectoryNameForClassName, VolConstBool &_cancelFlag, bool _rewiteRootFeatureFile, RunLog &_log)
Creates a feature vector list of all images located in the specified sub-directory tree...
virtual FeatureVectorListPtr LoadFeatureFile(const KKStr &_fileName, MLClassList &_mlClasses, kkint32 _maxCount, VolConstBool &_cancelFlag, bool &_successful, bool &_changesMade, RunLog &_log)
Loads the contents of a feature data file and returns a ImageFeaturesList container object...
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
void SaveFeatureFileMultipleParts(const KKStr &_fileName, FeatureNumListConst &_selFeatures, FeatureVectorList &_examples, VolConstBool &_cancelFlag, bool &_successful, RunLog &_log)
Saves the feature file in multiple parts with no one single part larger that 64k examples.
static void FinalCleanUp()
Before you terminate your application and after all FeatureFileIO activity is done call this method t...
Container class for FeatureVector derived objects.
KKB::KKStr osAddSlash(const KKStr &fileSpec)
virtual const type_info * FeatureVectorListTypeId() const =0
Returns the &#39;type_info&#39; of the FeatureVectorList that this instance of &#39;FactoryFVProducer&#39; will creat...
KKTHread * KKTHreadPtr
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
FeatureFileIO * FeatureFileIOPtr
Definition: FileDesc.h:45
virtual FeatureVectorListPtr ManufacturFeatureVectorList(bool owner, RunLog &runLog) const
Manufactures a instance of a derived &#39;FeatureVectorList&#39; class that is appropriate for containing ins...
Base class for all FeatureFileIO classes.
Definition: FeatureFileIO.h:48
bool Empty() const
Definition: KKStr.h:241
void SortByRootName(bool reversedOrder=false)
void ReportError(RunLog &log, const KKStr &fileName, const KKStr &funcName, kkint32 lineCount, const KKStr &errorDesc)
virtual FeatureVectorProducerPtr ManufactureInstance(RunLog &runLog)=0
Derived classes will instantiate appropriate instances of &#39;FeatureVectorProducer&#39;.
kkint16 Version() const
virtual void SaveFeatureFile(const KKStr &_fileName, FeatureNumListConst &_selFeatures, FeatureVectorList &_examples, kkuint32 &_numExamplesWritten, VolConstBool &_cancelFlag, bool &_successful, RunLog &_log)
Save examples to &#39;fileName&#39;.
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
virtual FeatureVectorPtr ComputeFeatureVector(const Raster &image, const MLClassPtr knownClass, RasterListPtr intermediateImages, float priorReductionFactor, RunLog &runLog)=0
Compute a FeatureVector for the supplied &#39;image&#39;.
static VectorKKStr RegisteredDriverNames(bool canRead, bool canWrite)
MLClassPtr MLClass() const
Class that is example is assigned to.
void MLClass(MLClassPtr _mlClass)
Assign a class to this example.
Definition: FeatureVector.h:74
FileDesc * FileDescPtr
static FeatureNumList AllFeatures(FileDescPtr fileDesc)
Create a FeatureNumList object where all features are selected, except ones that are flagged as Ignor...
KKStr osGetRootNameOfDirectory(KKStr fullDirName)
void Sort(bool _reversedOrder)
Definition: KKStr.cpp:4770
static void RegisterFeatureFileIODriver(FeatureFileIOPtr _driver)
For each feature file format register the appropriate driver through this static method.
const KKStr & Name() const
Definition: MLClass.h:154
static FeatureFileIOPtr FileFormatFromStr(const KKStr &_fileFormatStr)
KKStr(const char *str)
Definition: KKStr.cpp:537
KKStr operator+(const KKStr &right) const
Definition: KKStr.cpp:3998
static KKStr FileFormatsWrittenOptionsStr()
KKStrListPtr osGetListOfDirectories(KKStr fileSpec)
KKStr & operator=(const KKStr &src)
Definition: KKStr.cpp:1390
KKB::DateTime osGetFileDateTime(const KKStr &fileName)
Used for logging messages.
Definition: RunLog.h:49
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
static KKStr FileFormatsReadOptionsStr()
virtual MLClassPtr GetMLClassPtr(const KKStr &_name)
return pointer to instance with &#39;_name&#39;; if none exists, create one and add to list.
Definition: MLClass.cpp:861
FeatureFileIO * FeatureFileIOPtr
Definition: FeatureFileIO.h:51
FeatureVectorProducer * FeatureVectorProducerPtr
Maintains a list of MLClass instances.
Definition: MLClass.h:233
static KKStr FileFormatsReadAndWriteOptionsStr()
virtual FeatureVectorListPtr FeatureDataReSink(FactoryFVProducerPtr _fvProducerFactory, const KKStr &_dirName, const KKStr &_fileName, MLClassPtr _unknownClass, bool _useDirectoryNameForClassName, MLClassList &_mlClasses, VolConstBool &_cancelFlag, bool &_changesMade, KKB::DateTime &_timeStamp, RunLog &_log)
Synchronizes the contents of a feature data file with a directory of images.
Maintains one instance of a GoalKeeper object that can be used anywhere in the application.
virtual FileDescPtr FileDesc() const =0
static void RegisterDriver(FeatureFileIOPtr driver)
void Version(kkint16 _version)
void AppendToFile(const KKStr &_fileName, FeatureNumListConst &_selFeatures, FeatureVectorList &_examples, kkuint32 &_numExamplesWritten, VolConstBool &_cancelFlag, bool &_successful, RunLog &log)
const KKStr & DriverName()
FeatureFileIO(const KKStr &_driverName, bool _canRead, bool _canWrite)
const KKStr & ExampleFileName() const
Name of file that this FeatureVector was computed from.
KKStr osGetRootName(const KKStr &fullFileName)
DateTime & operator=(const DateTime &right)
Definition: DateTime.cpp:1231
static FeatureFileIOPtr FileFormatFromStr(const KKStr &_fileFormatStr, bool _canRead, bool _canWrite)
KKStr ToLower() const
Definition: KKStr.cpp:2529
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163
void osAddLastSlash(KKStr &fileSpec)
Will add the appropriate Directory separator character to the end of fileSpec if one is not there alr...