Now runs Tesseract in separate thread

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@745 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2011-10-25 16:43:37 +00:00
parent 16c4ab8908
commit 8212588bde
3 changed files with 291 additions and 236 deletions

View File

@ -66,7 +66,7 @@ namespace Nikse.SubtitleEdit.Forms
this.checkBoxUseModiInTesseractForUnknownWords = new System.Windows.Forms.CheckBox();
this.labelTesseractLanguage = new System.Windows.Forms.Label();
this.comboBoxTesseractLanguages = new System.Windows.Forms.ComboBox();
this.textBoxCurrentText = new SETextBox();
this.textBoxCurrentText = new Nikse.SubtitleEdit.Controls.SETextBox();
this.groupBoxOCRControls = new System.Windows.Forms.GroupBox();
this.labelStartFrom = new System.Windows.Forms.Label();
this.numericUpDownStartNumber = new System.Windows.Forms.NumericUpDown();
@ -288,10 +288,10 @@ namespace Nikse.SubtitleEdit.Forms
//
// groupBoxOcrMethod
//
this.groupBoxOcrMethod.Controls.Add(this.groupBoxImageCompareMethod);
this.groupBoxOcrMethod.Controls.Add(this.groupBoxModiMethod);
this.groupBoxOcrMethod.Controls.Add(this.comboBoxOcrMethod);
this.groupBoxOcrMethod.Controls.Add(this.GroupBoxTesseractMethod);
this.groupBoxOcrMethod.Controls.Add(this.groupBoxImageCompareMethod);
this.groupBoxOcrMethod.Controls.Add(this.groupBoxModiMethod);
this.groupBoxOcrMethod.Location = new System.Drawing.Point(13, 5);
this.groupBoxOcrMethod.Name = "groupBoxOcrMethod";
this.groupBoxOcrMethod.Size = new System.Drawing.Size(392, 192);
@ -485,6 +485,7 @@ namespace Nikse.SubtitleEdit.Forms
//
// textBoxCurrentText
//
this.textBoxCurrentText.AllowDrop = true;
this.textBoxCurrentText.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBoxCurrentText.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
@ -978,6 +979,7 @@ namespace Nikse.SubtitleEdit.Forms
this.Name = "VobSubOcr";
this.ShowIcon = false;
this.Text = "Import/OCR VobSub (sub/idx) subtitle";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.VobSubOcr_FormClosing);
this.Shown += new System.EventHandler(this.FormVobSubOcr_Shown);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.VobSubOcr_KeyDown);
this.Resize += new System.EventHandler(this.VobSubOcr_Resize);

View File

@ -148,6 +148,10 @@ namespace Nikse.SubtitleEdit.Forms
Keys _italicShortcut = Utilities.GetKeys(Configuration.Settings.Shortcuts.MainTextBoxItalic);
private string[] _tesseractAsyncStrings = null;
private int _tesseractAsyncIndex = 0;
private BackgroundWorker _tesseractThread;
public VobSubOcr()
{
InitializeComponent();
@ -957,6 +961,9 @@ namespace Nikse.SubtitleEdit.Forms
// Search images with minor location changes
FindBestMatch(ref index, ref smallestDifference, ref smallestIndex, target);
if (target.Height < 35)
{
if (smallestDifference > 0 && target.Width > 12)
{
Bitmap cutBitmap = CopyBitmapSection(target, new Rectangle(1, 0, target.Width - 2, target.Height));
@ -991,7 +998,6 @@ namespace Nikse.SubtitleEdit.Forms
cutBitmap.Dispose();
}
if (smallestDifference > 0 && target.Width > 15)
{
Bitmap cutBitmap = CopyBitmapSection(target, new Rectangle(1, 0, target.Width - 2, target.Height));
@ -1001,6 +1007,7 @@ namespace Nikse.SubtitleEdit.Forms
FindBestMatch(ref index, ref smallestDifference, ref smallestIndex, cutBitmap);
cutBitmap.Dispose();
}
}
if (smallestIndex >= 0)
{
@ -1777,6 +1784,26 @@ namespace Nikse.SubtitleEdit.Forms
progressBar1.Visible = false;
}
void TesseractThreadDoWork(object sender, DoWorkEventArgs e)
{
var bitmap = (Bitmap)e.Argument;
if (_tesseractAsyncIndex >= 0 && _tesseractAsyncIndex < _tesseractAsyncStrings.Length)
{
if (string.IsNullOrEmpty(_tesseractAsyncStrings[_tesseractAsyncIndex]))
_tesseractAsyncStrings[_tesseractAsyncIndex] = Tesseract3DoOcrViaExe(bitmap, _languageId);
}
}
void TesseractThreadRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (!e.Cancelled)
{
_tesseractAsyncIndex++;
if (_tesseractAsyncIndex >= 0 && _tesseractAsyncIndex < _tesseractAsyncStrings.Length)
_tesseractThread.RunWorkerAsync(GetSubtitleBitmap(_tesseractAsyncIndex));
}
}
private void ButtonStartOcrClick(object sender, EventArgs e)
{
Configuration.Settings.VobSubOcr.RightToLeft = checkBoxRightToLeft.Checked;
@ -1792,6 +1819,17 @@ namespace Nikse.SubtitleEdit.Forms
int max = GetSubtitleCount();
if (comboBoxOcrMethod.SelectedIndex == 0 && _tesseractAsyncStrings == null)
{
_tesseractAsyncStrings = new string[max];
_tesseractAsyncIndex = (int)numericUpDownStartNumber.Value + 5;
_tesseractThread = new BackgroundWorker();
_tesseractThread.DoWork += TesseractThreadDoWork;
_tesseractThread.RunWorkerCompleted += TesseractThreadRunWorkerCompleted;
_tesseractThread.WorkerSupportsCancellation = true;
_tesseractThread.RunWorkerAsync(GetSubtitleBitmap(_tesseractAsyncIndex));
}
progressBar1.Maximum = max;
progressBar1.Value = 0;
progressBar1.Visible = true;
@ -1944,7 +1982,17 @@ namespace Nikse.SubtitleEdit.Forms
LoadOcrFixEngine();
int badWords = 0;
string textWithOutFixes = Tesseract3DoOcrViaExe(bitmap, _languageId);
string textWithOutFixes;
if (!string.IsNullOrEmpty(_tesseractAsyncStrings[index]))
{
textWithOutFixes = _tesseractAsyncStrings[index];
}
else
{
if (_tesseractAsyncIndex <= index || _tesseractAsyncIndex > index + 50)
_tesseractAsyncIndex = index + 10;
textWithOutFixes = Tesseract3DoOcrViaExe(bitmap, _languageId);
}
if (textWithOutFixes.ToString().Trim().Length == 0)
textWithOutFixes = TesseractResizeAndRetry(bitmap);
@ -2734,7 +2782,7 @@ namespace Nikse.SubtitleEdit.Forms
string name = comboBoxDictionaries.SelectedItem.ToString();
int start = name.LastIndexOf("[");
int end = name.LastIndexOf("]");
if (start > 0 && end > start)
if (start >= 0 && end > start)
{
start++;
name = name.Substring(start, end - start);
@ -2984,7 +3032,6 @@ namespace Nikse.SubtitleEdit.Forms
SubtitleListView1SelectedIndexChanged(null, null);
}
internal void Initialize(string fileName, List<Color> palette, VobSubOcrSettings vobSubOcrSettings, List<SpHeader> spList)
{
_spList = spList;
@ -3079,7 +3126,6 @@ namespace Nikse.SubtitleEdit.Forms
LoadImageCompareCharacterDatabaseList();
//_palette = palette;
if (_palette == null)
checkBoxCustomFourColors.Checked = true;
@ -3101,5 +3147,12 @@ namespace Nikse.SubtitleEdit.Forms
subtitleListView1.Fill(_subtitle);
subtitleListView1.SelectIndexAndEnsureVisible(0);
}
private void VobSubOcr_FormClosing(object sender, FormClosingEventArgs e)
{
if (_tesseractThread != null)
_tesseractThread.CancelAsync();
_tesseractAsyncIndex = 10000;
}
}
}