ObjectDetectionView.cc 6.9 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
#include "MG.h"

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

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

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;
85 86
    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
87 88 89
    //}
}

90
void ObjectDetectionView::newPattern(int uasId, QString patternPath, float confidence, bool detected)
pixhawk's avatar
pixhawk committed
91
{
92 93 94 95 96 97
    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());
pixhawk's avatar
pixhawk committed
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            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
        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);
    }
pixhawk's avatar
pixhawk committed
133 134
}

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

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

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

156
        updateLetterList();
157

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

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

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

    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
187 188
}

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

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

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

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