/*===================================================================== PIXHAWK Micro Air Vehicle Flying Robotics Toolkit (c) 2009, 2010 PIXHAWK PROJECT This file is part of the PIXHAWK project PIXHAWK is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. PIXHAWK is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with PIXHAWK. If not, see . ======================================================================*/ /** * @file * @brief List of detected objects * @author Benjamin Knecht * @author Lorenz Meier * @author Fabian Landau * */ #include #include #include "ObjectDetectionView.h" #include "ui_ObjectDetectionView.h" #include "UASManager.h" #include "GAudioOutput.h" #include #include #include "MG.h" ObjectDetectionView::ObjectDetectionView(QString folder, QWidget *parent) : QWidget(parent), patternList(), letterList(), letterTimer(), uas(NULL), patternFolder(folder), separator(" "), m_ui(new Ui::ObjectDetectionView) { m_ui->setupUi(this); connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setUAS(UASInterface*))); letterTimer.start(1000); connect(&letterTimer, SIGNAL(timeout()), this, SLOT(decreaseLetterTime())); } ObjectDetectionView::~ObjectDetectionView() { delete m_ui; } void ObjectDetectionView::changeEvent(QEvent *e) { switch (e->type()) { case QEvent::LanguageChange: m_ui->retranslateUi(this); break; default: break; } } void ObjectDetectionView::setUAS(UASInterface* uas) { //if (this->uas == NULL && uas != NULL) //{ this->uas = uas; connect(uas, SIGNAL(patternDetected(int, QString, float, bool)), this, SLOT(newPattern(int, QString, float, bool))); connect(uas, SIGNAL(letterDetected(int, QString, float, bool)), this, SLOT(newLetter(int, QString, float, bool))); //} } void ObjectDetectionView::newPattern(int uasId, QString patternPath, float confidence, bool detected) { if (detected) { if (!patternList.contains(patternPath)) { // Emit audio message on detection if (detected) GAudioOutput::instance()->say("System " + QString::number(uasId) + " detected pattern " + QString(patternPath.split("/").last()).split(".").first()); patternList.insert(patternPath, Pattern(patternPath, confidence)); } else { Pattern pattern = patternList.value(patternPath); if (confidence > pattern.confidence) pattern.confidence = confidence; ++pattern.count; patternList.insert(patternPath, pattern); } // set list items QList templist; foreach (Pattern pattern, patternList) templist.push_back(pattern); qSort(templist); m_ui->listWidget->clear(); foreach (Pattern pattern, templist) m_ui->listWidget->addItem(pattern.name + separator + "(" + QString::number(pattern.count) + ")" + separator + QString::number(pattern.confidence)); // load image QString filePath = MG::DIR::getSupportFilesDirectory() + "/" + patternFolder + "/" + patternPath.split("/").last(); QPixmap image = QPixmap(filePath); if (image.width() > image.height()) image = image.scaledToWidth(m_ui->imageLabel->width()); else image = image.scaledToHeight(m_ui->imageLabel->height()); m_ui->imageLabel->setPixmap(image); // set textlabel QString patternName = patternPath.split("/").last(); // Remove preceding folder names patternName = patternName.split(".").first(); // Remove file ending m_ui->nameLabel->setText("Pattern: " + patternName); } } void ObjectDetectionView::newLetter(int uasId, QString letter, float confidence, bool detected) { Q_UNUSED(confidence); if (detected) { if (!letterList.contains(letter)) { // Emit audio message on detection if (detected) GAudioOutput::instance()->say("System " + QString::number(uasId) + " detected letter " + letter); letterList.insert(letter, Pattern(letter, 0)); } else { Pattern pattern = letterList.value(letter); pattern.confidence = 0; ++pattern.count; letterList.insert(letter, pattern); } updateLetterList(); // display letter m_ui->letterLabel->setText(letter); // set textlabel m_ui->nameLabel->setText("Letter: " + letter); } } void ObjectDetectionView::decreaseLetterTime() { foreach (Pattern pattern, letterList) { pattern.confidence -= 1; letterList.insert(pattern.name, pattern); } updateLetterList(); } void ObjectDetectionView::updateLetterList() { // set list items QList templist; foreach (Pattern pattern, letterList) templist.push_back(pattern); qSort(templist); m_ui->letterListWidget->clear(); foreach (Pattern pattern, templist) m_ui->letterListWidget->addItem(pattern.name + separator + "(" + QString::number(pattern.count) + ")" + separator + QString::number(pattern.confidence)); } void ObjectDetectionView::takeAction() { QAction* act = dynamic_cast(sender()); if (act) { QString patternPath = act->text().trimmed().split(separator).first(); // Remove additional information QString patternName = patternPath.split("//").last(); // Remove preceding folder names patternName = patternName.split(".").first(); // Remove file ending // Set name and label m_ui->nameLabel->setText(patternName); m_ui->imageLabel->setPixmap(act->icon().pixmap(64, 64)); } } void ObjectDetectionView::resizeEvent(QResizeEvent * event ) { if (event->isAccepted()) { // Enforce square shape of image label m_ui->imageLabel->resize(m_ui->imageLabel->width(), m_ui->imageLabel->width()); } }