ObjectDetectionView.cc 7.27 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 44 45

ObjectDetectionView::ObjectDetectionView(QString folder, QWidget *parent) :
        QWidget(parent),
        patternList(),
46 47
        letterList(),
        letterTimer(),
pixhawk's avatar
pixhawk committed
48 49 50 51 52 53 54
        uas(NULL),
        patternFolder(folder),
        separator(" "),
        m_ui(new Ui::ObjectDetectionView)
{
    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 81 82 83 84 85
    if (this->uas != NULL)
    {
        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
86
    this->uas = uas;
87 88
    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
89 90
}

91
void ObjectDetectionView::newPattern(int uasId, QString patternPath, float confidence, bool detected)
pixhawk's avatar
pixhawk committed
92
{
93 94 95 96 97
    if (detected)
    {
        if (!patternList.contains(patternPath))
        {
            // Emit audio message on detection
98
            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
99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
            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<Pattern> 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
121
        QString filePath = patternFolder + "/" + patternPath.split("/", QString::SkipEmptyParts).last();
122 123 124 125 126 127 128 129
        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
130 131
        QString patternName = patternPath.split("/", QString::SkipEmptyParts).last(); // Remove preceding folder names
        patternName = patternName.split(".", QString::SkipEmptyParts).first(); // Remove file ending
132 133
        m_ui->nameLabel->setText("Pattern: " + patternName);
    }
pixhawk's avatar
pixhawk committed
134 135
}

136
void ObjectDetectionView::newLetter(int uasId, QString letter, float confidence, bool detected)
pixhawk's avatar
pixhawk committed
137
{
138 139
    Q_UNUSED(confidence);

pixhawk's avatar
pixhawk committed
140 141
    if (detected)
    {
142
        if (!letterList.contains(letter))
pixhawk's avatar
pixhawk committed
143
        {
144 145
            // Emit audio message on detection
            if (detected) GAudioOutput::instance()->say("System " + QString::number(uasId) + " detected letter " + letter);
pixhawk's avatar
pixhawk committed
146

147
            letterList.insert(letter, Pattern(letter, 0));
pixhawk's avatar
pixhawk committed
148 149 150
        }
        else
        {
151 152 153 154 155
            Pattern pattern = letterList.value(letter);
            pattern.confidence = 0;
            ++pattern.count;
            letterList.insert(letter, pattern);
        }
lm's avatar
lm committed
156

157
        updateLetterList();
158

159 160
        // display letter
        m_ui->letterLabel->setText(letter);
161

162 163 164 165
        // set textlabel
        m_ui->nameLabel->setText("Letter: " + letter);
    }
}
pixhawk's avatar
pixhawk committed
166

167 168 169 170 171 172
void ObjectDetectionView::decreaseLetterTime()
{
    foreach (Pattern pattern, letterList)
    {
        pattern.confidence -= 1;
        letterList.insert(pattern.name, pattern);
pixhawk's avatar
pixhawk committed
173
    }
174 175 176 177 178 179 180 181 182 183 184 185 186 187

    updateLetterList();
}

void ObjectDetectionView::updateLetterList()
{
    // set list items
    QList<Pattern> 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));
pixhawk's avatar
pixhawk committed
188 189
}

190 191 192 193
void ObjectDetectionView::clearLists()
{
    patternList.clear();
    letterList.clear();
194

195 196
    m_ui->listWidget->clear();
    m_ui->letterListWidget->clear();
197 198 199 200

    m_ui->imageLabel->clear();;
    m_ui->letterLabel->clear();
    m_ui->nameLabel->clear();
201 202
}

pixhawk's avatar
pixhawk committed
203 204 205 206 207
void ObjectDetectionView::takeAction()
{
    QAction* act = dynamic_cast<QAction*>(sender());
    if (act)
    {
208 209 210
        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
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

        // 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());
    }
}