Commit ddeedfa6 authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle

improve speech to text config

parent e1ef25fb
Pipeline #52829 passed with stage
in 10 minutes and 16 seconds
......@@ -772,6 +772,7 @@ void KdenliveSettingsDialog::showPage(int page, int option)
break;
case 8:
setCurrentPage(m_page11);
checkVoskDependencies();
break;
default:
setCurrentPage(m_page1);
......@@ -1822,6 +1823,9 @@ void KdenliveSettingsDialog::initSpeechPage()
void KdenliveSettingsDialog::checkVoskDependencies()
{
QString pyExec = QStandardPaths::findExecutable(QStringLiteral("python3"));
if (pyExec.isEmpty()) {
pyExec = QStandardPaths::findExecutable(QStringLiteral("python"));
}
if (pyExec.isEmpty()) {
m_configSpeech.speech_info->setMessageType(KMessageWidget::Warning);
m_configSpeech.speech_info->setText(i18n("Cannot find python3, please install it on your system."));
......@@ -1859,13 +1863,29 @@ void KdenliveSettingsDialog::checkVoskDependencies()
m_configSpeech.speech_info->addAction(m_voskAction);
m_configSpeech.speech_info->show();
} else {
m_configSpeech.speech_info->removeAction(m_voskAction);
m_configSpeech.speech_info->animatedHide();
if (m_configSpeech.listWidget->count() == 0) {
m_configSpeech.speech_info->setMessageType(KMessageWidget::Information);
m_configSpeech.speech_info->setText(i18n("Please add a speech model."));
m_configSpeech.speech_info->animatedShow();
} else {
m_configSpeech.speech_info->removeAction(m_voskAction);
m_configSpeech.speech_info->setMessageType(KMessageWidget::Positive);
m_configSpeech.speech_info->setText(i18n("Speech to text is properly configured."));
m_configSpeech.speech_info->show();
}
}
pCore->updateVoskAvailability();
}
} else {
m_configSpeech.speech_info->animatedHide();
if (m_configSpeech.listWidget->count() == 0) {
m_configSpeech.speech_info->setMessageType(KMessageWidget::Information);
m_configSpeech.speech_info->setText(i18n("Please add a speech model."));
m_configSpeech.speech_info->animatedShow();
} else {
m_configSpeech.speech_info->setMessageType(KMessageWidget::Positive);
m_configSpeech.speech_info->setText(i18n("Speech to text is properly configured."));
m_configSpeech.speech_info->show();
}
}
}
......@@ -2019,6 +2039,10 @@ void KdenliveSettingsDialog::slotParseVoskDictionaries()
}
if (!final.isEmpty() && KdenliveSettings::vosk_found() && KdenliveSettings::vosk_srt_found()) {
m_configSpeech.speech_info->animatedHide();
} else if (final.isEmpty()) {
m_configSpeech.speech_info->setMessageType(KMessageWidget::Information);
m_configSpeech.speech_info->setText(i18n("Please add a speech model."));
m_configSpeech.speech_info->animatedShow();
}
pCore->voskModelUpdate(final);
}
......
......@@ -562,13 +562,13 @@ void VideoTextEdit::mouseMoveEvent(QMouseEvent *e)
TextBasedEdit::TextBasedEdit(QWidget *parent)
: QWidget(parent)
, m_clipDuration(0.)
, m_currentMessageAction(nullptr)
{
setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
setupUi(this);
setFocusPolicy(Qt::StrongFocus);
vosk_config->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
vosk_config->setToolTip(i18n("Configure speech recognition"));
connect(vosk_config, &QToolButton::clicked, [this]() {
m_voskConfig = new QAction(i18n("Configure"), this);
connect(m_voskConfig, &QAction::triggered, [this]() {
pCore->window()->slotPreferences(8);
});
......@@ -600,18 +600,12 @@ TextBasedEdit::TextBasedEdit(QWidget *parent)
m_speechJob->kill();
}
});
connect(pCore.get(), &Core::updateVoskAvailability, this, &TextBasedEdit::updateAvailability);
connect(pCore.get(), &Core::voskModelUpdate, [&](QStringList models) {
language_box->clear();
language_box->addItems(models);
updateAvailability();
if (models.isEmpty()) {
showMessage(i18n("Please install speech recognition models"), KMessageWidget::Information);
vosk_config->setVisible(true);
showMessage(i18n("Please install speech recognition models"), KMessageWidget::Information, m_voskConfig);
} else {
if (KdenliveSettings::vosk_found()) {
vosk_config->setVisible(false);
}
if (!KdenliveSettings::vosk_text_model().isEmpty() && models.contains(KdenliveSettings::vosk_text_model())) {
int ix = language_box->findText(KdenliveSettings::vosk_text_model());
if (ix > -1) {
......@@ -730,6 +724,14 @@ TextBasedEdit::TextBasedEdit(QWidget *parent)
parseVoskDictionaries();
}
TextBasedEdit::~TextBasedEdit()
{
if (m_speechJob && m_speechJob->state() == QProcess::Running) {
m_speechJob->kill();
m_speechJob->waitForFinished();
}
}
bool TextBasedEdit::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
......@@ -758,16 +760,20 @@ void TextBasedEdit::startRecognition()
m_visualEditor->cleanup();
//m_visualEditor->insertHtml(QStringLiteral("<body>"));
info_message->removeAction(m_logAction);
QString pyExec = QStandardPaths::findExecutable(QStringLiteral("python3"));
if (pyExec.isEmpty()) {
showMessage(i18n("Cannot find python3, please install it on your system."), KMessageWidget::Warning);
return;
}
if (!KdenliveSettings::vosk_found()) {
showMessage(i18n("Please configure speech to text."), KMessageWidget::Warning, m_voskConfig);
return;
}
// Start python script
QString language = language_box->currentText();
if (language.isEmpty()) {
showMessage(i18n("Please install a language model."), KMessageWidget::Warning);
showMessage(i18n("Please install a language model."), KMessageWidget::Warning, m_voskConfig);
return;
}
QString speechScript = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("scripts/speechtotext.py"));
......@@ -852,25 +858,12 @@ void TextBasedEdit::startRecognition()
frame_progress->setVisible(true);
}
void TextBasedEdit::updateAvailability()
{
bool enabled = KdenliveSettings::vosk_found() && language_box->count() > 0;
button_start->setEnabled(enabled);
vosk_config->setVisible(!enabled);
}
void TextBasedEdit::slotProcessSpeechStatus(int, QProcess::ExitStatus status)
{
if (status == QProcess::CrashExit) {
if (!m_errorString.isEmpty()) {
info_message->addAction(m_logAction);
}
showMessage(i18n("Speech recognition aborted."), KMessageWidget::Warning);
showMessage(i18n("Speech recognition aborted."), KMessageWidget::Warning, m_errorString.isEmpty() ? nullptr : m_logAction);
} else if (m_visualEditor->toPlainText().isEmpty()) {
if (!m_errorString.isEmpty()) {
info_message->addAction(m_logAction);
}
showMessage(i18n("No speech detected."), KMessageWidget::Information);
showMessage(i18n("No speech detected."), KMessageWidget::Information, m_errorString.isEmpty() ? nullptr : m_logAction);
} else {
button_add->setEnabled(true);
showMessage(i18n("Speech recognition finished."), KMessageWidget::Positive);
......@@ -1090,15 +1083,26 @@ void TextBasedEdit::previewPlaylist(bool createNew)
}
}
void TextBasedEdit::showMessage(const QString &text, KMessageWidget::MessageType type)
void TextBasedEdit::showMessage(const QString &text, KMessageWidget::MessageType type, QAction *action)
{
if (m_currentMessageAction != nullptr && (action == nullptr || action != m_currentMessageAction)) {
info_message->removeAction(m_currentMessageAction);
m_currentMessageAction = action;
if (m_currentMessageAction) {
info_message->addAction(m_currentMessageAction);
}
} else if (action) {
m_currentMessageAction = action;
info_message->addAction(m_currentMessageAction);
}
if (info_message->isVisible()) {
m_hideTimer.stop();
}
info_message->setMessageType(type);
info_message->setText(text);
info_message->animatedShow();
if (type != KMessageWidget::Error) {
if (type != KMessageWidget::Error && m_currentMessageAction == nullptr) {
m_hideTimer.start();
}
}
......
......@@ -154,6 +154,7 @@ class TextBasedEdit : public QWidget, public Ui::TextBasedEdit_UI
public:
explicit TextBasedEdit(QWidget *parent = nullptr);
~TextBasedEdit() override;
void openClip(std::shared_ptr<ProjectClip>);
public slots:
......@@ -165,13 +166,12 @@ private slots:
void slotProcessSpeechError();
void parseVoskDictionaries();
void slotProcessSpeechStatus(int, QProcess::ExitStatus status);
void updateAvailability();
/** @brief insert currently selected zones to timeline */
void insertToTimeline();
/** @brief Preview current edited text in the clip monitor */
void previewPlaylist(bool createNew = true);
/** @brief Display info message */
void showMessage(const QString &text, KMessageWidget::MessageType type);
void showMessage(const QString &text, KMessageWidget::MessageType type, QAction *action = nullptr);
void addBookmark();
protected:
......@@ -188,6 +188,8 @@ private:
int m_lastPosition;
QString m_errorString;
QAction *m_logAction;
QAction *m_voskConfig;
QAction *m_currentMessageAction;
VideoTextEdit *m_visualEditor;
QTextDocument m_document;
QString m_playlist;
......
......@@ -35,29 +35,6 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="10">
<widget class="QToolButton" name="vosk_config">
<property name="text">
<string>...</string>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="6">
<widget class="QToolButton" name="button_search">
<property name="text">
<string>...</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="9">
<spacer name="horizontalSpacer_2">
<property name="orientation">
......@@ -71,11 +48,14 @@
</property>
</spacer>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="button_insert">
<item row="0" column="6">
<widget class="QToolButton" name="button_search">
<property name="text">
<string>...</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
......@@ -98,8 +78,8 @@
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QToolButton" name="button_add">
<item row="0" column="1">
<widget class="QToolButton" name="button_insert">
<property name="text">
<string>...</string>
</property>
......@@ -118,6 +98,16 @@
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QToolButton" name="button_add">
<property name="text">
<string>...</string>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
......
  • After deleting kdenliverc and restart Kdenlive, I get some install messages and now it works.

    Edited by Eugen Mohr
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment