work on nOCR

This commit is contained in:
Nikolaj Olsson 2020-05-31 07:27:04 +02:00
parent 9ffdd7357c
commit 8d92cf6095
9 changed files with 203 additions and 87 deletions

View File

@ -120,6 +120,7 @@
0,
0,
0});
this.numericUpDownExpandCount.ValueChanged += new System.EventHandler(this.NumericUpDownExpandCountValueChanged);
//
// labelImageInfo
//
@ -163,6 +164,7 @@
this.buttonCancel.TabIndex = 3;
this.buttonCancel.Text = "C&ancel";
this.buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// buttonOK
//
@ -175,6 +177,7 @@
this.buttonOK.TabIndex = 5;
this.buttonOK.Text = "&OK";
this.buttonOK.UseVisualStyleBackColor = true;
this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
//
// AddBetterMultiMatchNOcr
//
@ -192,6 +195,7 @@
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "AddBetterMultiMatchNOcr";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.AddBetterMultiMatch_KeyDown);
this.groupBoxInspectItems.ResumeLayout(false);
this.groupBoxInspectItems.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownExpandCount)).EndInit();

View File

@ -1,19 +1,134 @@
using System;
using Nikse.SubtitleEdit.Core;
using Nikse.SubtitleEdit.Logic;
using Nikse.SubtitleEdit.Logic.Ocr;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Nikse.SubtitleEdit.Forms.Ocr
{
public partial class AddBetterMultiMatchNOcr : Form
{
public NOcrChar ExpandedMatch { get; set; }
private List<VobSubOcr.CompareMatch> _matches;
private List<ImageSplitterItem> _splitterItems;
private int _startIndex;
int _extraCount;
public AddBetterMultiMatchNOcr()
{
UiUtil.PreInitialize(this);
InitializeComponent();
UiUtil.FixFonts(this);
labelText.Text = Configuration.Settings.Language.General.Text;
labelImageInfo.Text = Configuration.Settings.Language.General.Preview;
buttonCancel.Text = Configuration.Settings.Language.General.Cancel;
buttonOK.Text = Configuration.Settings.Language.General.Ok;
UiUtil.FixLargeFonts(this, buttonOK);
}
internal void Initialize(int selectedIndex, List<VobSubOcr.CompareMatch> matches, List<ImageSplitterItem> splitterItems)
{
_startIndex = selectedIndex;
for (int i = 0; i < selectedIndex; i++)
{
if (matches[i] != null && matches[i].Extra != null && matches[i].Extra.Count > 0)
{
_extraCount += matches[i].Extra.Count - 1;
}
}
_matches = matches;
_splitterItems = splitterItems;
int count = 0;
for (int i = _startIndex; i < _splitterItems.Count - _extraCount; i++)
{
if (i >= _matches.Count)
{
break;
}
var m = _matches[i];
if (m == null || m.Extra?.Count > 0)
{
break;
}
count++;
listBoxInspectItems.Items.Add(m);
if (count < 3)
{
listBoxInspectItems.SetSelected(listBoxInspectItems.Items.Count - 1, true);
}
}
numericUpDownExpandCount.Maximum = listBoxInspectItems.Items.Count;
MakeExpandImage();
}
private void NumericUpDownExpandCountValueChanged(object sender, EventArgs e)
{
for (int i = 0; i < listBoxInspectItems.Items.Count; i++)
{
listBoxInspectItems.SetSelected(i, i < numericUpDownExpandCount.Value);
}
MakeExpandImage();
}
private void MakeExpandImage()
{
var splitterItem = _splitterItems[_startIndex + _extraCount];
if (splitterItem.NikseBitmap == null)
{
return;
}
//ExpandedMatch = new BinaryOcrBitmap(new NikseBitmap(splitterItem.NikseBitmap), false, (int)numericUpDownExpandCount.Value, string.Empty, splitterItem.X, splitterItem.Y) { ExpandedList = new List<BinaryOcrBitmap>() };
//for (int i = 1; i < listBoxInspectItems.Items.Count; i++)
//{
// if (i < numericUpDownExpandCount.Value)
// {
// splitterItem = _splitterItems[_startIndex + i + _extraCount];
// if (splitterItem.NikseBitmap == null)
// {
// break;
// }
// ExpandedMatch.ExpandedList.Add(new BinaryOcrBitmap(splitterItem.NikseBitmap, false, 0, null, splitterItem.X, splitterItem.Y));
// }
//}
//var newBmp = ExpandedMatch.ToOldBitmap();
//pictureBoxInspectItem.Image = newBmp;
//pictureBoxInspectItem.Width = newBmp.Width;
//pictureBoxInspectItem.Height = newBmp.Height;
}
private void buttonOK_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBoxText.Text))
{
return;
}
ExpandedMatch.Italic = checkBoxItalic.Checked;
ExpandedMatch.Text = textBoxText.Text;
DialogResult = DialogResult.OK;
}
private void buttonCancel_Click(object sender, EventArgs e)
{
ExpandedMatch = null;
DialogResult = DialogResult.Cancel;
}
private void AddBetterMultiMatch_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
DialogResult = DialogResult.Cancel;
}
}
}
}

View File

@ -36,6 +36,7 @@
this.labelImageDatabase = new System.Windows.Forms.Label();
this.buttonCancel = new System.Windows.Forms.Button();
this.buttonOK = new System.Windows.Forms.Button();
this.linkLabelOpenDictionaryFolder = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// buttonEditCharacterDatabase
@ -118,11 +119,23 @@
this.buttonOK.UseVisualStyleBackColor = true;
this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
//
// linkLabelOpenDictionaryFolder
//
this.linkLabelOpenDictionaryFolder.AutoSize = true;
this.linkLabelOpenDictionaryFolder.Location = new System.Drawing.Point(18, 181);
this.linkLabelOpenDictionaryFolder.Name = "linkLabelOpenDictionaryFolder";
this.linkLabelOpenDictionaryFolder.Size = new System.Drawing.Size(124, 13);
this.linkLabelOpenDictionaryFolder.TabIndex = 42;
this.linkLabelOpenDictionaryFolder.TabStop = true;
this.linkLabelOpenDictionaryFolder.Text = "Open \'Dictionaries\' folder";
this.linkLabelOpenDictionaryFolder.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelOpenDictionaryFolder_LinkClicked);
//
// BinaryOcrChooseEditDb
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(407, 206);
this.Controls.Add(this.linkLabelOpenDictionaryFolder);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonOK);
this.Controls.Add(this.comboBoxCharacterDatabase);
@ -156,5 +169,6 @@
private System.Windows.Forms.Label labelImageDatabase;
private System.Windows.Forms.Button buttonCancel;
private System.Windows.Forms.Button buttonOK;
private System.Windows.Forms.LinkLabel linkLabelOpenDictionaryFolder;
}
}

View File

@ -2,6 +2,7 @@
using Nikse.SubtitleEdit.Core;
using System.IO;
using System.Windows.Forms;
using Nikse.SubtitleEdit.Logic;
using Nikse.SubtitleEdit.Logic.Ocr.Binary;
namespace Nikse.SubtitleEdit.Forms.Ocr
@ -19,6 +20,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
buttonOK.Text = Configuration.Settings.Language.General.Ok;
buttonCancel.Text = Configuration.Settings.Language.General.Cancel;
labelImageDatabase.Text = Configuration.Settings.Language.VobSubOcr.ImageDatabase;
linkLabelOpenDictionaryFolder.Text = Configuration.Settings.Language.GetDictionaries.OpenDictionariesFolder;
Text = Configuration.Settings.Language.VobSubOcr.ImageDatabase;
var imageCompareDbName = string.Empty;
@ -132,5 +134,16 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
}
}
}
private void linkLabelOpenDictionaryFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string dictionaryFolder = Configuration.OcrDirectory;
if (!Directory.Exists(dictionaryFolder))
{
Directory.CreateDirectory(dictionaryFolder);
}
UiUtil.OpenFolder(dictionaryFolder);
}
}
}

View File

@ -294,7 +294,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
index++;
expandSelectionList.Add(_imageList[index]);
}
img = VobSubOcr.GetExpandedSelection(new NikseBitmap(_bitmap), expandSelectionList, false); // true
img = VobSubOcr.GetExpandedSelectionNew(new NikseBitmap(_bitmap), expandSelectionList); // true
}
vobSubOcrNOcrCharacter.Initialize(_bitmap2, img, new Point(0, 0), false, expandSelectionList.Count > 1);
@ -345,7 +345,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
{
using (var form = new AddBetterMultiMatchNOcr())
{
// form.Initialize(listBoxInspectItems.SelectedIndex, _matches, _splitterItems);
form.Initialize(listBoxInspectItems.SelectedIndex, _matchList, _imageList);
if (form.ShowDialog(this) == DialogResult.OK)
{
//_binOcrDb.Add(form.ExpandedMatch);

View File

@ -256,20 +256,10 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
return;
}
NOcrPoint selectedPoint = null;
if (listBoxLinesForeground.Focused && listBoxLinesForeground.SelectedIndex >= 0)
{
selectedPoint = (NOcrPoint)listBoxLinesForeground.Items[listBoxLinesForeground.SelectedIndex];
}
else if (listBoxlinesBackground.Focused && listBoxlinesBackground.SelectedIndex >= 0)
{
selectedPoint = (NOcrPoint)listBoxlinesBackground.Items[listBoxlinesBackground.SelectedIndex];
}
var foreground = new Pen(new SolidBrush(Color.Green));
var background = new Pen(new SolidBrush(Color.Red));
var selPenF = new Pen(new SolidBrush(Color.Green), 3);
var selPenB = new Pen(new SolidBrush(Color.Red), 3);
var selPenF = new Pen(new SolidBrush(Color.GreenYellow), 3);
var selPenB = new Pen(new SolidBrush(Color.DeepPink), 3);
if (pictureBoxCharacter.Image != null)
{
foreach (NOcrPoint op in _nocrChar.LinesForeground)
@ -282,20 +272,23 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
}
e.Graphics.DrawLine(foreground, start, end);
if (op == selectedPoint)
{
e.Graphics.DrawLine(selPenF, op.GetScaledStart(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height), op.GetScaledEnd(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height));
}
}
foreach (NOcrPoint op in _nocrChar.LinesBackground)
{
Point start = op.GetScaledStart(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height);
Point end = op.GetScaledEnd(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height);
e.Graphics.DrawLine(background, start, end);
if (op == selectedPoint)
{
e.Graphics.DrawLine(selPenB, op.GetScaledStart(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height), op.GetScaledEnd(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height));
}
}
if (listBoxLinesForeground.Focused && listBoxLinesForeground.SelectedIndex >= 0)
{
var op = (NOcrPoint)listBoxLinesForeground.Items[listBoxLinesForeground.SelectedIndex];
e.Graphics.DrawLine(selPenF, op.GetScaledStart(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height), op.GetScaledEnd(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height));
}
else if (listBoxlinesBackground.Focused && listBoxlinesBackground.SelectedIndex >= 0)
{
var op = (NOcrPoint)listBoxlinesBackground.Items[listBoxlinesBackground.SelectedIndex];
e.Graphics.DrawLine(selPenB, op.GetScaledStart(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height), op.GetScaledEnd(_nocrChar, pictureBoxCharacter.Width, pictureBoxCharacter.Height));
}
}
@ -492,8 +485,17 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
{
if (listBoxLinesForeground.SelectedItems.Count == 1)
{
var op = listBoxLinesForeground.Items[listBoxLinesForeground.SelectedIndex] as NOcrPoint;
var idx = listBoxLinesForeground.SelectedIndex;
var op = listBoxLinesForeground.Items[idx] as NOcrPoint;
_nocrChar.LinesForeground.Remove(op);
if (idx < listBoxLinesForeground.Items.Count)
{
listBoxLinesForeground.SelectedIndex = idx;
}
else if (listBoxLinesForeground.Items.Count > 0)
{
listBoxLinesForeground.SelectedIndex = listBoxLinesForeground.Items.Count - 1;
}
}
ShowOcrPoints();
}
@ -502,8 +504,17 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
{
if (listBoxlinesBackground.SelectedItems.Count == 1)
{
var op = listBoxlinesBackground.Items[listBoxlinesBackground.SelectedIndex] as NOcrPoint;
var idx = listBoxlinesBackground.SelectedIndex;
var op = listBoxlinesBackground.Items[idx] as NOcrPoint;
_nocrChar.LinesBackground.Remove(op);
if (idx < listBoxlinesBackground.Items.Count)
{
listBoxlinesBackground.SelectedIndex = idx;
}
else if (listBoxlinesBackground.Items.Count > 0)
{
listBoxlinesBackground.SelectedIndex = listBoxlinesBackground.Items.Count -1;
}
}
ShowOcrPoints();
}

View File

@ -441,10 +441,10 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
// groupBoxOcrMethod
//
this.groupBoxOcrMethod.Controls.Add(this.comboBoxOcrMethod);
this.groupBoxOcrMethod.Controls.Add(this.groupBoxNOCR);
this.groupBoxOcrMethod.Controls.Add(this.groupBoxImageCompareMethod);
this.groupBoxOcrMethod.Controls.Add(this.GroupBoxTesseractMethod);
this.groupBoxOcrMethod.Controls.Add(this.groupBoxModiMethod);
this.groupBoxOcrMethod.Controls.Add(this.groupBoxNOCR);
this.groupBoxOcrMethod.Location = new System.Drawing.Point(13, 5);
this.groupBoxOcrMethod.Name = "groupBoxOcrMethod";
this.groupBoxOcrMethod.Size = new System.Drawing.Size(392, 192);

View File

@ -501,7 +501,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
}
numericUpDownPixelsIsSpace.Value = Configuration.Settings.VobSubOcr.XOrMorePixelsMakesSpace;
numericUpDownNumberOfPixelsIsSpaceNOCR.Value = Configuration.Settings.VobSubOcr.LineOcrMaxErrorPixels;
numericUpDownNumberOfPixelsIsSpaceNOCR.Value = Configuration.Settings.VobSubOcr.XOrMorePixelsMakesSpace;
checkBoxShowOnlyForced.Text = language.ShowOnlyForcedSubtitles;
checkBoxUseTimeCodesFromIdx.Text = language.UseTimeCodesFromIdx;
@ -533,6 +533,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
checkBoxNOcrCorrect.Checked = Configuration.Settings.VobSubOcr.LineOcrDraw;
checkBoxNOcrItalic.Checked = Configuration.Settings.VobSubOcr.LineOcrAdvancedItalic;
numericUpDownNOcrMaxWrongPixels.Value = Configuration.Settings.VobSubOcr.LineOcrMaxErrorPixels;
comboBoxTesseractLanguages.Left = labelTesseractLanguage.Left + labelTesseractLanguage.Width;
buttonGetTesseractDictionaries.Left = comboBoxTesseractLanguages.Left + comboBoxTesseractLanguages.Width + 5;
@ -4027,7 +4028,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
expandSelectionList.Add(list[index]);
}
item = GetExpandedSelection(nbmpInput, expandSelectionList, checkBoxRightToLeft.Checked);
item = GetExpandedSelectionNew(nbmpInput, expandSelectionList);
item.NikseBitmap?.ReplaceTransparentWith(Color.Black);
_vobSubOcrNOcrCharacter.Initialize(bitmap, item, _manualOcrDialogPosition, _italicCheckedLast, expandSelectionList.Count > 1);
@ -4486,54 +4487,6 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
return line;
}
internal static ImageSplitterItem GetExpandedSelection(NikseBitmap bitmap, List<ImageSplitterItem> expandSelectionList, bool rightToLeft)
{
if (rightToLeft)
{
int minimumX = expandSelectionList[expandSelectionList.Count - 1].X - expandSelectionList[expandSelectionList.Count - 1].NikseBitmap.Width;
int maximumX = expandSelectionList[0].X;
int minimumY = expandSelectionList[0].Y;
int maximumY = expandSelectionList[0].Y + expandSelectionList[0].NikseBitmap.Height;
foreach (ImageSplitterItem item in expandSelectionList)
{
if (item.Y < minimumY)
{
minimumY = item.Y;
}
if (item.Y + item.NikseBitmap.Height > maximumY)
{
maximumY = item.Y + item.NikseBitmap.Height;
}
}
var part = bitmap.CopyRectangle(new Rectangle(minimumX, minimumY, maximumX - minimumX, maximumY - minimumY));
return new ImageSplitterItem(minimumX, minimumY, part);
}
else
{
int minimumX = expandSelectionList[0].X;
int maximumX = expandSelectionList[expandSelectionList.Count - 1].X + expandSelectionList[expandSelectionList.Count - 1].NikseBitmap.Width;
int minimumY = expandSelectionList[0].Y;
int maximumY = expandSelectionList[0].Y + expandSelectionList[0].NikseBitmap.Height;
foreach (ImageSplitterItem item in expandSelectionList)
{
if (item.Y < minimumY)
{
minimumY = item.Y;
}
if (item.Y + item.NikseBitmap.Height > maximumY)
{
maximumY = item.Y + item.NikseBitmap.Height;
}
}
var part = bitmap.CopyRectangle(new Rectangle(minimumX, minimumY, maximumX - minimumX, maximumY - minimumY));
return new ImageSplitterItem(minimumX, minimumY, part);
}
}
internal static ImageSplitterItem GetExpandedSelectionNew(NikseBitmap bitmap, List<ImageSplitterItem> expandSelectionList)
{
int minimumX = expandSelectionList[0].X;
@ -4547,8 +4500,8 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
{
for (int x = 0; x < item.NikseBitmap.Width; x++)
{
int a = item.NikseBitmap.GetAlpha(x, y);
if (a > 100)
var c = item.NikseBitmap.GetPixel(x, y);
if (c.A > 100 && c.R + c.G + c.B > 100)
{
nbmp.SetPixel(item.X + x, item.Y + y, Color.White);
}
@ -7930,7 +7883,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
{
Configuration.Settings.VobSubOcr.XOrMorePixelsMakesSpace = (int)numericUpDownNumberOfPixelsIsSpaceNOCR.Value;
}
Configuration.Settings.VobSubOcr.LineOcrMaxErrorPixels = (int)numericUpDownNumberOfPixelsIsSpaceNOCR.Value;
Configuration.Settings.VobSubOcr.LineOcrMaxErrorPixels = (int)numericUpDownNOcrMaxWrongPixels.Value;
Configuration.Settings.VobSubOcr.UseTesseractFallback = checkBoxTesseractFallback.Checked;
Configuration.Settings.VobSubOcr.CaptureTopAlign = toolStripMenuItemCaptureTopAlign.Checked;

View File

@ -595,7 +595,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
public static void GenerateLineSegments(int maxNumberOfLines, bool veryPrecise, NOcrChar nOcrChar, NikseBitmap nbmp)
{
const int giveUpCount = 10000;
const int giveUpCount = 15000;
var r = new Random();
int count = 0;
int hits = 0;
@ -639,7 +639,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
}
}
}
else if (hits < 20 && count < 1000) // a few large lines
else if (hits < 20 && count < 2000) // a few large lines
{
for (int k = 0; k < 500; k++)
{
@ -651,7 +651,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height));
}
}
else if (hits < 30 && count < 2000) // some medium lines
else if (hits < 30 && count < 3000) // some medium lines
{
for (int k = 0; k < 500; k++)
{
@ -686,6 +686,12 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
ok = false;
}
}
if (end.X == start.X && end.Y == start.Y)
{
ok = false;
}
if (ok && IsMatchPointForeGround(op, !tempVeryPrecise, nbmp, nOcrChar))
{
nOcrChar.LinesForeground.Add(op);