diff --git a/label_img.cpp b/label_img.cpp index c10dd29..6aa6fb2 100644 --- a/label_img.cpp +++ b/label_img.cpp @@ -107,25 +107,37 @@ void label_img::setMousePosition(int x, int y) m_relative_mouse_pos_in_ui = cvtAbsoluteToRelativePoint(QPoint(x, y)); } -void label_img::openImage(const QString &qstrImg) +void label_img::openImage(const QString &qstrImg, bool &ret) { - m_objBoundingBoxes.clear(); + QImage img(qstrImg); - m_inputImg = QImage(qstrImg); - m_inputImg = m_inputImg.convertToFormat(QImage::Format_RGB888); - - m_bLabelingStarted = false; - - QPoint mousePosInUi = this->mapFromGlobal(QCursor::pos()); - bool mouse_is_in_image = QRect(0, 0, this->width(), this->height()).contains(mousePosInUi); - - if (mouse_is_in_image) + if(img.isNull()) { - setMousePosition(mousePosInUi.x(), mousePosInUi.y()); + m_inputImg = QImage(); + ret = false; } else { - setMousePosition(0., 0.); + ret = true; + + m_objBoundingBoxes.clear(); + + m_inputImg = img.copy(); + m_inputImg = m_inputImg.convertToFormat(QImage::Format_RGB888); + + m_bLabelingStarted = false; + + QPoint mousePosInUi = this->mapFromGlobal(QCursor::pos()); + bool mouse_is_in_image = QRect(0, 0, this->width(), this->height()).contains(mousePosInUi); + + if (mouse_is_in_image) + { + setMousePosition(mousePosInUi.x(), mousePosInUi.y()); + } + else + { + setMousePosition(0., 0.); + } } } @@ -199,6 +211,11 @@ void label_img::setFocusObjectName(QString qstrName) m_foucsedObjectName = qstrName; } +bool label_img::isOpened() +{ + return !m_inputImg.isNull(); +} + QImage label_img::crop(QRect rect) { return m_inputImg.copy(rect); diff --git a/label_img.h b/label_img.h index 928c90d..34b5c91 100644 --- a/label_img.h +++ b/label_img.h @@ -39,7 +39,7 @@ public: QVector m_objBoundingBoxes; void init(); - void openImage(const QString &); + void openImage(const QString &, bool& ret); void showImage(); void loadLabelData(const QString & ); @@ -47,6 +47,8 @@ public: void setFocusObjectLabel(int); void setFocusObjectName(QString); + + bool isOpened(); QImage crop(QRect); QRectF getRelativeRectFromTwoPoints(QPointF , QPointF); diff --git a/mainwindow.cpp b/mainwindow.cpp index b777c6a..eb35890 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -30,7 +30,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(new QShortcut(QKeySequence(Qt::Key_D), this), SIGNAL(activated()), this, SLOT(next_img())); connect(new QShortcut(QKeySequence(Qt::Key_Space), this), SIGNAL(activated()), this, SLOT(next_img())); - init_tableWidget(); + init_table_widget(); } MainWindow::~MainWindow() @@ -43,11 +43,11 @@ void MainWindow::on_pushButton_open_files_clicked() bool bRetImgDir = false; bool bRetObjFile = false; - openImgDir(bRetImgDir); + open_img_dir(bRetImgDir); if (!bRetImgDir) return ; - openObjListFile(bRetObjFile); + open_obj_file(bRetObjFile); if (!bRetObjFile) return ; @@ -58,16 +58,8 @@ void MainWindow::init() { ui->label_image->init(); - ui->horizontalSlider_images->setEnabled(true); - ui->pushButton_next->setEnabled(true); - ui->pushButton_prev->setEnabled(true); - ui->pushButton_save->setEnabled(true); - - ui->horizontalSlider_images->setRange(0, m_imgList.size() - 1); - - ui->horizontalSlider_images->blockSignals(true); - ui->horizontalSlider_images->setValue(0); - ui->horizontalSlider_images->blockSignals(false); + init_button_event(); + init_horizontal_slider(); set_label(0); goto_img(0); @@ -88,35 +80,19 @@ void MainWindow::set_focused_file(const int fileIndex) void MainWindow::goto_img(const int fileIndex) { - bool indexIsOutOfRange = (fileIndex < 0 || fileIndex > m_imgList.size() - 1); + bool bIndexIsOutOfRange = (fileIndex < 0 || fileIndex > m_imgList.size() - 1); + if (bIndexIsOutOfRange) return; - if(!indexIsOutOfRange) - { - m_imgIndex = fileIndex; + m_imgIndex = fileIndex; - ui->label_image->openImage(m_imgList.at(m_imgIndex)); - ui->label_image->loadLabelData(get_labeling_data(m_imgList.at(m_imgIndex))); - ui->label_image->showImage(); + bool bImgOpened; + ui->label_image->openImage(m_imgList.at(m_imgIndex), bImgOpened); - set_label_progress(m_imgIndex); - set_focused_file(m_imgIndex); - } -} + ui->label_image->loadLabelData(get_labeling_data(m_imgList.at(m_imgIndex))); + ui->label_image->showImage(); -void MainWindow::next_img() -{ - save_label_data(); - goto_img(m_imgIndex + 1); - - ui->horizontalSlider_images->blockSignals(true); - ui->horizontalSlider_images->setValue(m_imgIndex); - ui->horizontalSlider_images->blockSignals(false); -} - -void MainWindow::prev_img() -{ - save_label_data(); - goto_img(m_imgIndex - 1); + set_label_progress(m_imgIndex); + set_focused_file(m_imgIndex); //it blocks crash with slider change ui->horizontalSlider_images->blockSignals(true); @@ -124,6 +100,18 @@ void MainWindow::prev_img() ui->horizontalSlider_images->blockSignals(false); } +void MainWindow::next_img(bool bSavePrev) +{ + if(bSavePrev && ui->label_image->isOpened()) save_label_data(); + goto_img(m_imgIndex + 1); +} + +void MainWindow::prev_img(bool bSavePrev) +{ + if(bSavePrev) save_label_data(); + goto_img(m_imgIndex - 1); +} + void MainWindow::save_label_data()const { if(m_imgList.size() == 0) return; @@ -252,7 +240,7 @@ void MainWindow::set_label(const int labelIndex) } } -void MainWindow::set_label_color(int index, QColor color) +void MainWindow::set_label_color(const int index, const QColor color) { ui->label_image->m_drawObjectBoxColor.replace(index, color); } @@ -272,7 +260,7 @@ void MainWindow::pjreddie_style_msgBox(QMessageBox::Icon icon, QString title, QS msgBox.exec(); } -void MainWindow::openImgDir(bool& ret) +void MainWindow::open_img_dir(bool& ret) { pjreddie_style_msgBox(QMessageBox::Information,"Help", "Step 1. Open Your Data Set Directory"); @@ -289,21 +277,21 @@ void MainWindow::openImgDir(bool& ret) if(fileList.empty()) { - pjreddie_style_msgBox(QMessageBox::Critical,"Error", "This folder is empty"); ret = false; + pjreddie_style_msgBox(QMessageBox::Critical,"Error", "This folder is empty"); } else { + ret = true; m_imgDir = imgDir; m_imgList = fileList; for(QString& str: m_imgList) str = m_imgDir + "/" + str; - ret = true; } } -void MainWindow::openObjListFile(bool& ret) +void MainWindow::open_obj_file(bool& ret) { pjreddie_style_msgBox(QMessageBox::Information,"Help", "Step 2. Open Your Label List File(*.txt or *.names)"); @@ -315,16 +303,21 @@ void MainWindow::openObjListFile(bool& ret) if(fileLabelList.size() == 0) { - pjreddie_style_msgBox(QMessageBox::Critical,"Error", "LabelList file is not opened()"); ret = false; + pjreddie_style_msgBox(QMessageBox::Critical,"Error", "LabelList file is not opened()"); } else { - load_label_list_data(fileLabelList); ret = true; + load_label_list_data(fileLabelList); } } +void MainWindow::reupdate_img_list() +{ + +} + void MainWindow::wheelEvent(QWheelEvent *ev) { if(ev->delta() > 0) // up Wheel @@ -371,6 +364,30 @@ void MainWindow::on_pushButton_save_clicked() save_label_data(); } +void MainWindow::on_pushButton_remove_clicked() +{ + //remove a image + QFile::remove(m_imgList.at(m_imgIndex)); + + //remove a txt file + QString qstrOutputLabelData = get_labeling_data(m_imgList.at(m_imgIndex)); + QFile::remove(qstrOutputLabelData); + + m_imgList.removeAt(m_imgIndex); + + if(m_imgList.size() == 0) + { + pjreddie_style_msgBox(QMessageBox::Information,"End", "In directory, there are not any image. program quit."); + QCoreApplication::quit(); + } + else if( m_imgIndex == m_imgList.size()) + { + m_imgIndex --; + } + + goto_img(m_imgIndex); +} + void MainWindow::on_tableWidget_label_cellDoubleClicked(int row, int column) { bool bColorAttributeClicked = (column == 1); @@ -398,11 +415,24 @@ void MainWindow::on_horizontalSlider_images_sliderMoved(int position) goto_img(position); } -void MainWindow::on_horizontalSlider_images_sliderPressed() +void MainWindow::init_button_event() { + ui->pushButton_next->setEnabled(true); + ui->pushButton_prev->setEnabled(true); + ui->pushButton_save->setEnabled(true); + ui->pushButton_remove->setEnabled(true); } -void MainWindow::init_tableWidget() +void MainWindow::init_horizontal_slider() +{ + ui->horizontalSlider_images->setEnabled(true); + ui->horizontalSlider_images->setRange(0, m_imgList.size() - 1); + ui->horizontalSlider_images->blockSignals(true); + ui->horizontalSlider_images->setValue(0); + ui->horizontalSlider_images->blockSignals(false); +} + +void MainWindow::init_table_widget() { ui->tableWidget_label->horizontalHeader()->setVisible(true); ui->tableWidget_label->horizontalHeader()->setStyleSheet(""); diff --git a/mainwindow.h b/mainwindow.h index c3dbc51..eeb0cfc 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -24,30 +24,31 @@ public: private slots: void on_pushButton_open_files_clicked(); void on_pushButton_save_clicked(); + void on_pushButton_remove_clicked(); void on_pushButton_prev_clicked(); void on_pushButton_next_clicked(); void keyPressEvent(QKeyEvent *); - void next_img(); - void prev_img(); + void next_img(bool bSavePrev = true); + void prev_img(bool bSavePrev = true); void save_label_data() const; void clear_label_data(); void next_label(); void prev_label(); - void on_tableWidget_label_cellDoubleClicked(int row, int column); - void on_tableWidget_label_cellClicked(int row, int column); + void on_tableWidget_label_cellDoubleClicked(int , int ); + void on_tableWidget_label_cellClicked(int , int ); - void on_horizontalSlider_images_sliderMoved(int position); - void on_horizontalSlider_images_sliderPressed(); + void on_horizontalSlider_images_sliderMoved(int ); private: - void init_tableWidget(); - void init(); + void init_table_widget(); + void init_button_event(); + void init_horizontal_slider(); void img_open(const int); void set_label_progress(const int); @@ -59,12 +60,14 @@ private: QString get_labeling_data(QString)const; void set_label(const int); - void set_label_color(const int , QColor); + void set_label_color(const int , const QColor); void pjreddie_style_msgBox(QMessageBox::Icon, QString, QString); - void openImgDir(bool&); - void openObjListFile(bool&); + void open_img_dir(bool&); + void open_obj_file(bool&); + + void reupdate_img_list(); Ui::MainWindow *ui; diff --git a/mainwindow.ui b/mainwindow.ui index a153ae0..33aee3f 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -340,7 +340,7 @@ border-color: rgb(0, 255, 255); - 271 + 200 91 @@ -384,7 +384,7 @@ border-color: rgb(0, 255, 255); - 271 + 200 91 @@ -427,6 +427,37 @@ border-color: rgb(0, 255, 255); + + + + false + + + + 200 + 91 + + + + + 75 + true + + + + Qt::NoFocus + + + background-color : rgb(0, 0, 17);color : rgb(0, 255, 255); +border-style: outset; +border-width: 2px; +border-color: rgb(0, 255, 255); + + + Remove + + + @@ -450,6 +481,9 @@ border-color: rgb(0, 255, 255); ArrowCursor + + Qt::NoFocus + background-color : rgb(0, 0, 17);color : rgb(0, 255, 255); border-style: outset; @@ -664,7 +698,7 @@ QTableView { 2 - true + false false