ObjectDetectionView.cc 7.15 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*=====================================================================

PIXHAWK Micro Air Vehicle Flying Robotics Toolkit

(c) 2009, 2010 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>

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 <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief List of detected objects
 *   @author Benjamin Knecht <mavteam@student.ethz.ch>
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
29
 *   @author Fabian Landau <mavteam@student.ethz.ch>
pixhawk's avatar
pixhawk committed
30 31 32 33 34 35 36 37
 *
 */

#include <QListView>
#include <QPixmap>
#include "ObjectDetectionView.h"
#include "ui_ObjectDetectionView.h"
#include "UASManager.h"
lm's avatar
lm committed
38
#include "GAudioOutput.h"
pixhawk's avatar
pixhawk committed
39 40

#include <QDebug>
41
#include <QMap>
pixhawk's avatar
pixhawk committed
42 43

ObjectDetectionView::ObjectDetectionView(QString folder, QWidget *parent) :
44 45 46 47 48 49 50 51
    QWidget(parent),
    patternList(),
    letterList(),
    letterTimer(),
    uas(NULL),
    patternFolder(folder),
    separator(" "),
    m_ui(new Ui::ObjectDetectionView)
pixhawk's avatar
pixhawk committed
52 53 54
{
    m_ui->setupUi(this);
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setUAS(UASInterface*)));
55 56
    letterTimer.start(1000);
    connect(&letterTimer, SIGNAL(timeout()), this, SLOT(decreaseLetterTime()));
57
    connect(m_ui->clearButton, SIGNAL(clicked()), this, SLOT(clearLists()));
58 59

    this->setVisible(false);
pixhawk's avatar
pixhawk committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
}

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)
{
80
    if (this->uas != NULL) {
81 82 83 84
        disconnect(this->uas, SIGNAL(patternDetected(int, QString, float, bool)), this, SLOT(newPattern(int, QString, float, bool)));
        disconnect(this->uas, SIGNAL(letterDetected(int, QString, float, bool)), this, SLOT(newLetter(int, QString, float, bool)));
    }

pixhawk's avatar
pixhawk committed
85
    this->uas = uas;
86 87
    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)));
pixhawk's avatar
pixhawk committed
88 89
}

90
void ObjectDetectionView::newPattern(int uasId, QString patternPath, float confidence, bool detected)
pixhawk's avatar
pixhawk committed
91
{
92 93
    if (detected) {
        if (!patternList.contains(patternPath)) {
94
            // Emit audio message on detection
95
            if (detected) GAudioOutput::instance()->say("System " + QString::number(uasId) + " detected pattern " + QString(patternPath.split("/", QString::SkipEmptyParts).last()).split(".", QString::SkipEmptyParts).first());
pixhawk's avatar
pixhawk committed
96

97
            patternList.insert(patternPath, Pattern(patternPath, confidence));
98
        } else {
99 100 101 102 103 104 105 106 107 108
            Pattern pattern = patternList.value(patternPath);
            if (confidence > pattern.confidence)
                pattern.confidence = confidence;
            ++pattern.count;
            patternList.insert(patternPath, pattern);
        }

        // set list items
        QList<Pattern> templist;
        foreach (Pattern pattern, patternList)
109
        templist.push_back(pattern);
110 111 112
        qSort(templist);
        m_ui->listWidget->clear();
        foreach (Pattern pattern, templist)
113
        m_ui->listWidget->addItem(pattern.name + separator + "(" + QString::number(pattern.count) + ")" + separator + QString::number(pattern.confidence));
114 115

        // load image
116
        QString filePath = patternFolder + "/" + patternPath.split("/", QString::SkipEmptyParts).last();
117 118 119 120 121 122 123 124
        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
125 126
        QString patternName = patternPath.split("/", QString::SkipEmptyParts).last(); // Remove preceding folder names
        patternName = patternName.split(".", QString::SkipEmptyParts).first(); // Remove file ending
127 128
        m_ui->nameLabel->setText("Pattern: " + patternName);
    }
pixhawk's avatar
pixhawk committed
129 130
}

131
void ObjectDetectionView::newLetter(int uasId, QString letter, float confidence, bool detected)
pixhawk's avatar
pixhawk committed
132
{
133 134
    Q_UNUSED(confidence);

135 136
    if (detected) {
        if (!letterList.contains(letter)) {
137 138
            // Emit audio message on detection
            if (detected) GAudioOutput::instance()->say("System " + QString::number(uasId) + " detected letter " + letter);
pixhawk's avatar
pixhawk committed
139

140
            letterList.insert(letter, Pattern(letter, 0));
141
        } else {
142 143 144 145 146
            Pattern pattern = letterList.value(letter);
            pattern.confidence = 0;
            ++pattern.count;
            letterList.insert(letter, pattern);
        }
lm's avatar
lm committed
147

148
        updateLetterList();
149

150 151
        // display letter
        m_ui->letterLabel->setText(letter);
152

153 154 155 156
        // set textlabel
        m_ui->nameLabel->setText("Letter: " + letter);
    }
}
pixhawk's avatar
pixhawk committed
157

158 159
void ObjectDetectionView::decreaseLetterTime()
{
160
    foreach (Pattern pattern, letterList) {
161 162
        pattern.confidence -= 1;
        letterList.insert(pattern.name, pattern);
pixhawk's avatar
pixhawk committed
163
    }
164 165 166 167 168 169 170 171 172

    updateLetterList();
}

void ObjectDetectionView::updateLetterList()
{
    // set list items
    QList<Pattern> templist;
    foreach (Pattern pattern, letterList)
173
    templist.push_back(pattern);
174 175 176
    qSort(templist);
    m_ui->letterListWidget->clear();
    foreach (Pattern pattern, templist)
177
    m_ui->letterListWidget->addItem(pattern.name + separator + "(" + QString::number(pattern.count) + ")" + separator + QString::number(pattern.confidence));
pixhawk's avatar
pixhawk committed
178 179
}

180 181 182 183
void ObjectDetectionView::clearLists()
{
    patternList.clear();
    letterList.clear();
184

185 186
    m_ui->listWidget->clear();
    m_ui->letterListWidget->clear();
187 188 189 190

    m_ui->imageLabel->clear();;
    m_ui->letterLabel->clear();
    m_ui->nameLabel->clear();
191 192
}

pixhawk's avatar
pixhawk committed
193 194 195
void ObjectDetectionView::takeAction()
{
    QAction* act = dynamic_cast<QAction*>(sender());
196
    if (act) {
197 198 199
        QString patternPath = act->text().trimmed().split(separator, QString::SkipEmptyParts).first(); // Remove additional information
        QString patternName = patternPath.split("//", QString::SkipEmptyParts).last(); // Remove preceding folder names
        patternName = patternName.split(".", QString::SkipEmptyParts).first(); // Remove file ending
pixhawk's avatar
pixhawk committed
200 201 202 203 204 205 206 207 208

        // Set name and label
        m_ui->nameLabel->setText(patternName);
        m_ui->imageLabel->setPixmap(act->icon().pixmap(64, 64));
    }
}

void ObjectDetectionView::resizeEvent(QResizeEvent * event )
{
209
    if (event->isAccepted()) {
pixhawk's avatar
pixhawk committed
210 211 212 213 214
        // Enforce square shape of image label
        m_ui->imageLabel->resize(m_ui->imageLabel->width(), m_ui->imageLabel->width());
    }
}