ObjectDetectionView.cc 7.03 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 80 81 82
}

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;
83 84
    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
85 86 87
    //}
}

88
void ObjectDetectionView::newPattern(int uasId, QString patternPath, float confidence, bool detected)
pixhawk's avatar
pixhawk committed
89
{
90 91 92 93 94
    if (detected)
    {
        if (!patternList.contains(patternPath))
        {
            // 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            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
118
        QString filePath = patternFolder + "/" + patternPath.split("/", QString::SkipEmptyParts).last();
119 120 121 122 123 124 125 126
        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
127 128
        QString patternName = patternPath.split("/", QString::SkipEmptyParts).last(); // Remove preceding folder names
        patternName = patternName.split(".", QString::SkipEmptyParts).first(); // Remove file ending
129 130
        m_ui->nameLabel->setText("Pattern: " + patternName);
    }
pixhawk's avatar
pixhawk committed
131 132
}

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

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

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

154
        updateLetterList();
155

156 157
        // display letter
        m_ui->letterLabel->setText(letter);
158

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

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

    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
185 186
}

187 188 189 190
void ObjectDetectionView::clearLists()
{
    patternList.clear();
    letterList.clear();
191

192 193
    m_ui->listWidget->clear();
    m_ui->letterListWidget->clear();
194 195 196 197

    m_ui->imageLabel->clear();;
    m_ui->letterLabel->clear();
    m_ui->nameLabel->clear();
198 199
}

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

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