public VobSubNOcrEdit(NOcrDb nOcrDb, Bitmap bitmap, string fileName) { UiUtil.PreInitialize(this); InitializeComponent(); VobSubEditCharacters.MakeToolStripLetters(contextMenuStripLetters, InsertLanguageCharacter); UiUtil.FixFonts(this); _nOcrDb = nOcrDb; _bitmap = bitmap; FillComboBox(); if (bitmap != null) { pictureBoxCharacter.Image = bitmap; SizePictureBox(); } labelInfo.Text = $"{(_nOcrDb.OcrCharacters.Count + _nOcrDb.OcrCharactersExpanded.Count):#,###,##0} elements in database"; labelNOcrCharInfo.Text = string.Empty; if (!string.IsNullOrEmpty(fileName)) { Text = "nOCR DB - " + fileName; } if (comboBoxTexts.Items.Count > 0) { comboBoxTexts.SelectedIndex = 0; } }
private void buttonTrain_Click(object sender, EventArgs e) { if (!System.IO.File.Exists(textBoxInputFile.Text)) { return; } int numberOfCharactersLeaned = 0; int numberOfCharactersSkipped = 0; var nOcrD = new NOcrDb(textBoxNOcrDb.Text); var lines = new List <string>(); foreach (string line in System.IO.File.ReadAllLines(textBoxInputFile.Text)) { lines.Add(line); } var format = new SubRip(); var sub = new Subtitle(); format.LoadSubtitle(sub, lines, textBoxInputFile.Text); var charactersLearned = new List <string>(); foreach (ListViewItem item in listViewFonts.Items) { if (item.Checked) { _subtitleFontName = item.Text; _subtitleFontSize = Convert.ToInt32(comboBoxSubtitleFontSize.Items[comboBoxSubtitleFontSize.SelectedIndex].ToString()); charactersLearned = new List <string>(); foreach (Paragraph p in sub.Paragraphs) { foreach (char ch in p.Text) { string s = ch.ToString(); if (s.Trim().Length > 0) { if (!charactersLearned.Contains(s)) { TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, s, false); if (checkBoxBold.Checked) { TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, s, true); } } } } } } } nOcrD.Save(); }
internal void Initialize(Bitmap bitmap, int pixelsIsSpace, bool rightToLeft, NOcrDb nOcrDb, VobSubOcr vobSubOcr, double unItalicFactor) { _bitmap = bitmap; var nbmp = new NikseBitmap(bitmap); nbmp.ReplaceNonWhiteWithTransparent(); bitmap = nbmp.GetBitmap(); _bitmap2 = bitmap; _nocrChars = nOcrDb.OcrCharacters; _matchList = new List <VobSubOcr.CompareMatch>(); _vobSubOcr = vobSubOcr; _unItalicFactor = unItalicFactor; int minLineHeight = 6; _imageList = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, pixelsIsSpace, rightToLeft, Configuration.Settings.VobSubOcr.TopToBottom, minLineHeight); // _imageList = NikseBitmapImageSplitter.SplitBitmapToLetters(nbmp, pixelsIsSpace, rightToLeft, Configuration.Settings.VobSubOcr.TopToBottom); int index = 0; while (index < _imageList.Count) { ImageSplitterItem item = _imageList[index]; if (item.NikseBitmap == null) { listBoxInspectItems.Items.Add(item.SpecialCharacter); _matchList.Add(null); } else { nbmp = item.NikseBitmap; nbmp.ReplaceNonWhiteWithTransparent(); item.Y += nbmp.CropTopTransparent(0); nbmp.CropTransparentSidesAndBottom(0, true); nbmp.ReplaceTransparentWith(Color.Black); //get nocr matches Nikse.SubtitleEdit.Forms.VobSubOcr.CompareMatch match = vobSubOcr.GetNOcrCompareMatchNew(item, nbmp, nOcrDb, _unItalicFactor, false, false); if (match == null) { listBoxInspectItems.Items.Add("?"); _matchList.Add(null); } else { listBoxInspectItems.Items.Add(match.Text); _matchList.Add(match); } } index++; } }
public BinaryOcrChooseEditDb(string binaryImageDb) { UiUtil.PreInitialize(this); InitializeComponent(); UiUtil.FixFonts(this); buttonNewCharacterDatabase.Text = Configuration.Settings.Language.VobSubOcr.New; buttonEditCharacterDatabase.Text = Configuration.Settings.Language.VobSubOcr.Edit; 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; var nOcrDbName = string.Empty; ImageCompareDatabaseName = binaryImageDb; if (!string.IsNullOrEmpty(binaryImageDb)) { var parts = binaryImageDb.Split('+'); if (parts.Length > 0) { imageCompareDbName = parts[0]; if (parts.Length > 1) { nOcrDbName = parts[1]; } } } comboBoxNOcrLanguage.Items.Clear(); comboBoxNOcrLanguage.Items.Add(string.Empty); foreach (string s in NOcrDb.GetDatabases()) { comboBoxNOcrLanguage.Items.Add(s); if (s == nOcrDbName) { comboBoxNOcrLanguage.SelectedIndex = comboBoxNOcrLanguage.Items.Count - 1; } } comboBoxCharacterDatabase.Items.Clear(); foreach (string s in BinaryOcrDb.GetDatabases()) { comboBoxCharacterDatabase.Items.Add(s); if (s == imageCompareDbName) { comboBoxCharacterDatabase.SelectedIndex = comboBoxCharacterDatabase.Items.Count - 1; } } }
public void TestNOcrSaveLoad() { string tempFileName = FileUtil.GetTempFileName(".nocr"); var db = new NOcrDb(tempFileName); var nOcrChar = new NOcrChar("t"); nOcrChar.ExpandCount = 0; nOcrChar.Italic = false; nOcrChar.MarginTop = 2; nOcrChar.Width = 10; nOcrChar.Height = 10; nOcrChar.LinesForeground.Add(new NOcrPoint(new Point(1, 1), new Point(2, 2))); nOcrChar.LinesBackground.Add(new NOcrPoint(new Point(3, 4), new Point(5, 6))); db.Add(nOcrChar); var nOcrChar2 = new NOcrChar("u"); nOcrChar2.ExpandCount = 0; nOcrChar2.Italic = false; nOcrChar2.MarginTop = 3; nOcrChar2.Width = 12; nOcrChar2.Height = 12; nOcrChar2.LinesForeground.Add(new NOcrPoint(new Point(1, 1), new Point(2, 2))); nOcrChar2.LinesBackground.Add(new NOcrPoint(new Point(3, 4), new Point(5, 6))); db.Add(nOcrChar2); db.Save(); db = new NOcrDb(tempFileName); Assert.IsTrue(db.OcrCharacters.Count == 2); Assert.IsTrue(db.OcrCharacters[0].Text == nOcrChar2.Text); Assert.IsTrue(db.OcrCharacters[0].Italic == nOcrChar2.Italic); Assert.IsTrue(db.OcrCharacters[0].MarginTop == nOcrChar2.MarginTop); Assert.IsTrue(db.OcrCharacters[0].LinesForeground.Count == nOcrChar2.LinesForeground.Count); Assert.IsTrue(db.OcrCharacters[0].LinesForeground[0].Start.X == nOcrChar2.LinesForeground[0].Start.X); Assert.IsTrue(db.OcrCharacters[0].LinesForeground[0].Start.Y == nOcrChar2.LinesForeground[0].Start.Y); Assert.IsTrue(db.OcrCharacters[0].LinesBackground.Count == nOcrChar2.LinesBackground.Count); Assert.IsTrue(db.OcrCharacters[0].LinesBackground[0].Start.X == nOcrChar2.LinesBackground[0].Start.X); Assert.IsTrue(db.OcrCharacters[0].LinesBackground[0].Start.Y == nOcrChar2.LinesBackground[0].Start.Y); Assert.IsTrue(db.OcrCharacters[1].Text == nOcrChar.Text); try { File.Delete(tempFileName); } catch { } }
internal void Initialize(Bitmap bitmap, int pixelsIsSpace, bool rightToLeft, NOcrDb nOcrDb, VobSubOcr vobSubOcr, bool italic, int minLineHeight, bool deepSeek) { _bitmap = bitmap; var nikseBitmap = new NikseBitmap(bitmap); nikseBitmap.ReplaceNonWhiteWithTransparent(); _nOcrChars = nOcrDb.OcrCharacters; _nOcrDb = nOcrDb; _matchList = new List <VobSubOcr.CompareMatch>(); _imageList = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nikseBitmap, pixelsIsSpace, rightToLeft, Configuration.Settings.VobSubOcr.TopToBottom, minLineHeight, false); int index = 0; _indexLookup = new Dictionary <int, int>(); while (index < _imageList.Count) { var item = _imageList[index]; if (item.NikseBitmap == null) { _indexLookup.Add(listBoxInspectItems.Items.Count, index); listBoxInspectItems.Items.Add(item.SpecialCharacter); _matchList.Add(null); } else { var match = vobSubOcr.GetNOcrCompareMatchNew(item, nikseBitmap, nOcrDb, italic, deepSeek, index, _imageList); if (match == null) { _indexLookup.Add(listBoxInspectItems.Items.Count, index); listBoxInspectItems.Items.Add("?"); _matchList.Add(null); } else { _indexLookup.Add(listBoxInspectItems.Items.Count, index); listBoxInspectItems.Items.Add(match.Text); _matchList.Add(match); if (match.ExpandCount > 0) { index += match.ExpandCount - 1; } } } index++; } }
private void buttonImport_Click(object sender, EventArgs e) { int importedCount = 0; int notImportedCount = 0; openFileDialog1.Filter = "nOCR files|*.nocr"; openFileDialog1.InitialDirectory = Configuration.DataDirectory; openFileDialog1.FileName = string.Empty; openFileDialog1.Title = "Import existing nOCR database into current"; if (openFileDialog1.ShowDialog(this) == DialogResult.OK) { var newDb = new NOcrDb(openFileDialog1.FileName); importedCount = ImportCharacters(importedCount, ref notImportedCount, newDb.OcrCharacters); importedCount += ImportCharacters(importedCount, ref notImportedCount, newDb.OcrCharactersExpanded); if (importedCount > 0) { Changed = true; } MessageBox.Show($"Number of characters imported: {importedCount}\r\nNumber of characters not imported (already present): {notImportedCount}"); } }
private void buttonTrain_Click(object sender, EventArgs e) { if (buttonTrain.Text == "Abort") { _abort = true; return; } if (!File.Exists(textBoxInputFile.Text)) { return; } _abort = false; buttonTrain.Text = "Abort"; buttonOK.Enabled = false; int numberOfCharactersLeaned = 0; int numberOfCharactersSkipped = 0; var nOcrD = new NOcrDb(null); var lines = new List <string>(); foreach (string line in File.ReadAllLines(textBoxInputFile.Text)) { lines.Add(line); } var format = new SubRip(); var sub = new Subtitle(); format.LoadSubtitle(sub, lines, textBoxInputFile.Text); foreach (ListViewItem item in listViewFonts.Items) { if (item.Checked) { _subtitleFontName = item.Text; _subtitleFontSize = Convert.ToInt32(comboBoxSubtitleFontSize.Items[comboBoxSubtitleFontSize.SelectedIndex].ToString()); var charactersLearned = new List <string>(); foreach (Paragraph p in sub.Paragraphs) { foreach (char ch in p.Text) { if (!char.IsWhiteSpace(ch)) { var s = ch.ToString(); if (!charactersLearned.Contains(s)) { TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, s, false); if (checkBoxBold.Checked) { TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, s, true); } } } } if (_abort) { break; } } if (_abort) { break; } } } labelInfo.Text = "Saving..."; labelInfo.Refresh(); saveFileDialog1.Filter = "nOCR DB|*.nocr"; saveFileDialog1.InitialDirectory = Configuration.OcrDirectory; if (saveFileDialog1.ShowDialog(this) == DialogResult.OK) { labelInfo.Text = $"Saving to {saveFileDialog1.FileName}..."; nOcrD.FileName = saveFileDialog1.FileName; nOcrD.Save(); labelInfo.Text = "Training done"; } else { labelInfo.Text = "Saving aborted"; } buttonOK.Enabled = true; buttonTrain.Text = "Start training"; _abort = false; }
private void TrainLetter(ref int numberOfCharactersLeaned, ref int numberOfCharactersSkipped, NOcrDb nOcrD, List <string> charactersLearned, string s, bool bold) { Bitmap bmp = GenerateImageFromTextWithStyle("H " + s, bold); var nbmp = new NikseBitmap(bmp); nbmp.MakeTwoColor(280); nbmp.CropTop(0, Color.FromArgb(0, 0, 0, 0)); var list = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, 10, false, false, 25); if (list.Count == 3) { var item = list[2]; NOcrChar match = nOcrD.GetMatch(item.NikseBitmap, item.Top, false, false, 0); if (match == null || match.Text != s) { labelInfo.Refresh(); Application.DoEvents(); NOcrChar nOcrChar = new NOcrChar(s) { Width = item.NikseBitmap.Width, Height = item.NikseBitmap.Height, MarginTop = item.Top, }; VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value, checkBoxVeryAccurate.Checked, nOcrChar, item.NikseBitmap); nOcrD.Add(nOcrChar); charactersLearned.Add(s); numberOfCharactersLeaned++; labelInfo.Text = string.Format("Now training font '{1}', total characters learned is {0:#,###,###}, {2:#,###,###} skipped", numberOfCharactersLeaned, _subtitleFontName, numberOfCharactersSkipped); bmp.Dispose(); } else { numberOfCharactersSkipped++; } } }
private void TrainLetter(ref int numberOfCharactersLeaned, ref int numberOfCharactersSkipped, NOcrDb nOcrD, List <string> charactersLearned, string s, bool bold) { Bitmap bmp = GenerateImageFromTextWithStyle(s, bold); var nbmp = new NikseBitmap(bmp); nbmp.MakeTwoColor(210, 280); var list = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, 10, false, false, 25); if (list.Count == 1) { NOcrChar match = nOcrD.GetMatch(list[0].NikseBitmap); if (match == null) { pictureBox1.Image = list[0].NikseBitmap.GetBitmap(); this.Refresh(); Application.DoEvents(); System.Threading.Thread.Sleep(100); NOcrChar nOcrChar = new NOcrChar(s); nOcrChar.Width = list[0].NikseBitmap.Width; nOcrChar.Height = list[0].NikseBitmap.Height; VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value, checkBoxVeryAccurate.Checked, nOcrChar, list[0].NikseBitmap); nOcrD.Add(nOcrChar); charactersLearned.Add(s); numberOfCharactersLeaned++; labelInfo.Text = string.Format("Now training font '{1}', total characters leaned is {0}, {2} skipped", numberOfCharactersLeaned, _subtitleFontName, numberOfCharactersSkipped); bmp.Dispose(); } else { numberOfCharactersSkipped++; } } }
private void buttonImport_Click(object sender, EventArgs e) { int importedCount = 0; int notImportedCount = 0; openFileDialog1.Filter = "nOCR files|*.nocr"; openFileDialog1.InitialDirectory = Configuration.DataDirectory; openFileDialog1.FileName = string.Empty; openFileDialog1.Title = "Import existing nOCR database into current"; if (openFileDialog1.ShowDialog(this) == DialogResult.OK) { NOcrDb newDb = new NOcrDb(openFileDialog1.FileName); foreach (NOcrChar newChar in newDb.OcrCharacters) { bool found = false; foreach (NOcrChar oldChar in _nocrChars) { if (oldChar.Text == newChar.Text && oldChar.Width == newChar.Width && oldChar.Height == newChar.Height && oldChar.MarginTop == newChar.MarginTop && oldChar.ExpandCount == newChar.ExpandCount && oldChar.LinesForeground.Count == newChar.LinesForeground.Count && oldChar.LinesBackground.Count == newChar.LinesBackground.Count) { found = true; for (int i = 0; i < oldChar.LinesForeground.Count; i++) { if (oldChar.LinesForeground[i].Start.X != newChar.LinesForeground[i].Start.X || oldChar.LinesForeground[i].Start.Y != newChar.LinesForeground[i].Start.Y || oldChar.LinesForeground[i].End.X != newChar.LinesForeground[i].End.X || oldChar.LinesForeground[i].End.Y != newChar.LinesForeground[i].End.Y) { found = false; } } for (int i = 0; i < oldChar.LinesBackground.Count; i++) { if (oldChar.LinesBackground[i].Start.X != newChar.LinesBackground[i].Start.X || oldChar.LinesBackground[i].Start.Y != newChar.LinesBackground[i].Start.Y || oldChar.LinesBackground[i].End.X != newChar.LinesBackground[i].End.X || oldChar.LinesBackground[i].End.Y != newChar.LinesBackground[i].End.Y) { found = false; } } } } if (!found) { _nocrChars.Add(newChar); importedCount++; } else { notImportedCount++; } } MessageBox.Show(string.Format("Number of characters imported: {0}\r\nNumber of characters not imported (already present): {1}", importedCount, notImportedCount)); } }
internal void Initialize(NOcrDb _nOcrDb) { if (_nOcrDb != null) { } }
private void TrainLetter(ref int numberOfCharactersLearned, ref int numberOfCharactersSkipped, NOcrDb nOcrD, string s, bool bold, bool italic, bool doubleLetter) { var bmp = GenerateImageFromTextWithStyle("H " + s, bold, italic); var nikseBitmap = new NikseBitmap(bmp); nikseBitmap.MakeTwoColor(280); nikseBitmap.CropTop(0, Color.FromArgb(0, 0, 0, 0)); var list = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nikseBitmap, 10, false, false, 25, false); if (list.Count == 3) { var item = list[2]; var match = nOcrD.GetMatch(item.NikseBitmap, item.Top, false, 25); if (match == null || match.Text != s) { labelInfo.Refresh(); Application.DoEvents(); var nOcrChar = new NOcrChar(s) { Width = item.NikseBitmap.Width, Height = item.NikseBitmap.Height, MarginTop = item.Top, Italic = italic, }; VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value + (doubleLetter ? 20 : 0), false, nOcrChar, item.NikseBitmap); nOcrD.Add(nOcrChar); numberOfCharactersLearned++; labelInfo.Text = string.Format(Configuration.Settings.Language.VobSubOcr.NowTraining, numberOfCharactersLearned, _subtitleFontName, numberOfCharactersSkipped); bmp.Dispose(); } else { numberOfCharactersSkipped++; } } else if (!doubleLetter) { if (list.Count == 4 && list[2].NikseBitmap != null && list[3].NikseBitmap != null) { // e.g. quote (") var expandItem = VobSubOcr.GetExpandedSelectionNew(nikseBitmap, new List <ImageSplitterItem> { list[2], list[3] }); var match = nOcrD.GetMatchExpanded(nikseBitmap, expandItem, 2, list); if (match != null && match.Text == s) { numberOfCharactersSkipped++; return; } var nOcrChar = new NOcrChar(s) { Width = expandItem.NikseBitmap.Width, Height = expandItem.NikseBitmap.Height, MarginTop = expandItem.Top, Italic = italic, ExpandCount = 2, }; VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value + 5, false, nOcrChar, expandItem.NikseBitmap); nOcrD.Add(nOcrChar); return; } if (list.Count == 5 && list[2].NikseBitmap != null && list[3].NikseBitmap != null && list[4].NikseBitmap != null) { // e.g. "%" var expandItem = VobSubOcr.GetExpandedSelectionNew(nikseBitmap, new List <ImageSplitterItem> { list[2], list[3], list[4] }); var match = nOcrD.GetMatchExpanded(nikseBitmap, expandItem, 2, list); if (match != null && match.Text == s) { numberOfCharactersSkipped++; return; } var nOcrChar = new NOcrChar(s) { Width = expandItem.NikseBitmap.Width, Height = expandItem.NikseBitmap.Height, MarginTop = expandItem.Top, Italic = italic, ExpandCount = 3, }; nOcrD.Add(nOcrChar); VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value + 10, false, nOcrChar, expandItem.NikseBitmap); return; } numberOfCharactersSkipped++; } }