Work on nOCR

This commit is contained in:
Nikolaj Olsson 2020-06-08 17:56:40 +02:00
parent eb9da33e05
commit 9b6535c454
4 changed files with 56 additions and 58 deletions

Binary file not shown.

View File

@ -1,6 +1,5 @@
using Nikse.SubtitleEdit.Core; using Nikse.SubtitleEdit.Core;
using Nikse.SubtitleEdit.Logic; using Nikse.SubtitleEdit.Logic;
using Nikse.SubtitleEdit.Logic.Ocr;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
@ -10,14 +9,16 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
{ {
public partial class AddBetterMultiMatchNOcr : Form public partial class AddBetterMultiMatchNOcr : Form
{ {
public NOcrChar NOcrChar { get; set; } public ImageSplitterItem ExpandItem { get; set; }
public int ExpandCount { get; set; }
public bool ExpandItalic { get; set; }
public string ExpandText { get; set; }
private NikseBitmap _wholeImage; private NikseBitmap _wholeImage;
private List<VobSubOcr.CompareMatch> _matches; private List<VobSubOcr.CompareMatch> _matches;
private List<ImageSplitterItem> _splitterItems; private List<ImageSplitterItem> _splitterItems;
private ImageSplitterItem _expandItem;
private int _startIndex; private int _startIndex;
int _extraCount; private int _extraCount;
public AddBetterMultiMatchNOcr() public AddBetterMultiMatchNOcr()
{ {
@ -103,8 +104,8 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
} }
} }
_expandItem = VobSubOcr.GetExpandedSelectionNew(_wholeImage,expandList); ExpandItem = VobSubOcr.GetExpandedSelectionNew(_wholeImage, expandList);
var newBmp = _expandItem.NikseBitmap.GetBitmap(); var newBmp = ExpandItem.NikseBitmap.GetBitmap();
pictureBoxInspectItem.Image = newBmp; pictureBoxInspectItem.Image = newBmp;
pictureBoxInspectItem.Width = newBmp.Width; pictureBoxInspectItem.Width = newBmp.Width;
pictureBoxInspectItem.Height = newBmp.Height; pictureBoxInspectItem.Height = newBmp.Height;
@ -112,14 +113,10 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
private void buttonOK_Click(object sender, EventArgs e) private void buttonOK_Click(object sender, EventArgs e)
{ {
using (var form = new VobSubOcrNOcrCharacter()) ExpandCount = (int)numericUpDownExpandCount.Value;
{ ExpandItalic = checkBoxItalic.Checked;
form.Initialize(_expandItem.NikseBitmap.GetBitmap(), _expandItem, new Point(0, 0), checkBoxItalic.Checked, false, false, textBoxText.Text); ExpandText = textBoxText.Text;
var result = form.ShowDialog(this); DialogResult = DialogResult.OK;
NOcrChar = form.NOcrChar;
NOcrChar.ExpandCount = (int)numericUpDownExpandCount.Value;
DialogResult = result;
}
} }
private void buttonCancel_Click(object sender, EventArgs e) private void buttonCancel_Click(object sender, EventArgs e)

View File

@ -12,12 +12,10 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
{ {
private List<ImageSplitterItem> _imageList; private List<ImageSplitterItem> _imageList;
private List<VobSubOcr.CompareMatch> _matchList; private List<VobSubOcr.CompareMatch> _matchList;
private List<NOcrChar> _nocrChars; private List<NOcrChar> _nOcrChars;
private NOcrChar _nocrChar; private NOcrChar _nOcrChar;
private NOcrDb _nocrDb; private NOcrDb _nOcrDb;
private VobSubOcr _vobSubOcr;
private Bitmap _bitmap; private Bitmap _bitmap;
private Bitmap _bitmap2;
private double _zoomFactor = 3.0; private double _zoomFactor = 3.0;
private Dictionary<int, int> _indexLookup; private Dictionary<int, int> _indexLookup;
@ -68,16 +66,13 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
internal void Initialize(Bitmap bitmap, int pixelsIsSpace, bool rightToLeft, NOcrDb nOcrDb, VobSubOcr vobSubOcr, bool italic, int minLineHeight) internal void Initialize(Bitmap bitmap, int pixelsIsSpace, bool rightToLeft, NOcrDb nOcrDb, VobSubOcr vobSubOcr, bool italic, int minLineHeight)
{ {
_bitmap = bitmap; _bitmap = bitmap;
var nbmp = new NikseBitmap(bitmap); var nikseBitmap = new NikseBitmap(bitmap);
nbmp.ReplaceNonWhiteWithTransparent(); nikseBitmap.ReplaceNonWhiteWithTransparent();
bitmap = nbmp.GetBitmap(); _nOcrChars = nOcrDb.OcrCharacters;
_bitmap2 = bitmap; _nOcrDb = nOcrDb;
_nocrChars = nOcrDb.OcrCharacters;
_nocrDb = nOcrDb;
_matchList = new List<VobSubOcr.CompareMatch>(); _matchList = new List<VobSubOcr.CompareMatch>();
_vobSubOcr = vobSubOcr;
_imageList = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, pixelsIsSpace, rightToLeft, Configuration.Settings.VobSubOcr.TopToBottom, minLineHeight); _imageList = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nikseBitmap, pixelsIsSpace, rightToLeft, Configuration.Settings.VobSubOcr.TopToBottom, minLineHeight);
int index = 0; int index = 0;
_indexLookup = new Dictionary<int, int>(); _indexLookup = new Dictionary<int, int>();
@ -92,7 +87,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
} }
else else
{ {
var match = vobSubOcr.GetNOcrCompareMatchNew(item, nbmp, nOcrDb, italic, false, index, _imageList); var match = vobSubOcr.GetNOcrCompareMatchNew(item, nikseBitmap, nOcrDb, italic, false, index, _imageList);
if (match == null) if (match == null)
{ {
_indexLookup.Add(listBoxInspectItems.Items.Count, index); _indexLookup.Add(listBoxInspectItems.Items.Count, index);
@ -166,7 +161,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
if (match == null) if (match == null)
{ // spaces+new lines { // spaces+new lines
_nocrChar = null; _nOcrChar = null;
pictureBoxCharacter.Invalidate(); pictureBoxCharacter.Invalidate();
buttonUpdate.Enabled = false; buttonUpdate.Enabled = false;
@ -184,7 +179,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
buttonDelete.Enabled = false; buttonDelete.Enabled = false;
textBoxText.Text = string.Empty; textBoxText.Text = string.Empty;
checkBoxItalic.Checked = match.Italic; checkBoxItalic.Checked = match.Italic;
_nocrChar = null; _nOcrChar = null;
pictureBoxCharacter.Invalidate(); pictureBoxCharacter.Invalidate();
buttonEditDB.Enabled = true; buttonEditDB.Enabled = true;
@ -198,7 +193,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
buttonDelete.Enabled = true; buttonDelete.Enabled = true;
textBoxText.Text = match.Text; textBoxText.Text = match.Text;
checkBoxItalic.Checked = match.Italic; checkBoxItalic.Checked = match.Italic;
_nocrChar = match.NOcrCharacter; _nOcrChar = match.NOcrCharacter;
pictureBoxCharacter.Invalidate(); pictureBoxCharacter.Invalidate();
buttonEditDB.Enabled = true; buttonEditDB.Enabled = true;
@ -210,7 +205,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
private void pictureBoxCharacter_Paint(object sender, PaintEventArgs e) private void pictureBoxCharacter_Paint(object sender, PaintEventArgs e)
{ {
if (_nocrChar == null) if (_nOcrChar == null)
{ {
return; return;
} }
@ -219,13 +214,13 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
var background = new Pen(new SolidBrush(Color.Red)); var background = new Pen(new SolidBrush(Color.Red));
if (pictureBoxCharacter.Image != null) if (pictureBoxCharacter.Image != null)
{ {
foreach (NOcrPoint op in _nocrChar.LinesForeground) foreach (NOcrPoint op in _nOcrChar.LinesForeground)
{ {
e.Graphics.DrawLine(foreground, op.GetScaledStart(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height), op.GetScaledEnd(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height)); e.Graphics.DrawLine(foreground, op.GetScaledStart(_nOcrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height), op.GetScaledEnd(_nOcrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height));
} }
foreach (NOcrPoint op in _nocrChar.LinesBackground) foreach (NOcrPoint op in _nOcrChar.LinesBackground)
{ {
e.Graphics.DrawLine(background, op.GetScaledStart(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height), op.GetScaledEnd(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height)); e.Graphics.DrawLine(background, op.GetScaledStart(_nOcrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height), op.GetScaledEnd(_nOcrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height));
} }
} }
} }
@ -261,10 +256,10 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
private void buttonUpdate_Click(object sender, EventArgs e) private void buttonUpdate_Click(object sender, EventArgs e)
{ {
if (_nocrChar != null) if (_nOcrChar != null)
{ {
_nocrChar.Text = textBoxText.Text; _nOcrChar.Text = textBoxText.Text;
_nocrChar.Italic = checkBoxItalic.Checked; _nOcrChar.Italic = checkBoxItalic.Checked;
ShowStatus("Character updated"); ShowStatus("Character updated");
} }
} }
@ -285,9 +280,9 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
private void buttonDelete_Click(object sender, EventArgs e) private void buttonDelete_Click(object sender, EventArgs e)
{ {
if (_nocrChar != null) if (_nOcrChar != null)
{ {
_nocrDb.Remove(_nocrChar); _nOcrDb.Remove(_nOcrChar);
ShowStatus("Character deleted"); ShowStatus("Character deleted");
} }
} }
@ -310,7 +305,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
using (var vobSubOcrNOcrCharacter = new VobSubOcrNOcrCharacter()) using (var vobSubOcrNOcrCharacter = new VobSubOcrNOcrCharacter())
{ {
var text = string.Empty; var text = string.Empty;
if (_nocrChar != null && _nocrChar.Text != textBoxText.Text) if (_nOcrChar != null && _nOcrChar.Text != textBoxText.Text)
{ {
text = textBoxText.Text; text = textBoxText.Text;
} }
@ -344,7 +339,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
img = VobSubOcr.GetExpandedSelectionNew(new NikseBitmap(_bitmap), expandSelectionList); // true img = VobSubOcr.GetExpandedSelectionNew(new NikseBitmap(_bitmap), expandSelectionList); // true
} }
vobSubOcrNOcrCharacter.Initialize(_bitmap2, img, new Point(0, 0), checkBoxItalic.Checked, true,expandSelectionList.Count > 1, string.Empty); vobSubOcrNOcrCharacter.Initialize(_bitmap, img, new Point(0, 0), checkBoxItalic.Checked, true, expandSelectionList.Count > 1, string.Empty);
result = vobSubOcrNOcrCharacter.ShowDialog(this); result = vobSubOcrNOcrCharacter.ShowDialog(this);
if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ShrinkSelection) if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ShrinkSelection)
@ -359,7 +354,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
else if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ExpandSelection) else if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ExpandSelection)
{ {
expandSelection = true; expandSelection = true;
if (_imageList[index+1].NikseBitmap != null) if (_imageList[index + 1].NikseBitmap != null)
{ {
index++; index++;
expandSelectionList.Add(_imageList[index]); expandSelectionList.Add(_imageList[index]);
@ -373,7 +368,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
vobSubOcrNOcrCharacter.NOcrChar.ExpandCount = expandSelectionList.Count; vobSubOcrNOcrCharacter.NOcrChar.ExpandCount = expandSelectionList.Count;
} }
_nocrChars.Add(vobSubOcrNOcrCharacter.NOcrChar); _nOcrChars.Add(vobSubOcrNOcrCharacter.NOcrChar);
DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;
} }
} }
@ -386,7 +381,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
private void buttonEditDB_Click(object sender, EventArgs e) private void buttonEditDB_Click(object sender, EventArgs e)
{ {
var form = new VobSubNOcrEdit(_nocrDb, pictureBoxInspectItem.Image as Bitmap, null); var form = new VobSubNOcrEdit(_nOcrDb, pictureBoxInspectItem.Image as Bitmap, null);
form.ShowDialog(this); form.ShowDialog(this);
} }
@ -411,9 +406,20 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
form.Initialize(_bitmap, 0, tempMatchList, tempImageList); form.Initialize(_bitmap, 0, tempMatchList, tempImageList);
if (form.ShowDialog(this) == DialogResult.OK) if (form.ShowDialog(this) == DialogResult.OK)
{ {
_nocrDb.Add(form.NOcrChar); using (var charForm = new VobSubOcrNOcrCharacter())
DialogResult = DialogResult.OK; {
charForm.Initialize(_bitmap, form.ExpandItem, new Point(0, 0), form.ExpandItalic, false, false, form.ExpandText);
if (charForm.ShowDialog(this) == DialogResult.OK)
{
charForm.NOcrChar.ExpandCount = form.ExpandCount;
_nOcrDb.Add(charForm.NOcrChar);
DialogResult = DialogResult.OK;
return;
}
}
} }
DialogResult = DialogResult.Cancel;
} }
} }
} }

View File

@ -4856,14 +4856,8 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
private void InitializeNOcrThreads(int max) private void InitializeNOcrThreads(int max)
{ {
if (_nOcrDbThread == null || _nOcrThreadResults == null || _nOcrDbThread = new NOcrDb(_nOcrDb, null);
_nOcrDb.OcrCharacters.Count != _nOcrDbThread.OcrCharacters.Count || _nOcrThreadResults = new NOcrThreadResult[max];
_nOcrDb.OcrCharacters.Count != _nOcrDbThread.OcrCharacters.Count ||
_subtitle.Paragraphs.Count != _nOcrThreadResults.Length)
{
_nOcrDbThread = new NOcrDb(_nOcrDb, null);
_nOcrThreadResults = new NOcrThreadResult[max];
}
int noOfThreads = Environment.ProcessorCount - 1; int noOfThreads = Environment.ProcessorCount - 1;
if (noOfThreads >= max) if (noOfThreads >= max)
@ -4895,7 +4889,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
}; };
bw.DoWork += NOcrThreadDoWork; bw.DoWork += NOcrThreadDoWork;
bw.RunWorkerCompleted += NOcrThreadRunWorkerCompleted; bw.RunWorkerCompleted += NOcrThreadRunWorkerCompleted;
// bw.RunWorkerAsync(p); bw.RunWorkerAsync(p);
Application.DoEvents(); Application.DoEvents();
} }
} }
@ -6503,6 +6497,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
{ {
_mainOcrTimer?.Stop(); _mainOcrTimer?.Stop();
_abort = true; _abort = true;
_ocrThreadStop = true;
_tesseractThreadRunner?.Cancel(); _tesseractThreadRunner?.Cancel();
buttonStop.Enabled = false; buttonStop.Enabled = false;
progressBar1.Visible = false; progressBar1.Visible = false;