Allow italic in nOCR train

This commit is contained in:
Nikolaj Olsson 2020-06-01 22:43:39 +02:00
parent f9c28b6544
commit cf9d87f2c5
2 changed files with 36 additions and 13 deletions

View File

@ -49,6 +49,7 @@
this.buttonOK = new System.Windows.Forms.Button();
this.labelInfo = new System.Windows.Forms.Label();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.checkBoxItalic = new System.Windows.Forms.CheckBox();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownSegmentsPerCharacter)).BeginInit();
this.groupBox2.SuspendLayout();
@ -59,6 +60,7 @@
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.checkBoxItalic);
this.groupBox1.Controls.Add(this.checkBoxBold);
this.groupBox1.Controls.Add(this.listViewFonts);
this.groupBox1.Controls.Add(this.label3);
@ -369,6 +371,17 @@
this.labelInfo.TabIndex = 4;
this.labelInfo.Text = "labelInfo";
//
// checkBoxItalic
//
this.checkBoxItalic.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.checkBoxItalic.AutoSize = true;
this.checkBoxItalic.Location = new System.Drawing.Point(116, 420);
this.checkBoxItalic.Name = "checkBoxItalic";
this.checkBoxItalic.Size = new System.Drawing.Size(93, 17);
this.checkBoxItalic.TabIndex = 8;
this.checkBoxItalic.Text = "Also train italic";
this.checkBoxItalic.UseVisualStyleBackColor = true;
//
// VobSubNOcrTrain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -420,5 +433,6 @@
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBoxMerged;
private System.Windows.Forms.CheckBox checkBoxItalic;
}
}

View File

@ -110,10 +110,14 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
var s = ch.ToString();
if (!charactersLearned.Contains(s))
{
TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, s, false, false);
TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, s, false, false, false);
if (checkBoxBold.Checked)
{
TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, s, true, false);
TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, s, true, false, false);
}
if (checkBoxItalic.Checked)
{
TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, s, false, true, false);
}
}
}
@ -131,10 +135,14 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
{
if (!charactersLearned.Contains(text) && text.Length > 1 && text.Length <= 3)
{
TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, text, false, true);
TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, text, false, false, true);
if (checkBoxBold.Checked)
{
TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, text, true,true);
TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, text, true, false, true);
}
if (checkBoxBold.Checked)
{
TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, text, false, true, true);
}
}
}
@ -168,9 +176,9 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
_abort = false;
}
private void TrainLetter(ref int numberOfCharactersLeaned, ref int numberOfCharactersSkipped, NOcrDb nOcrD, List<string> charactersLearned, string s, bool bold, bool doubleLetter)
private void TrainLetter(ref int numberOfCharactersLeaned, ref int numberOfCharactersSkipped, NOcrDb nOcrD, List<string> charactersLearned, string s, bool bold, bool italic, bool doubleLetter)
{
Bitmap bmp = GenerateImageFromTextWithStyle("H " + s, bold);
Bitmap bmp = GenerateImageFromTextWithStyle("H " + s, bold, italic);
var nikseBitmap = new NikseBitmap(bmp);
nikseBitmap.MakeTwoColor(280);
nikseBitmap.CropTop(0, Color.FromArgb(0, 0, 0, 0));
@ -255,20 +263,21 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
}
}
private Bitmap GenerateImageFromTextWithStyle(string text, bool bold)
private Bitmap GenerateImageFromTextWithStyle(string text, bool bold, bool italic)
{
bool subtitleFontBold = bold;
text = HtmlUtil.RemoveHtmlTags(text);
Font font;
try
{
var fontStyle = FontStyle.Regular;
if (subtitleFontBold)
if (bold)
{
fontStyle = FontStyle.Bold;
}
else if (italic)
{
fontStyle = FontStyle.Italic;
}
font = new Font(_subtitleFontName, _subtitleFontSize, fontStyle);
}
@ -328,7 +337,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
char ch = text[i];
if (ch == '\n' || ch == '\r')
{
TextDraw.DrawText(font, sf, path, sb, false, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint);
TextDraw.DrawText(font, sf, path, sb, italic, bold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint);
top += lineHeight;
newLine = true;
@ -349,7 +358,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
}
if (sb.Length > 0)
{
TextDraw.DrawText(font, sf, path, sb, false, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint);
TextDraw.DrawText(font, sf, path, sb, italic, bold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint);
}
sf.Dispose();