Parcourir la source

Modification of csl.

Rémi Synave il y a 1 an
Parent
commit
0e6efd7a77

+ 16 - 14
StrengthLine/StrengthLine.cpp

@@ -21,6 +21,8 @@
 /*                                                                   */
 /*********************************************************************/
 
+#include <cstdlib>
+#include <ctime>
 #include <stdexcept>
 
 #include "StrengthLine.hpp"
@@ -275,32 +277,32 @@ std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) c
 
 StrengthLine* StrengthLine::getRandomLine (QImage * img)
 {
-  srand (time(NULL));
+  std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand())); 
   
-  int x1rand = rand() % img->width();
-  int x2rand = rand() % img->width();
-  int y1rand = rand() % img->height();
-  int y2rand = rand() % img->height();
+  int x1rand = std::rand() % img->width();
+  int x2rand = std::rand() % img->width();
+  int y1rand = std::rand() % img->height();
+  int y2rand = std::rand() % img->height();
   while ( x1rand == x2rand && y1rand==y2rand )
     {
-      x2rand = rand() % img->width();
-      y2rand = rand() % img->height();
+      x2rand = std::rand() % img->width();
+      y2rand = std::rand() % img->height();
     }
   return new StrengthLine( new QPoint(x1rand, y1rand), new QPoint(x2rand, y2rand) );
 }
 
 StrengthLine* StrengthLine::getRandomLine (int width, int height)
 {
-  srand (time(NULL));
+  std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand())); 
   
-  int x1rand = rand() % width;
-  int x2rand = rand() % width;
-  int y1rand = rand() % height;
-  int y2rand = rand() % height;
+  int x1rand = std::rand() % width;
+  int x2rand = std::rand() % width;
+  int y1rand = std::rand() % height;
+  int y2rand = std::rand() % height;
   while ( x1rand == x2rand && y1rand==y2rand )
     {
-      x2rand = rand() % width;
-      y2rand = rand() % height;
+      x2rand = std::rand() % width;
+      y2rand = std::rand() % height;
     }
   return new StrengthLine( new QPoint(x1rand, y1rand), new QPoint(x2rand, y2rand) );
 }

+ 0 - 3
StrengthLine/StrengthLine.hpp

@@ -24,9 +24,6 @@
 #ifndef STRENGTHLINE_HPP
 #define STRENGTHLINE_HPP
 
-#include <cstdlib>
-#include <ctime>
-
 #include <QPoint>
 #include <QImage>
 

+ 119 - 52
csl/Experiment.cpp

@@ -1,8 +1,13 @@
+#include <cstdlib>
+#include <ctime>
+
 #include <QGuiApplication>
 #include <QScreen>
 #include <QWidget>
 #include <QMenuBar>
 #include <QFileDialog>
+#include <QInputDialog>
+#include <QDirIterator>
 #include <QVBoxLayout>
 #include <QHBoxLayout>
 #include <QPushButton>
@@ -16,6 +21,10 @@ Experiment::Experiment ():QMainWindow()
 {
   createActions ();
   createMenus ();
+  
+  filenames = NULL;
+  filenames_json = NULL;
+  suffix = NULL;
 
   setWindowTitle (tr ("DSL - Draw Strength Line"));
 
@@ -84,94 +93,152 @@ Experiment::Experiment ():QMainWindow()
   progressBar->setFixedHeight(20);
   progressBar->setRange(0, maxProgressBar);
   progressBar->setValue(0);
-  connect(progressBar, SIGNAL(valueChanged(int)), this, SLOT(progressBarValueChanged(int)));
+  // connect(progressBar, SIGNAL(valueChanged(int)), this, SLOT(progressBarValueChanged(int)));
   layoutV->addWidget(progressBar);
 
   timer = new QTimer(central);
   timer->setSingleShot(false);
   timer->setInterval(time);
-  connect(timer, SIGNAL(timeout()), this, SLOT(ticTimer()));
+  // connect(timer, SIGNAL(timeout()), this, SLOT(ticTimer()));
   
   setFixedSize (screenWidth, screenHeight);
 
-  /*POUR TEST*/
-  
-  QString img_a_ouvrir("../img_test/1675082395883.jpg");
-  QString sl1_a_ouvrir("../img_test/1675082395883_bob.json");
-  QString sl2_a_ouvrir("../img_test/1675082395883_charlotte.json");
-
-  openC( img_a_ouvrir, sl1_a_ouvrir, sl2_a_ouvrir);
-
-  timer->start();
+  /*timer->start();*/
 }
 
 void
 Experiment::open ()
-{
-  QString imageFilename = QFileDialog::getOpenFileName (this,
-						   QObject::
-						   tr ("Open image file"),
-						   QDir::currentPath (),
-						   QObject::
-						   tr
-						   ("Images files (*.jpg *.png);;All files (*.*)"));
-  QString SLLeftFilename = QFileDialog::getOpenFileName (this,
-						   QObject::
-						   tr ("Open strength lines file"),
-						   QDir::currentPath (),
-						   QObject::
-						   tr
-						   ("Text files (*.txt);;All files (*.*)"));
-  QString SLRightFilename = QFileDialog::getOpenFileName (this,
-						   QObject::
-						   tr ("Open strength lines file"),
-						   QDir::currentPath (),
-						   QObject::
-						   tr
-						   ("Text files (*.txt);;All files (*.*)"));
-
-  openC(imageFilename, SLLeftFilename, SLRightFilename);
+{  
+  QString dir = QFileDialog::getExistingDirectory(this,
+						  tr("Open Directory"),
+						  QDir::currentPath (),
+						  QFileDialog::ShowDirsOnly
+						  | QFileDialog::DontResolveSymlinks);
+  
+  QString* directoryname = new QString(dir);
+  QDir::setCurrent(*directoryname);
+  
+  if(!dir.isEmpty())
+    {
+      bool ok;
+      suffix = new QString(QInputDialog::getText(this, tr("Your ID"),
+						 tr("ID to use :"), QLineEdit::Normal,
+						 "_"+QDir::home().dirName(), &ok));
+      
+      if(ok)
+	{
+	  delete filenames;
+	  delete filenames_json;
+	  filenames = new QStringList();
+	  filenames_json = new QStringList();
+	  
+	  QDirIterator it(*directoryname, {"*.jpg", "*.png"}, QDir::Files);
+	  while (it.hasNext()) {
+	    filenames->append( it.next() );
+	  }
+
+	  for(int i = 0 ; i<filenames->size() ; i++)
+	    {
+	      QString jsonFile(QDir::currentPath ().append("/").append(QFileInfo(filenames->at(i)).baseName().append(*suffix_IA).append(".json")));
+	      if(! (QFile(jsonFile).exists()) )
+		{
+		  qDebug() << "ATTENTION !!!! " << jsonFile << " file missing";
+		  ok=false;
+		}else{
+		filenames_json->append(jsonFile);
+	      }
+	    }
+
+	  if(!ok)
+	    std::exit(EXIT_FAILURE);
+
+	  std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand())); 
+	  
+	  for(int i = 0 ; i<filenames->size()*2 ; i++)
+	    {
+	      int rand1 = std::rand()%filenames->size();
+	      int rand2 = std::rand()%filenames->size();
+	      filenames->swapItemsAt(rand1, rand2);
+	      filenames_json->swapItemsAt(rand1, rand2);
+	    }
+	  openedImage = -1;
+	}
+    }
+  openNext();
+  
 }
 
 void Experiment::leftImage(){
   qDebug() << "Clic sur image gauche" << Qt::endl;
   progressBar->setValue(0);
+  openNext();
 }
 
 void Experiment::rightImage(){
   qDebug() << "Clic sur image droite" << Qt::endl;
   progressBar->setValue(0);
+  openNext();
 }
 
-void Experiment::progressBarValueChanged(int value){
+/*void Experiment::progressBarValueChanged(int value){
   if(value == maxProgressBar ){
+    qDebug() << "That's all folks !" << Qt::endl;
     QApplication::quit();
   }
-}
+  }*/
 
-void Experiment::ticTimer(){
+/*void Experiment::ticTimer(){
   progressBar->setValue(progressBar->value()+time);
-}
+  }*/
 
 void
-Experiment::openC (QString image, QString leftSL, QString rightSL){
-  waLeft->loadImage (image.toStdString ());
-  waRight->loadImage (image.toStdString ());
-  waLeft->loadSL(leftSL.toStdString());
-  waRight->loadSL(rightSL.toStdString());
-  //waRight->addSL(StrengthLine::getRandomLine(waRight->getOriginalImage()));
-  waRight->addRandomSL();
-  waLeft->setReadOnly(true);
-  waRight->setReadOnly(true);
-  waLeft->repaint();
-  waRight->repaint();
-  setFixedSize (waLeft->geometry().width()*2+spaceInBetween, waLeft->geometry().height()+waLeft->geometry().y()+spaceInBetween+leftButton->geometry().height()+22); // +22 ??!!
+Experiment::openNext (){
+  openedImage = openedImage+1;
+  
+  progressBar->setValue((int)((openedImage+1.0)/(filenames->size())*100.0));
+  
+  if(openedImage >= filenames->size())
+    {
+      qDebug() << "That's all folks !" << Qt::endl;
+      QApplication::quit();
+    }
+  else
+    {
+
+      waLeft->loadImage (filenames->at(openedImage).toStdString ());
+      waRight->loadImage (filenames->at(openedImage).toStdString ());
+
+      std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
+      if((std::rand()%2) == 0)
+	{
+	  waLeft->loadSL (filenames_json->at(openedImage).toStdString ());
+	  for(int i = 0 ; i < waLeft->getNumberOfLines() ; i++)
+	    waRight->addRandomSL();
+	}
+      else
+	{
+	  waRight->loadSL (filenames_json->at(openedImage).toStdString ());
+	  for(int i = 0 ; i < waRight->getNumberOfLines() ; i++)
+	    waLeft->addRandomSL();
+	}
+      /*waLeft->loadImage (image.toStdString ());
+	waRight->loadImage (image.toStdString ());
+	waLeft->loadSL(leftSL.toStdString());
+	waRight->loadSL(rightSL.toStdString());
+	waRight->addRandomSL();*/
+      waLeft->setReadOnly(true);
+      waRight->setReadOnly(true);
+      waLeft->repaint();
+      waRight->repaint();
+      setFixedSize (waLeft->geometry().width()*2+spaceInBetween, waLeft->geometry().height()+waLeft->geometry().y()+spaceInBetween+leftButton->geometry().height()+22); // +22 ??!!
+    }
+
 }
 
 void
 Experiment::createActions ()
 {
-  openAct = new QAction (tr ("&Open image"), this);
+  openAct = new QAction (tr ("&Open directory"), this);
   openAct->setShortcuts (QKeySequence::Open);
   connect (openAct, &QAction::triggered, this, &Experiment::open);
 

+ 10 - 4
csl/Experiment.hpp

@@ -20,7 +20,7 @@ class Experiment: public QMainWindow
 private:
   const int spaceInBetween = 20;
   const int time = 100;
-  const int maxProgressBar = 5000;
+  const int maxProgressBar = 100;
   int screenWidth, screenHeight;
 
   QMenu *fileMenu;
@@ -35,6 +35,12 @@ private:
 
   QProgressBar *progressBar;
   QTimer *timer;
+  
+  QStringList *filenames;
+  QStringList *filenames_json;
+  int openedImage;
+  QString *suffix_IA = new QString("_remi");
+  QString *suffix;
 
   void createActions ();
   void createMenus ();
@@ -44,12 +50,12 @@ private slots:
   void open ();
   void leftImage();
   void rightImage();
-  void progressBarValueChanged(int value);
-  void ticTimer();
+  // void progressBarValueChanged(int value);
+  // void ticTimer();
   
 public:
   Experiment ();
-  void openC (QString image, QString leftSL, QString rightSL);
+  void openNext ();
   
 };
 

img_test/1675082327571.jpg → img_test/image_sans_json/1675082327571.jpg


img_test/1675082342945.jpg → img_test/image_sans_json/1675082342945.jpg