From 62b433ab6e4c6d57ba392cfb484f6c9f5c9fc2ed Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Thu, 14 Mar 2024 18:36:22 +0100 Subject: [PATCH] Work on batch OCR cancel - thx guido-visser :) Fix #7992 --- src/ui/Forms/BatchConvert.cs | 17 ++++++++++------ src/ui/Forms/Ocr/VobSubOcr.cs | 20 +++++++++++++------ .../CommandLineConverter.cs | 12 +++++------ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/ui/Forms/BatchConvert.cs b/src/ui/Forms/BatchConvert.cs index 03a5d5720..467553b95 100644 --- a/src/ui/Forms/BatchConvert.cs +++ b/src/ui/Forms/BatchConvert.cs @@ -133,6 +133,7 @@ namespace Nikse.SubtitleEdit.Forms InitializeComponent(); UiUtil.FixFonts(this); Icon = (Icon)icon.Clone(); + DoubleBuffered = true; _cancellationTokenSource = new CancellationTokenSource(); progressBar1.Visible = false; @@ -1428,7 +1429,7 @@ namespace Nikse.SubtitleEdit.Forms item.SubItems[3].Text = LanguageSettings.Current.BatchConvert.Ocr + " " + progress; }; vobSubOcr.FileName = Path.GetFileName(fileName); - vobSubOcr.InitializeBatch(vobSubs, idx.Palette, Configuration.Settings.VobSubOcr, fileName, false, track.Language, _ocrEngine); + vobSubOcr.InitializeBatch(vobSubs, idx.Palette, Configuration.Settings.VobSubOcr, fileName, false, track.Language, _ocrEngine, _cancellationTokenSource.Token); sub = vobSubOcr.SubtitleFromOcr; } } @@ -1481,7 +1482,7 @@ namespace Nikse.SubtitleEdit.Forms item.SubItems[3].Text = LanguageSettings.Current.BatchConvert.Ocr + " " + progress; }; vobSubOcr.FileName = Path.GetFileName(fileName); - vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, false, track.Language, _ocrEngine); + vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, false, track.Language, _ocrEngine, _cancellationTokenSource.Token); sub = vobSubOcr.SubtitleFromOcr; } } @@ -1525,7 +1526,7 @@ namespace Nikse.SubtitleEdit.Forms vobSubOcr.FileName = Path.GetFileName(fileName); //TODO: fix - vobSubOcr.InitializeBatch(binaryParagraphs.Cast().ToList(), Configuration.Settings.VobSubOcr, fileName, false, track.Language, _ocrEngine); + vobSubOcr.InitializeBatch(binaryParagraphs.Cast().ToList(), Configuration.Settings.VobSubOcr, fileName, false, track.Language, _ocrEngine, _cancellationTokenSource.Token); sub = vobSubOcr.SubtitleFromOcr; } } @@ -1592,7 +1593,7 @@ namespace Nikse.SubtitleEdit.Forms } }; vobSubOcr.FileName = Path.GetFileName(fileName); - vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, false, _ocrLanguage, _ocrEngine); + vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, false, _ocrLanguage, _ocrEngine, _cancellationTokenSource.Token); sub = vobSubOcr.SubtitleFromOcr; } } @@ -1617,7 +1618,7 @@ namespace Nikse.SubtitleEdit.Forms lastProgress = progress; } }; - vobSubOcr.InitializeBatch(fileName, Configuration.Settings.VobSubOcr, false, _ocrEngine, _ocrLanguage); + vobSubOcr.InitializeBatch(fileName, Configuration.Settings.VobSubOcr, false, _ocrEngine, _ocrLanguage, _cancellationTokenSource.Token); sub = vobSubOcr.SubtitleFromOcr; } } @@ -1687,7 +1688,7 @@ namespace Nikse.SubtitleEdit.Forms var language = programMapTableParser.GetSubtitleLanguage(id); language = string.IsNullOrEmpty(language) ? _ocrLanguage : language; vobSubOcr.FileName = Path.GetFileName(fileName); - vobSubOcr.InitializeBatch(tsBinaryParagraphs, Configuration.Settings.VobSubOcr, fileName, false, language, _ocrEngine); + vobSubOcr.InitializeBatch(tsBinaryParagraphs, Configuration.Settings.VobSubOcr, fileName, false, language, _ocrEngine, _cancellationTokenSource.Token); subtitle = vobSubOcr.SubtitleFromOcr; } @@ -1852,6 +1853,10 @@ namespace Nikse.SubtitleEdit.Forms { item.SubItems[3].Text = LanguageSettings.Current.BatchConvert.FilterSkipped; } + else if (_abort) + { + item.SubItems[3].Text = "Cancelled"; + } else { if (binaryParagraphs.Count == 0) diff --git a/src/ui/Forms/Ocr/VobSubOcr.cs b/src/ui/Forms/Ocr/VobSubOcr.cs index da7d5f256..9823c69f5 100644 --- a/src/ui/Forms/Ocr/VobSubOcr.cs +++ b/src/ui/Forms/Ocr/VobSubOcr.cs @@ -24,10 +24,12 @@ using System.Net; using System.Reflection; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using MessageBox = Nikse.SubtitleEdit.Forms.SeMsgBox.MessageBox; +using Timer = System.Windows.Forms.Timer; namespace Nikse.SubtitleEdit.Forms.Ocr { @@ -263,6 +265,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr private XmlDocument _compareDoc = new XmlDocument(); private Point _manualOcrDialogPosition = new Point(-1, -1); private volatile bool _abort; + private CancellationToken _cancellationToken = CancellationToken.None; private int _selectedIndex = -1; private VobSubOcrSettings _vobSubOcrSettings; private bool _italicCheckedLast; @@ -678,8 +681,9 @@ namespace Nikse.SubtitleEdit.Forms.Ocr comboBoxDictionaries.SelectedIndexChanged += comboBoxDictionaries_SelectedIndexChanged; } - internal void InitializeBatch(string vobSubFileName, VobSubOcrSettings vobSubOcrSettings, bool forcedOnly, string ocrEngine, string language = null) + internal void InitializeBatch(string vobSubFileName, VobSubOcrSettings vobSubOcrSettings, bool forcedOnly, string ocrEngine, string language, CancellationToken cancellationToken) { + _cancellationToken = cancellationToken; Initialize(vobSubFileName, vobSubOcrSettings, null, true); FormVobSubOcr_Shown(null, null); checkBoxShowOnlyForced.Checked = forcedOnly; @@ -768,7 +772,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr _subtitle.Paragraphs[i].Text = text; Application.DoEvents(); - if (_abort) + if (_abort || _cancellationToken.IsCancellationRequested) { SetButtonsEnabledAfterOcrDone(); return; @@ -872,13 +876,15 @@ namespace Nikse.SubtitleEdit.Forms.Ocr } } - internal void InitializeBatch(IList subtitles, VobSubOcrSettings vobSubOcrSettings, string fileName, bool forcedOnly, string language = null, string ocrEngine = null) + internal void InitializeBatch(IList subtitles, VobSubOcrSettings vobSubOcrSettings, string fileName, bool forcedOnly, string language, string ocrEngine, CancellationToken cancellationToken) { if (subtitles.Count == 0) { return; } + _cancellationToken = cancellationToken; + if (subtitles.First() is TransportStreamSubtitle) { var tssList = new List(); @@ -922,13 +928,14 @@ namespace Nikse.SubtitleEdit.Forms.Ocr checkBoxNOcrDrawUnknownLetters.Checked = oldNOcrDrawText; } - internal void InitializeBatch(List subtitles, VobSubOcrSettings vobSubOcrSettings, string fileName, bool forcedOnly, string language = null, string ocrEngine = null) + internal void InitializeBatch(List subtitles, VobSubOcrSettings vobSubOcrSettings, string fileName, bool forcedOnly, string language, string ocrEngine, CancellationToken cancellationToken) { Initialize(subtitles, vobSubOcrSettings, fileName); _ocrMethodIndex = Configuration.Settings.VobSubOcr.LastOcrMethod == "Tesseract4" ? _ocrMethodTesseract5 : _ocrMethodTesseract302; var oldNOcrDrawText = checkBoxNOcrDrawUnknownLetters.Checked; InitializeOcrEngineBatch(language, ocrEngine); + _cancellationToken = cancellationToken; checkBoxShowOnlyForced.Checked = forcedOnly; DoBatch(); @@ -958,11 +965,12 @@ namespace Nikse.SubtitleEdit.Forms.Ocr } } - internal void InitializeBatch(List vobSubMergedPackList, List palette, VobSubOcrSettings vobSubOcrSettings, string fileName, bool forcedOnly, string language, string ocrEngine) + internal void InitializeBatch(List vobSubMergedPackList, List palette, VobSubOcrSettings vobSubOcrSettings, string fileName, bool forcedOnly, string language, string ocrEngine, CancellationToken cancellationToken) { Initialize(vobSubMergedPackList, palette, vobSubOcrSettings, language); checkBoxShowOnlyForced.Checked = forcedOnly; InitializeOcrEngineBatch(language, ocrEngine); + _cancellationToken = cancellationToken; DoBatch(); } @@ -1058,7 +1066,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr _subtitle.Paragraphs[i].Text = text; Application.DoEvents(); - if (_abort) + if (_abort || _cancellationToken.IsCancellationRequested) { SetButtonsEnabledAfterOcrDone(); return; diff --git a/src/ui/Logic/CommandLineConvert/CommandLineConverter.cs b/src/ui/Logic/CommandLineConvert/CommandLineConverter.cs index 1e7aee36c..d2987358a 100644 --- a/src/ui/Logic/CommandLineConvert/CommandLineConverter.cs +++ b/src/ui/Logic/CommandLineConvert/CommandLineConverter.cs @@ -575,7 +575,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert _stdOutWriter?.Write($"\r{LanguageSettings.Current.BatchConvert.Ocr} : {progress}"); }; vobSubOcr.FileName = Path.GetFileName(fileName); - vobSubOcr.InitializeBatch(vobSubs, idx.Palette, Configuration.Settings.VobSubOcr, fileName, false, lang, ocrEngine); + vobSubOcr.InitializeBatch(vobSubs, idx.Palette, Configuration.Settings.VobSubOcr, fileName, false, lang, ocrEngine, CancellationToken.None); _stdOutWriter?.WriteLine(); sub = vobSubOcr.SubtitleFromOcr; } @@ -614,7 +614,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert _stdOutWriter?.Write($"\r{LanguageSettings.Current.BatchConvert.Ocr} : {progress}"); }; vobSubOcr.FileName = Path.GetFileName(fileName); - vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, false, lang, ocrEngine); + vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, false, lang, ocrEngine, CancellationToken.None); _stdOutWriter?.WriteLine(); sub = vobSubOcr.SubtitleFromOcr; } @@ -652,7 +652,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert _stdOutWriter?.Write($"\r{LanguageSettings.Current.BatchConvert.Ocr} : {progress}"); }; vobSubOcr.FileName = Path.GetFileName(fileName); - vobSubOcr.InitializeBatch(binaryParagraphs.Cast().ToList(), Configuration.Settings.VobSubOcr, fileName, false, lang, ocrEngine); + vobSubOcr.InitializeBatch(binaryParagraphs.Cast().ToList(), Configuration.Settings.VobSubOcr, fileName, false, lang, ocrEngine, CancellationToken.None); _stdOutWriter?.WriteLine(); sub = vobSubOcr.SubtitleFromOcr; } @@ -988,7 +988,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert _stdOutWriter?.Write($"\r{LanguageSettings.Current.BatchConvert.Ocr} : {progress}"); }; vobSubOcr.FileName = Path.GetFileName(fileName); - vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, forcedOnly, ocrDb, ocrEngine); + vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, forcedOnly, ocrDb, ocrEngine, CancellationToken.None); _stdOutWriter?.WriteLine(); sub = vobSubOcr.SubtitleFromOcr; _stdOutWriter?.WriteLine($"Extracted subtitles from file \"{fileName}\""); @@ -1014,7 +1014,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert { _stdOutWriter?.Write($"\r{LanguageSettings.Current.BatchConvert.Ocr} : {progress}"); }; - vobSubOcr.InitializeBatch(fileName, Configuration.Settings.VobSubOcr, forcedOnly, ocrEngine, ocrDb); + vobSubOcr.InitializeBatch(fileName, Configuration.Settings.VobSubOcr, forcedOnly, ocrEngine, ocrDb, CancellationToken.None); _stdOutWriter?.WriteLine(); sub = vobSubOcr.SubtitleFromOcr; _stdOutWriter?.WriteLine($"Extracted subtitles from file \"{fileName}\""); @@ -1423,7 +1423,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert _stdOutWriter?.Write($"\r{LanguageSettings.Current.BatchConvert.Ocr} : {progress}"); }; vobSubOcr.FileName = Path.GetFileName(fileName); - vobSubOcr.InitializeBatch(binaryParagraphs.Cast().ToList(), Configuration.Settings.VobSubOcr, fileName, false, null, ocrEngine); + vobSubOcr.InitializeBatch(binaryParagraphs.Cast().ToList(), Configuration.Settings.VobSubOcr, fileName, false, null, ocrEngine, CancellationToken.None); _stdOutWriter?.WriteLine(); sub = vobSubOcr.SubtitleFromOcr; _stdOutWriter?.WriteLine($"Extracted subtitles from file \"{fileName}\"");