Blame view

EditSimShotDialog.cpp 6.27 KB
3f2992b2c   bostx   V1.0
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  #include <QGridLayout>
  #include <QPixmap>
  #include <QLabel>
  
  #include <opencv2/core/core.hpp>
  #include <opencv2/highgui/highgui.hpp>
  #include <opencv2/imgproc/imgproc.hpp>
  
  #include "EditSimShotDialog.h"
  #include "Convert.h"
  
  #include <QDebug>
  
  using namespace cv;
  
  EditSimShotDialog::EditSimShotDialog(const QString &fName, int currIdx, const QList<qint64> &shotPositions, int nVignettes, int frameWidth, QWidget *parent)
    : QDialog(parent),
      m_nVignettes(nVignettes),
      m_refIdx(currIdx),
      m_currIdx(currIdx),
      m_shotPositions(shotPositions),
      m_nCamera(0)
  {
    int fWidth;
    int fHeight;
  
    // vector containing the camera ids
    m_shotCamera.resize(shotPositions.size());
  
    // retrieving movie features
    m_cap.release();
    m_cap.open(fName.toStdString());
    fWidth = m_cap.get(CV_CAP_PROP_FRAME_WIDTH);
    fHeight = m_cap.get(CV_CAP_PROP_FRAME_HEIGHT);
    m_vignetteWidth = frameWidth / m_nVignettes;
    m_vignetteHeight = m_vignetteWidth * fHeight / fWidth;
  
    // doesn't work anymore after updating Ubuntu to 14.04
    // m_frameDur = 1 / m_cap.get(CV_CAP_PROP_FPS) * 1000;
    m_frameDur = 1 / 25.0 * 1000;
  
    // initializing vignette list
    int i(m_refIdx);
  
    while (i >= m_refIdx - m_nVignettes + 1) {
      if (i < 0)
        m_vignettePositions.push_front(-1);
      else
        m_vignettePositions.push_front(m_shotPositions[i] - m_frameDur);
      i--;
    }
  
    m_currentVignette = new QLabel;
    m_currentVignette->setPixmap(refShot());
  
    m_vignetteWidget = new VignetteWidget(m_nVignettes, frameWidth);
    m_vignetteWidget->setVideoCapture(fName);
    m_vignetteWidget->updateVignette(m_vignettePositions);
  
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(m_currentVignette, 0, 0, Qt::AlignRight);
    layout->addWidget(m_vignetteWidget, 1, 0, Qt::AlignRight);
  
    setLayout(layout);
    
    setWindowTitle("Annotate similar shots - Shot " + QString::number(m_refIdx + 1));
  
    // setting deletion operations vector
    m_delOp.resize(m_shotPositions.size());
    m_delOp.fill(false);
  }
  
  ///////////////////////////////////
  // slot called when pressing key //
  ///////////////////////////////////
  
  void EditSimShotDialog::keyPressEvent(QKeyEvent *event)
  {
    int toAddIdx; // index of shot to add to visible ones
    int idxToDel; // index of single linked shot to untie
  
    /********************************/
    /* going through previous shots */
    /********************************/
  
    if (event->key() == Qt::Key_Left && m_currIdx > m_nVignettes / 2) {
  
      m_currIdx--;
      toAddIdx = m_currIdx - m_nVignettes + 1;
  
      if (toAddIdx < 0)
        m_vignettePositions.push_front(-1);
      else
        m_vignettePositions.push_front(m_shotPositions[toAddIdx] - m_frameDur);
      m_vignettePositions.pop_back();
    }
    
    else if (event->key() == Qt::Key_Right && m_currIdx < m_refIdx) {
  
      m_vignettePositions.push_back(m_shotPositions[++m_currIdx] - m_frameDur);
      m_vignettePositions.pop_front();
    }
  
    /*******************************************************/
    /* linking current shot to previous one or ignoring it */
    /*******************************************************/
  
    else if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Backspace) {
  
      // no similar shot found: increment camera label
      if (event->key() == Qt::Key_Backspace) {
        m_shotCamera[m_refIdx] = m_nCamera++;
  
        // setting corresponding deletion operation to true
        m_delOp[m_refIdx] = true;
      }
  
      // similar shot found: label current shot with selected one
      else if (event->key() == Qt::Key_Return) {
        
        // linking the shots
        m_shotCamera[m_refIdx] = m_shotCamera[m_currIdx - 1];
        emit labelSimShot(m_shotPositions[m_currIdx - 1], m_shotCamera[m_currIdx - 1], Segment::Manual);
        emit labelSimShot(m_shotPositions[m_refIdx], m_shotCamera[m_refIdx], Segment::Manual);
  
        // setting corresponding deletion operation to false
        m_delOp[m_refIdx] = false;
      }
  
      // move to next shot position
      m_currIdx = ++m_refIdx;
  
      // last shot annotated: exit dialog
      if (m_refIdx == m_shotPositions.size())
        accept();
  
      else {
  
        // re-initializing vignette list
        int i(m_refIdx);
        m_vignettePositions.clear();
  
        while (i >= m_refIdx - m_nVignettes + 1) {
  	if (i < 0)
  	  m_vignettePositions.push_front(-1);
  	else
  	  m_vignettePositions.push_front(m_shotPositions[i] - m_frameDur);
  	i--;
        }
  
        // set pixmap of current shot and window title
        m_currentVignette->setPixmap(refShot());
        setWindowTitle("Annotate similar shots - Shot " + QString::number(m_refIdx + 1));
        
        update();
      }
    }
  
    /*****************************/
    /* cancelling last operation */
    /*****************************/
  
    else if (event->key() == Qt::Key_Escape && m_refIdx > 0) {
  
      // updating indices
      m_currIdx = --m_refIdx;
  
      // last operation consisted in ignoring new shot
      if (m_delOp[m_refIdx])
        m_nCamera--;
  
      // last operation consisted in linking the shot to a previous one
      else {
        // retrieving camera label to cancel
        const int camToDel(m_shotCamera[m_refIdx]);
  
        // deleting last shot annotation
        emit labelSimShot(m_shotPositions[m_refIdx], -1, Segment::Manual);
  
        // if linked with only one shot, delete its associated label
        if (m_shotCamera.count(camToDel) == 2) {
  	idxToDel = m_shotCamera.lastIndexOf(camToDel);
  	emit labelSimShot(m_shotPositions[idxToDel], -1, Segment::Manual);
        }
      }
  
      // updating vignette position list
      toAddIdx = m_currIdx - m_nVignettes + 1;
  
      if (toAddIdx < 0)
        m_vignettePositions.push_front(-1);
      else
        m_vignettePositions.push_front(m_shotPositions[toAddIdx] - m_frameDur);
  
      m_vignettePositions.pop_back();
  
      // set pixmap of current shot and window title
      m_currentVignette->setPixmap(refShot());
      setWindowTitle("Annotate similar shots - Shot " + QString::number(m_refIdx + 1));
  
      update();
    }
  
    // updating corresponding images
    m_vignetteWidget->updateVignette(m_vignettePositions);
    m_vignetteWidget->update();
  }
  
  ///////////////////////
  // auxiliary methods //
  ///////////////////////
  
  QPixmap EditSimShotDialog::refShot()
  {
    Mat frame;
  
    // retrieving selected frame
    m_cap.set(CV_CAP_PROP_POS_MSEC, m_shotPositions[m_refIdx]);
    m_cap >> frame;
    cv::resize(frame, frame, Size(m_vignetteWidth, m_vignetteHeight));
  
    return QPixmap::fromImage(Convert::fromBGRMatToQImage(frame));
  }