Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 138 additions & 52 deletions ClassificationTool/classificationtools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,51 @@
#include <QKeySequence>
#include <QShortcut>

#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <QDir>
#include <QFileInfo>
#include <QMessageBox>

ClassificationTools::ClassificationTools(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ClassificationTools)
{
ui->setupUi(this);

QObject::connect(ui->selectFolder1Btn, &QPushButton::clicked, this, &ClassificationTools::onClickSelectFolder1);
QObject::connect(ui->selectFolder2Btn, &QPushButton::clicked, this, &ClassificationTools::onClickSelectFolder2);

QObject::connect(ui->filePath1, &QLineEdit::textChanged, this, &ClassificationTools::listFolderPaths1);
QObject::connect(ui->filePath2, &QLineEdit::textChanged, this, &ClassificationTools::listFolderPaths2);
QObject::connect(ui->selectFolder1Btn, &QPushButton::clicked, this, &ClassificationTools::onClickSelectFolder1);
QObject::connect(ui->selectFolder2Btn, &QPushButton::clicked, this, &ClassificationTools::onClickSelectFolder2);

QObject::connect(ui->folder1ActionBtn, &QPushButton::clicked, this, [&](){
this->currentIndex = 0;
QObject::connect(ui->filePath1, &QLineEdit::textChanged, this, &ClassificationTools::listFolderPaths1);
QObject::connect(ui->filePath2, &QLineEdit::textChanged, this, &ClassificationTools::listFolderPaths2);
QObject::connect(ui->folder1ActionBtn, &QPushButton::clicked, this, [&](){
//this->currentIndex = 0;
this->activeFolderInfoList = &this->folder1InfoList;
this->moveTargetDir = ui->filePath2->text();
setPreviewImage();
ui->frame_1->setStyleSheet("QFrame#frame_1 {border: 1px solid #0000FF}");
ui->frame_2->setStyleSheet("");
});
QObject::connect(ui->folder2ActionBtn, &QPushButton::clicked, this, [&](){
this->currentIndex = 0;
});
QObject::connect(ui->folder2ActionBtn, &QPushButton::clicked, this, [&](){
//this->currentIndex = 0;
this->activeFolderInfoList = &this->folder2InfoList;
this->moveTargetDir = ui->filePath1->text();
setPreviewImage();
ui->frame_1->setStyleSheet("");
ui->frame_2->setStyleSheet("QFrame#frame_2 {border: 1px solid #0000FF}");
});

ui->filePath1->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->filePath2->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->selectFolder1Btn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->selectFolder2Btn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->folder1ActionBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->folder2ActionBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->previousBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->moveBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->nextBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->undoBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
});

ui->filePath1->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->filePath2->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->selectFolder1Btn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->selectFolder2Btn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->folder1ActionBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->folder2ActionBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->previousBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->moveBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->nextBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
ui->undoBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);

QShortcut *shortcutNext = new QShortcut(QKeySequence(Qt::Key_Right), this);
QObject::connect(shortcutNext, &QShortcut::activated, this, &ClassificationTools::onNextPressed);
Expand All @@ -57,45 +67,89 @@ ClassificationTools::ClassificationTools(QWidget *parent)
QShortcut *shortcutUndo = new QShortcut(QKeySequence(Qt::Key_Backspace), this);
QObject::connect(shortcutUndo, &QShortcut::activated, this, &ClassificationTools::onUndoPressed);

QObject::connect(ui->nextBtn, &QPushButton::pressed, this, &ClassificationTools::onNextPressed);
QObject::connect(ui->previousBtn, &QPushButton::pressed, this, &ClassificationTools::onNextPressed);
QObject::connect(ui->moveBtn, &QPushButton::pressed, this, &ClassificationTools::onNextPressed);
QObject::connect(ui->undoBtn, &QPushButton::pressed, this, &ClassificationTools::onUndoPressed);
QObject::connect(ui->nextBtn, &QPushButton::pressed, this, &ClassificationTools::onNextPressed);
QObject::connect(ui->previousBtn, &QPushButton::pressed, this, &ClassificationTools::onPreviousPressed);
QObject::connect(ui->moveBtn, &QPushButton::pressed, this, &ClassificationTools::onMovePressed);
QObject::connect(ui->undoBtn, &QPushButton::pressed, this, &ClassificationTools::onUndoPressed);

if (!falsePositiveDir.exists()) {
auto path = falsePositiveDir.absolutePath();
if (!falsePositiveDir.mkpath(".")) {
QString errMsg = QString("Error creating folder %1").arg(path);
qDebug() << errMsg;
QMessageBox::critical(this, "Create Folder", errMsg, QMessageBox::StandardButton::NoButton, QMessageBox::StandardButton::NoButton);
}
}

loadConfig();
}

ClassificationTools::~ClassificationTools()
{
delete ui;
}

QString workingPath = "/home/mert/Documents/person_filtered/person"; // QDir::homePath()

void ClassificationTools::onClickSelectFolder1()
{
QString dirName = QFileDialog::getExistingDirectory(this, "Open a folder", workingPath);
ui->filePath1->setText(dirName);
this->folder1InfoList = QDir(dirName).entryInfoList(QDir::Files);
ui->filePath1->setText(dirName);
}

void ClassificationTools::onClickSelectFolder2()
{
QString dirName = QFileDialog::getExistingDirectory(this, "Open a folder", workingPath);
ui->filePath2->setText(dirName);
this->folder2InfoList = QDir(dirName).entryInfoList(QDir::Files);
ui->filePath2->setText(dirName);
}

void ClassificationTools::listFolderPaths1(QString path)
{
listImages(path);
this->folder1InfoList = QDir(path).entryInfoList(QDir::Files);
}

void ClassificationTools::listFolderPaths2(QString path)
{
listImages(path);
this->folder2InfoList = QDir(path).entryInfoList(QDir::Files);
}

void ClassificationTools::listImages(QString path) {
qDebug() << path;
void ClassificationTools::saveConfig()
{
QJsonObject obj
{
{"currentIndex", this->currentIndex},
{"folder1", ui->filePath1->text()},
{"folder2", ui->filePath2->text()}
};

QJsonDocument doc;
doc.setObject(obj);
configFile.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text);
configFile.write(doc.toJson());
configFile.flush();
configFile.close();

qDebug() << "Saved config " << doc.toJson();
}

void ClassificationTools::loadConfig()
{
configFile.open(QIODevice::OpenModeFlag::ReadOnly);
QString config(configFile.readAll());
configFile.flush();
configFile.close();

qDebug() << "Load config " << config;
QJsonDocument doc = QJsonDocument::fromJson(config.toUtf8());
QJsonObject obj = doc.object();
this->currentIndex = obj["currentIndex"].toInt();
ui->filePath1->setText(obj["folder1"].toString());
ui->filePath2->setText(obj["folder2"].toString());

ui->folder1ActionBtn->click();
}

void ClassificationTools::closeEvent(QCloseEvent *event)
{
saveConfig();
}

void ClassificationTools::onNextPressed() {
Expand All @@ -106,35 +160,67 @@ void ClassificationTools::onNextPressed() {

auto it = this->activeFolderInfoList;
this->currentIndex++;
if (this->currentIndex > it->size() - 1)
if (this->currentIndex >= it->size()) {
this->currentIndex = it->size() - 1;
QMessageBox::information(this, "", "Last image", QMessageBox::StandardButton::NoButton, QMessageBox::StandardButton::NoButton);
return;
}

auto fi = it->at(this->currentIndex);
qDebug() << "next" << fi.fileName();
ui->previewImage->setPixmap(QPixmap(fi.absoluteFilePath()));
qDebug() << "next";
setPreviewImage();
}

void ClassificationTools::onPreviousPressed() {
if (this->activeFolderInfoList == nullptr) {
qDebug() << "Not set current folder !!!!!!";
this->currentIndex--;
if (this->currentIndex < 0) {
this->currentIndex = 0;
QMessageBox::warning(this, "", "First image", QMessageBox::StandardButton::NoButton, QMessageBox::StandardButton::NoButton);
return;
}

auto it = this->activeFolderInfoList;
this->currentIndex--;
if (this->currentIndex < 0)
this->currentIndex = 0;
qDebug() << "pre";
setPreviewImage();
}

void ClassificationTools::onMovePressed() {
if (this->activeFolderInfoList == nullptr || moveTargetDir == nullptr || moveTargetDir.isEmpty()) {
qDebug() << "Not selected move target !!!";
return;
}

auto it = this->activeFolderInfoList;
auto fi = it->at(this->currentIndex);
qDebug() << "pre" << fi.fileName();
ui->previewImage->setPixmap(QPixmap(fi.absoluteFilePath()));
QFile::copy(fi.absoluteFilePath(), falsePositiveDir.absoluteFilePath(fi.fileName()));
auto isMove = QFile::rename(fi.absoluteFilePath(), QDir(moveTargetDir).absoluteFilePath(fi.fileName()));
if (isMove) {
qDebug() << "Moved file name: " << isMove << fi.absoluteFilePath() << " >> " << moveTargetDir;

it->removeAt(this->currentIndex);

{
this->folder1InfoList = QDir(ui->filePath1->text()).entryInfoList(QDir::Files);
this->folder2InfoList = QDir(ui->filePath2->text()).entryInfoList(QDir::Files);
}
onNextPressed();
}
}

void ClassificationTools::onMovePressed() {
qDebug() << "move";
void ClassificationTools::setPreviewImage() {
if (this->activeFolderInfoList == nullptr)
return;

if (this->activeFolderInfoList->empty()) {
QMessageBox::critical(this, "", "Image not found", QMessageBox::StandardButton::NoButton, QMessageBox::StandardButton::NoButton);
return;
}
if (this->currentIndex < 0 || this->activeFolderInfoList->size() <= this->currentIndex)
return;

ui->statusbar->showMessage(QString("%1/%2").arg(this->currentIndex).arg(this->activeFolderInfoList->size()));
auto fi = this->activeFolderInfoList->at(this->currentIndex);
ui->previewImage->setPixmap(QPixmap(fi.absoluteFilePath()));
}

void ClassificationTools::onUndoPressed() {
qDebug() << "undo";
qDebug() << "TODO: undo";
}

17 changes: 17 additions & 0 deletions ClassificationTool/classificationtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,33 @@ public slots:
void onClickSelectFolder2();
void listFolderPaths1(QString path);
void listFolderPaths2(QString path);
void saveConfig();
void loadConfig();

private:
Ui::ClassificationTools *ui;

QString workingPath = "/home/mert/Documents/person_filtered/person"; // QDir::homePath()
QDir falsePositiveDir = QDir::cleanPath(workingPath + QDir::separator() + "false-positive");

void listImages(QString path);
void updateFolder1List();
void updateFolder2List();
void setPreviewImage();

QFile configFile = QFile("config.json");
int currentIndex = 0;
QString folder1Path;
QString folder2Path;

QFileInfoList* activeFolderInfoList = nullptr;
QFileInfoList folder1InfoList;
QFileInfoList folder2InfoList;

QString moveTargetDir;

protected:
void closeEvent(QCloseEvent *event);
};

#endif // CLASSIFICATION_H