Blame view
SegmentMonitor.cpp
19.8 KB
3f2992b2c
|
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 |
#include <QPainter> #include <QMouseEvent> #include <QDebug> #include <QAction> #include <QFont> #include <QGuiApplication> #include <QInputDialog> #include <QMessageBox> #include <QFileDialog> #include <QRegularExpression> #include <cmath> #include <string> #include <regex> #include <iostream> #include "SegmentMonitor.h" SegmentMonitor::SegmentMonitor(QWidget *parent) : QWidget(parent), m_step(40), m_segmentHeight(4), m_duration(1), m_idxColor(0), m_annotMode(false), m_prevPosition(-1) { resize(75000, 140); // contextual menu containing speaker as manually annotated QFont boldFont = QGuiApplication::font(); boldFont.setBold(true); m_newSpeakerAct = new QAction(tr("Add new speaker"), this); m_endAct = new QAction(tr("End"), this); m_endAct->setFont(boldFont); m_newSpeakerAct->setFont(boldFont); setContextMenuPolicy(Qt::CustomContextMenu); m_speakers = new QMenu; m_speakers->addAction(m_newSpeakerAct); m_speakers->addAction(m_endAct); m_speakers->addSeparator(); m_colors << QColor(219, 213, 185) << QColor(224, 199, 30); m_colors << QColor(186, 22, 63) << QColor(255, 250, 129); m_colors << QColor(181, 225, 174) << QColor(154, 206, 223); m_colors << QColor(249, 149, 51) << QColor(252, 169, 133); m_colors << QColor(193, 179, 215) << QColor(116, 161, 97); m_colors << QColor(133, 202, 93) << QColor(72, 181, 163); m_colors << QColor(69, 114, 147) << QColor(249, 150, 182); m_colors << QColor(117, 137, 191) << QColor(84, 102, 84); reset(); connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&))); connect(m_newSpeakerAct, SIGNAL(triggered()), this, SLOT(addNewSpeaker())); connect(m_endAct, SIGNAL(triggered()), this, SLOT(endOfAnnotation())); } void SegmentMonitor::reset() { for (int i(0); i < m_segmentationList.size(); i++) m_segmentationList[i].clear(); m_segmentationList.clear(); m_currList.clear(); m_lastStart = 0; m_currPosition = 0; m_idxColor = 0; m_currText = QString(); m_currSpeaker = QString(); m_segColors.insert(QString(), QColor(255, 255, 255)); m_segColors.insert("S", QColor(255, 255, 255)); } /////////////// // modifiers // /////////////// void SegmentMonitor::setAnnotMode(bool annotMode) { m_annotMode = annotMode; if (annotMode) m_segmentHeight = 8; else m_segmentHeight = 4; } /////////////// // accessors // /////////////// qreal SegmentMonitor::getRatio() const { return m_ratio; } /////////// // slots // /////////// void SegmentMonitor::processShot(Segment *shot) { m_currList.push_back(shot); QString camera = shot->getLabel(); if (!camera.isEmpty() && !m_segColors.contains(camera)) m_segColors.insert(camera, m_colors[m_idxColor++ % m_colors.size()]); } void SegmentMonitor::processSpokenFrame(qint64 position, const QString &text, const QString &speaker) { // regular expression to detect subtitles corresponding to noise QRegularExpression noiseSource("\\(.*\\)"); QRegularExpressionMatch match = noiseSource.match(text); if (position != 0 && !match.hasMatch()) { // possible boundary if (m_currSpeaker != speaker || m_currText != text) { // end of speech segment if (!m_currSpeaker.isEmpty()) { SpeechSegment *segment = new SpeechSegment(m_lastStart, m_currPosition, m_currText, m_currSpeaker); m_currList.push_back(segment); if (!m_segColors.contains(m_currSpeaker)) m_segColors.insert(m_currSpeaker, m_colors[m_idxColor++ % m_colors.size()]); } // beginning of new segment if (!speaker.isEmpty()) m_lastStart = position; } // updating data m_currPosition = position; m_currText = text; m_currSpeaker = speaker; } } void SegmentMonitor::segmentationRetrieved() { // retrieving speakers as annotated if (m_segmentationList.size() == 1) { QString currSpeaker; for (int i(0); i < m_currList.size(); i++) if (!m_annotSpeakers.contains((currSpeaker = m_currList[i]->getLabel())) && currSpeaker != "S") { m_annotSpeakers.push_back(currSpeaker); m_speakers->addAction(new QAction(currSpeaker, this)); } } m_segmentationList.push_back(m_currList); m_selectedLabels.push_back(QString()); m_currList.clear(); m_step = height() / (m_segmentationList.size() + 1); } void SegmentMonitor::positionChanged(qint64 position) { m_currPosition = position; update(); } void SegmentMonitor::updateDuration(qint64 duration) { m_duration = duration; } void SegmentMonitor::setWidth(int newWidth) { m_width = newWidth; resize(m_width, height()); m_ratio = static_cast<qreal>(m_width) / m_duration; update(); } void SegmentMonitor::paintEvent(QPaintEvent *event) { Q_UNUSED(event); int x1, x2; int y1, y2; QString segLabel; QPainter painter(this); QColor color; QFont font; font.setPointSize(m_segmentHeight * 2); painter.setFont(font); // filling background painter.fillRect(QRect(0, 0, m_width, height()), QBrush(Qt::white)); // draw current position painter.setPen(Qt::gray); painter.drawLine(m_currPosition * m_ratio, 0, m_currPosition * m_ratio, height()); // drawing segments for (int i(0); i < m_segmentationList.size(); i++) { y1 = m_step * (i + 1) - m_segmentHeight / 2; y2 = y1 + m_segmentHeight; // surrounding each segmentation painter.setPen(Qt::lightGray); painter.drawRect(0, y1, m_width, m_segmentHeight); painter.setPen(Qt::black); for (int j(0); j < m_segmentationList[i].size(); j++) { segLabel = m_segmentationList.at(i).at(j)->getLabel(); color = m_segColors[segLabel]; if (m_selectedLabels[i].isEmpty() || m_selectedLabels[i] == segLabel) { x1 = qRound(m_segmentationList.at(i).at(j)->getPosition() * m_ratio); // case of shot segmentation if (i == 0) { // last shot if (j == m_segmentationList[i].size() - 1) x2 = qRound(m_duration * m_ratio); else x2 = qRound(m_segmentationList.at(i).at(j + 1)->getPosition() * m_ratio); } // case of speech segmentation else x2 = qRound(m_segmentationList.at(i).at(j)->getEnd() * m_ratio); if (m_selectedLabels[i] == segLabel) { if (segLabel == "S") segLabel = QString::number(j + 1); QRectF rectF = painter.boundingRect(QRectF(x1, y2 + m_segmentHeight, x2 - x1, m_segmentHeight * 4), Qt::AlignHCenter, segLabel); if (rectF.width() <= x2 - x1) painter.drawText(rectF, Qt::AlignHCenter, segLabel); } QPointF p1(x1, y1); QPointF p2(x2, y2); painter.fillRect(QRectF(p1, p2), color); painter.drawRect(QRectF(p1, p2)); } } } } void SegmentMonitor::mouseDoubleClickEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { bool inBound(false); qreal inf, sup; int start(-1), end(-1); bool found(false); int min, max; qreal position(event->x() / m_ratio); // test if clicked in segmentation regions int i(0); int j(0); while (i < m_segmentationList.size() && !inBound) { inf = m_step * (i + 1); sup = inf + m_segmentHeight; if (event->y() >= inf && event->y() <= sup) inBound = true; i++; } // binary search to retrieve index of segment clicked segment if (inBound) { QList<Segment *> segList(m_segmentationList[--i]); min = 0; max = segList.size() - 1; while (min <= max && !found) { j = (min + max) / 2; start = segList[j]->getPosition(); // shot segmentation activated if (i == 0) { // last shot if (j == segList.size() - 1) end = m_duration; else end = segList[j+1]->getPosition(); } // speech segmentation activated else end = segList[j]->getEnd(); if (position >= start && position <= end) found = true; else if (position < start) max = j - 1; else min = j + 1; } if (found && (m_selectedLabels[i].isEmpty() || m_selectedLabels[i] == segList[j]->getLabel())) { QList<QPair<qint64, qint64>> utterances; utterances.push_back(QPair<qint64, qint64>(start, end)); emit playSegments(utterances); } } } } void SegmentMonitor::mousePressEvent(QMouseEvent *event) { bool inBound(false); qreal inf, sup; int start(-1), end(-1); bool found(false); int min, max; qreal position(event->x() / m_ratio); // test if clicked in segmentation regions int i(0); int j(0); while (i < m_segmentationList.size() && !inBound) { inf = m_step * (i + 1); sup = inf + m_segmentHeight; if (event->y() >= inf && event->y() <= sup) inBound = true; i++; } // binary search to retrieve index of segment clicked segment if (inBound) { QList<Segment *> segList(m_segmentationList[--i]); min = 0; min = 0; max = segList.size() - 1; while (min <= max && !found) { j = (min + max) / 2; start = segList[j]->getPosition(); // shot segmentation activated if (i == 0) { // last shot if (j == segList.size() - 1) end = m_duration; else end = segList[j+1]->getPosition(); } // speech segmentation activated else end = segList[j]->getEnd(); if (position >= start && position <= end) found = true; else if (position < start) max = j - 1; else min = j + 1; } if (found && !m_annotMode && event->button() == Qt::RightButton) { if (m_selectedLabels[i].isEmpty()) m_selectedLabels[i] = segList[j]->getLabel(); else if (m_selectedLabels[i] == segList[j]->getLabel()) m_selectedLabels[i] = ""; update(); } } } void SegmentMonitor::mouseMoveEvent(QMouseEvent *event) { bool inBound(false); bool firstHalf(false); qreal inf, sup; int start(-1), end(-1); bool found(false); int min, max; qreal position(event->x() / m_ratio); // test if clicked in segmentation regions int i(0); int j(0); while (i < m_segmentationList.size() && !inBound) { inf = m_step * (i + 1); sup = inf + m_segmentHeight; if (event->y() >= inf && event->y() <= sup) inBound = true; i++; } // binary search to retrieve index of clicked segment if (i == 2 && inBound) { QList<Segment *> segList(m_segmentationList[--i]); min = 0; min = 0; max = segList.size() - 1; while (min <= max && !found) { j = (min + max) / 2; start = segList[j]->getPosition(); // shot segmentation activated if (i == 0) { // last shot if (j == segList.size() - 1) end = m_duration; else end = segList[j+1]->getPosition(); } // speech segmentation activated else end = segList[j]->getEnd(); if (position >= start && position <= end) found = true; else if (position < start) max = j - 1; else min = j + 1; } // reference speech segment clicked for annotation purpose if (found && m_annotMode) { if (m_prevPosition != -1) { qint64 start, end, mid, position, newPosition, diff; start = segList[j]->getPosition(); end = segList[j]->getEnd(); mid = (start + end) / 2; // normalized mouse position in milliseconds from the beginning position = event->x() / m_ratio; if (m_prevPosition != -1) diff = position - m_prevPosition; else diff = 0; // modifying beginning of segment if (position >= start && position < mid) { firstHalf = true; if (j > 0 && start + diff <= segList[j-1]->getEnd()) newPosition = start; else newPosition = start + diff; newPosition = static_cast<qint64>(ceil(newPosition / 40.0)) * 40; if (end - newPosition >= 240) { segList[j]->setPosition(newPosition); emit resetSpeaker(start, end, newPosition, end, true, VideoFrame::Ref); } } // modifying end of segment else if (position >= mid && position <= end) { firstHalf = false; if (j < segList.size() - 1 && end + diff >= segList[j+1]->getPosition()) newPosition = end; else newPosition = end + diff; newPosition = static_cast<qint64>(floor(newPosition / 40.0)) * 40; if (newPosition - start >= 240) { segList[j]->setEnd(newPosition); emit resetSpeaker(start, end, start, newPosition, true, VideoFrame::Ref); } } update(); } } // updating normalized mouse position if (firstHalf) m_prevPosition = static_cast<qint64>(ceil(position / 40.0)) * 40; else m_prevPosition = static_cast<qint64>(floor(position / 40.0)) * 40; } } void SegmentMonitor::mouseReleaseEvent(QMouseEvent *event) { Q_UNUSED(event); m_prevPosition = -1; } void SegmentMonitor::showContextMenu(const QPoint &point) { // retrieving selected speech segment bool inBound(false); qreal inf, sup; int start(-1), end(-1); bool found(false); int min, max; qreal position(point.x() / m_ratio); // test if clicked in segmentation regions int i(0); int j(0); while (i < m_segmentationList.size() && !inBound) { inf = m_step * (i + 1); sup = inf + m_segmentHeight; if (point.y() >= inf && point.y() <= sup) inBound = true; i++; } // binary search to retrieve index of segment clicked segment if (i == 2 && inBound) { QList<Segment *> segList(m_segmentationList[--i]); min = 0; max = segList.size() - 1; while (min <= max && !found) { j = (min + max) / 2; start = segList[j]->getPosition(); // shot segmentation activated if (i == 0) { // last shot if (j == segList.size() - 1) end = m_duration; else end = segList[j+1]->getPosition(); } // speech segmentation activated else end = segList[j]->getEnd(); if (position >= start && position <= end) found = true; else if (position < start) max = j - 1; else min = j + 1; } if (found && m_annotMode) { QPoint globalPos = mapToGlobal(point); QAction *selected = m_speakers->exec(globalPos); if (selected && selected != m_newSpeakerAct && selected != m_endAct) { emit setSpeaker(segList[j]->getPosition(), segList[j]->getEnd(), selected->text(), VideoFrame::Ref); segList[j]->setLabel(selected->text()); update(); } } } } void SegmentMonitor::addNewSpeaker() { bool ok(false); QString speaker = QInputDialog::getText(this, "New speaker", "New speaker name:", QLineEdit::Normal, QString(), &ok); if (ok && !speaker.isEmpty()) { m_speakers->addAction(new QAction(speaker, this)); m_segColors.insert(speaker, m_colors[m_idxColor++ % m_colors.size()]); } } /////////////////////// // auxiliary methods // /////////////////////// void SegmentMonitor::endOfAnnotation() { QList<Segment *> shotSegList = m_segmentationList[0]; QList<Segment *> spkSegList = m_segmentationList[1]; int ans; /*********************************/ /* I: merging adjacent segments? */ /*********************************/ ans = QMessageBox::question(this, tr("End of annotation"), tr("End of annotation. Merge adjacent segments?"), QMessageBox::Yes | QMessageBox::No); if (ans == QMessageBox::Yes) { QString prevSpeaker, currSpeaker; int prevStart, prevEnd, start, end; // merging close speech segments coming from same speaker for (int i(1); i < spkSegList.size(); i++) { prevSpeaker = spkSegList[i-1]->getLabel(); prevStart = spkSegList[i-1]->getPosition(); prevEnd = spkSegList[i-1]->getEnd(); currSpeaker = spkSegList[i]->getLabel(); start = spkSegList[i]->getPosition(); end = spkSegList[i]->getEnd(); if (currSpeaker == prevSpeaker && start - prevEnd <= 500) emit resetSpeaker(start, end, prevStart, end, false, VideoFrame::Ref); } } /*********************************************/ /* II: exporting file containing utterances? */ /*********************************************/ ans = QMessageBox::question(this, tr("End of annotation"), tr("End of annotation. Export file containing labeled speech segments?"), QMessageBox::Yes | QMessageBox::No); if (ans == QMessageBox::Yes) { QString fName = QFileDialog::getSaveFileName(this, tr("Save File"), tr("tools/spkDiarization/lblAcousticSegmentation"), tr("Label (*.lbl)")); if (!fName.isEmpty()) { QFile datFile(fName + ".dat"); if (!datFile.open(QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream lblOut(&datFile); qreal start(-1.0), end(-1.0); QString label; // test for estimation of speech segments based on subtitles bool sub(true); int i(0); while (i < spkSegList.size() && sub) if (spkSegList[i++]->getLabel() != "S") sub = false; if (sub) { for (int i(0); i < spkSegList.size(); i++) { start = spkSegList[i]->getPosition() / 1000.0; end = spkSegList[i]->getEnd() / 1000.0; lblOut << start << " " << end << " " << "speech" << " "; } } else { qreal beginning(-1.0); for (int i(0); i < spkSegList.size(); i++) { label = spkSegList[i]->getLabel(); label.replace(QRegularExpression(" "), "_"); if (label != "S") { start = spkSegList[i]->getPosition() / 1000.0; end = spkSegList[i]->getEnd() / 1000.0; lblOut << start << " " << end << " " << label << " "; if (beginning == -1) beginning = start; } } QFile uemFile(fName + ".uem"); QRegularExpression re(".*/(.+_.+)"); QRegularExpressionMatch match = re.match(fName); QString baseName; if (match.hasMatch()) baseName = match.captured(1); if (!uemFile.open(QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream uemOut(&uemFile); uemOut << baseName << " " << 1 << " " << beginning << " " << end << " "; } } } /****************************************************************************/ /* III: exporting file containing utterances included in alternating shots? */ /****************************************************************************/ ans = QMessageBox::question(this, tr("End of annotation"), tr("End of annotation. Export file containing utterances included in alternating shots?"), QMessageBox::Yes | QMessageBox::No); if (ans == QMessageBox::Yes) { QString fName = QFileDialog::getSaveFileName(this, tr("Save File"), tr("tools/spkDiarization/shotPatterns"), tr("CSV (*.csv)")); if (!fName.isEmpty()) { QFile csvFile(fName + ".csv"); QList<QString> lblWindow; // last four shot labels: used to detect shot pattern if (!csvFile.open(QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream csvOut(&csvFile); QString label; bool inPattern(false); // indicates if currently visiting a pattern int j(0); // looping over the shot positions for (int i(0); i < shotSegList.size() - 1; i++) { label = shotSegList[i]->getLabel(); // current shot label // updating shot label window lblWindow.push_back(label); if (lblWindow.size() > 4) lblWindow.pop_front(); // new pattern detected if (!inPattern && testShotPattern(lblWindow)) { // setting spoken segment position while (j > 0 && spkSegList[j]->getPosition() > shotSegList[i-3]->getPosition()) j--; while (j < spkSegList.size() && spkSegList[j]->getEnd() < shotSegList[i-3]->getPosition()) j++; // writing out labels of alternating labels csvOut << lblWindow[0] << "_" << lblWindow[1] << " "; inPattern = true; } // end of pattern if (inPattern && !testShotPattern(lblWindow)) { // writing out new line csvOut << endl; inPattern = false; } // moving into current pattern if (inPattern && testShotPattern(lblWindow)) // writing out positions of spoken segments covered by the pattern while (j < spkSegList.size() && spkSegList[j]->getPosition() < shotSegList[i+1]->getPosition()) { csvOut << (j + 1) << " "; j++; } // updating flag inPattern = testShotPattern(lblWindow); } } } } bool SegmentMonitor::testShotPattern(const QList<QString> &lblWindow) { if (!lblWindow.contains("") && lblWindow.size() == 4) return lblWindow[0] == lblWindow[2] && lblWindow[1] == lblWindow[3] && lblWindow[0] != lblWindow[1]; return false; } |