private void LoadImagesInListView()
        {
            int existingMatches = 0;

            listView1.SmallImageList = imageList1;
            listView1.BeginUpdate();
            var importDb = new BinaryOcrDb(openFileDialog1.FileName, true);
            var list     = new List <BinaryOcrBitmap>();

            list.AddRange(importDb.CompareImages);
            list.AddRange(importDb.CompareImagesExpanded);
            foreach (var bob in list.OrderBy(p => p.Text))
            {
                if (bob.ExpandCount > 0 && _existingDb.FindExactMatchExpanded(bob) < 0)
                {
                    AddToListView(bob);
                }
                else if (bob.ExpandCount == 0 && _existingDb.FindExactMatch(bob) < 0)
                {
                    AddToListView(bob);
                }
                else
                {
                    existingMatches++;
                }
            }
            listView1.EndUpdate();
            if (listView1.Items.Count > 0)
            {
                listView1.Items[0].Selected = true;
                listView1.Items[0].Focused  = true;
            }
            labelInfo.Text = $"Images found not in current db: {imageList1.Images.Count:#,##0} ({existingMatches:#,##0} matches already in current db)";
        }
Exemplo n.º 2
0
        private void buttonNewCharacterDatabase_Click(object sender, EventArgs e)
        {
            using (var newFolder = new VobSubOcrNewFolder(false))
            {
                if (newFolder.ShowDialog(this) == DialogResult.OK)
                {
                    try
                    {
                        string fileName = Path.Combine(Configuration.OcrDirectory, newFolder.FolderName + ".db");
                        if (File.Exists(fileName))
                        {
                            MessageBox.Show("OCR db already exists!");
                            return;
                        }

                        comboBoxCharacterDatabase.Items.Add(newFolder.FolderName);
                        comboBoxCharacterDatabase.SelectedIndex = comboBoxCharacterDatabase.Items.Count - 1;
                        var binaryOcrDb = new BinaryOcrDb(fileName);
                        binaryOcrDb.Save();
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message);
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void buttonTrain_Click(object sender, EventArgs e)
        {
            var startFontSize = Convert.ToInt32(comboBoxSubtitleFontSize.Items[comboBoxSubtitleFontSize.SelectedIndex].ToString());
            var endFontSize   = Convert.ToInt32(comboBoxFontSizeEnd.Items[comboBoxFontSizeEnd.SelectedIndex].ToString());

            if (!File.Exists(textBoxInputFile.Text))
            {
                MessageBox.Show($"Input file '{textBoxInputFile.Text}' does not exist!");
                return;
            }

            if (!Directory.Exists(textBoxNOcrDb.Text))
            {
                MessageBox.Show($"Output folder '{textBoxNOcrDb.Text}' does not exist!");
                return;
            }

            if (listViewFonts.CheckedItems.Count == 1)
            {
                MessageBox.Show("Please select at least one font!");
                return;
            }

            foreach (ListViewItem fontItem in listViewFonts.CheckedItems)
            {
                _subtitleFontName = fontItem.Text;
                for (_subtitleFontSize = startFontSize; _subtitleFontSize <= endFontSize; _subtitleFontSize++)
                {
                    int numberOfCharactersLeaned  = 0;
                    int numberOfCharactersSkipped = 0;
                    var bicDb  = new BinaryOcrDb(Path.Combine(textBoxNOcrDb.Text, $"{_subtitleFontName}_{_subtitleFontSize}.db"));
                    var lines  = File.ReadAllLines(textBoxInputFile.Text).ToList();
                    var format = new SubRip();
                    var sub    = new Subtitle();
                    format.LoadSubtitle(sub, lines, textBoxInputFile.Text);
                    var charactersLearned = new List <string>();
                    foreach (var 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, bicDb, charactersLearned, s, false, false);
                                    TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, bicDb, charactersLearned, s, false, true);
                                    if (checkBoxBold.Checked)
                                    {
                                        TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, bicDb, charactersLearned, s, true, false);
                                    }
                                }
                            }
                        }
                    }

                    bicDb.Save();
                }
            }
        }
        internal void Initialize(string databaseFolderName, List <VobSubOcr.CompareMatch> matches, List <Bitmap> imageSources, BinaryOcrDb binOcrDb)
        {
            _binOcrDb     = binOcrDb;
            _matches      = matches;
            _imageSources = imageSources;

            if (_binOcrDb == null)
            {
                ImageCompareDocument = new XmlDocument();
                _directoryPath       = Configuration.VobSubCompareFolder + databaseFolderName + Path.DirectorySeparatorChar;
                if (!File.Exists(_directoryPath + "Images.xml"))
                {
                    ImageCompareDocument.LoadXml("<OcrBitmaps></OcrBitmaps>");
                }
                else
                {
                    ImageCompareDocument.Load(_directoryPath + "Images.xml");
                }
            }

            for (int i = 0; i < _matches.Count; i++)
            {
                listBoxInspectItems.Items.Add(_matches[i]);
            }
            if (listBoxInspectItems.Items.Count > 0)
            {
                listBoxInspectItems.SelectedIndex = 0;
            }
            ShowCount();
        }
Exemplo n.º 5
0
        public void TestMethodBinOcrSaveLoad()
        {
            string tempFileName = Path.GetTempFileName();
            var    db           = new BinaryOcrDb(tempFileName);
            var    nbmp         = new NikseBitmap(2, 2);

            nbmp.SetPixel(0, 0, Color.Transparent);
            nbmp.SetPixel(1, 0, Color.Transparent);
            nbmp.SetPixel(1, 0, Color.Transparent);
            nbmp.SetPixel(1, 1, Color.White);

            var bob = new BinaryOcrBitmap(nbmp);

            bob.Text = "Debug";
            db.Add(bob);

            nbmp.SetPixel(0, 0, Color.White);
            var bob2 = new BinaryOcrBitmap(nbmp);

            bob2.X            = 2;
            bob2.Y            = 4;
            bob2.Text         = "tt";
            bob2.Italic       = true;
            bob2.ExpandCount  = 2;
            bob2.ExpandedList = new System.Collections.Generic.List <BinaryOcrBitmap>();
            bob2.ExpandedList.Add(bob2);
            db.Add(bob2);
            db.Save();

            db = new BinaryOcrDb(tempFileName, true);
            Assert.IsTrue(db.CompareImages.Count == 1);
            Assert.IsTrue(db.CompareImagesExpanded.Count == 1);

            Assert.IsTrue(bob.Width == db.CompareImages[0].Width);
            Assert.IsTrue(bob.Height == db.CompareImages[0].Height);
            Assert.IsTrue(bob.NumberOfColoredPixels == db.CompareImages[0].NumberOfColoredPixels);
            Assert.IsTrue(bob.Hash == db.CompareImages[0].Hash);
            Assert.IsTrue(bob.Italic == db.CompareImages[0].Italic);
            Assert.IsTrue(bob.ExpandCount == db.CompareImages[0].ExpandCount);
            Assert.IsTrue(bob.Text == db.CompareImages[0].Text);

            Assert.IsTrue(bob2.Width == db.CompareImagesExpanded[0].Width);
            Assert.IsTrue(bob2.Height == db.CompareImagesExpanded[0].Height);
            Assert.IsTrue(bob2.NumberOfColoredPixels == db.CompareImagesExpanded[0].NumberOfColoredPixels);
            Assert.IsTrue(bob2.Hash == db.CompareImagesExpanded[0].Hash);
            Assert.IsTrue(bob2.Italic == db.CompareImagesExpanded[0].Italic);
            Assert.IsTrue(bob2.ExpandCount == db.CompareImagesExpanded[0].ExpandCount);
            Assert.IsTrue(bob2.Text == db.CompareImagesExpanded[0].Text);
            Assert.IsTrue(bob2.X == db.CompareImagesExpanded[0].X);
            Assert.IsTrue(bob2.Y == db.CompareImagesExpanded[0].Y);

            try
            {
                File.Delete(tempFileName);
            }
            catch
            {
            }
        }
Exemplo n.º 6
0
        internal VobSubEditCharacters(string databaseFolderName, List <VobSubOcr.ImageCompareAddition> additions, BinaryOcrDb binOcrDb)
        {
            UiUtil.PreInitialize(this);
            InitializeComponent();
            UiUtil.FixFonts(this);

            labelExpandCount.Text = string.Empty;
            _binOcrDb             = binOcrDb;
            labelCount.Text       = string.Empty;
            if (additions != null)
            {
                Additions = new List <VobSubOcr.ImageCompareAddition>();
                foreach (var a in additions)
                {
                    Additions.Add(a);
                }

                const int makeHigher = 40;
                labelImageCompareFiles.Top         = labelImageCompareFiles.Top - makeHigher;
                listBoxFileNames.Top               = listBoxFileNames.Top - makeHigher;
                listBoxFileNames.Height            = listBoxFileNames.Height + makeHigher;
                groupBoxCurrentCompareImage.Top    = groupBoxCurrentCompareImage.Top - makeHigher;
                groupBoxCurrentCompareImage.Height = groupBoxCurrentCompareImage.Height + makeHigher;
            }

            labelImageInfo.Text  = string.Empty;
            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;

            _directoryPath = Configuration.VobSubCompareDirectory + databaseFolderName + Path.DirectorySeparatorChar;
            if (!File.Exists(_directoryPath + "Images.xml"))
            {
                _compareDoc.LoadXml("<OcrBitmaps></OcrBitmaps>");
            }
            else
            {
                _compareDoc.Load(_directoryPath + "Images.xml");
            }

            Refill(Additions);

            Text = Configuration.Settings.Language.VobSubEditCharacters.Title;
            labelChooseCharacters.Text        = Configuration.Settings.Language.VobSubEditCharacters.ChooseCharacter;
            labelImageCompareFiles.Text       = Configuration.Settings.Language.VobSubEditCharacters.ImageCompareFiles;
            groupBoxCurrentCompareImage.Text  = Configuration.Settings.Language.VobSubEditCharacters.CurrentCompareImage;
            labelTextAssociatedWithImage.Text = Configuration.Settings.Language.VobSubEditCharacters.TextAssociatedWithImage;
            checkBoxItalic.Text  = Configuration.Settings.Language.VobSubEditCharacters.IsItalic;
            buttonUpdate.Text    = Configuration.Settings.Language.VobSubEditCharacters.Update;
            buttonDelete.Text    = Configuration.Settings.Language.VobSubEditCharacters.Delete;
            labelDoubleSize.Text = Configuration.Settings.Language.VobSubEditCharacters.ImageDoubleSize;
            buttonImport.Text    = Configuration.Settings.Language.SubStationAlphaStyles.Import;
            buttonOK.Text        = Configuration.Settings.Language.General.Ok;
            buttonCancel.Text    = Configuration.Settings.Language.General.Cancel;
            UiUtil.FixLargeFonts(this, buttonOK);
            buttonImport.Visible = binOcrDb != null;
        }
Exemplo n.º 7
0
        public VobSubCharactersImport(BinaryOcrDb binaryOcrDb)
        {
            InitializeComponent();

            _existingDb = binaryOcrDb;

            labelInfo.Text         = string.Empty;
            labelCurrentImage.Text = string.Empty;
            Text = string.Format("Import OCR images into \"{0}\"", Path.GetFileName(_existingDb.FileName));
            buttonCancel.Text = Configuration.Settings.Language.General.Cancel;
        }
Exemplo n.º 8
0
        internal VobSubEditCharacters(List <VobSubOcr.ImageCompareAddition> additions, BinaryOcrDb binOcrDb)
        {
            UiUtil.PreInitialize(this);
            InitializeComponent();
            UiUtil.FixFonts(this);

            labelExpandCount.Text = string.Empty;
            _binOcrDb             = binOcrDb;
            labelCount.Text       = string.Empty;
            if (additions != null)
            {
                Additions = new List <VobSubOcr.ImageCompareAddition>();
                foreach (var a in additions)
                {
                    Additions.Add(a);
                }

                const int makeHigher = 40;
                labelImageCompareFiles.Top         = labelImageCompareFiles.Top - makeHigher;
                listBoxFileNames.Top               = listBoxFileNames.Top - makeHigher;
                listBoxFileNames.Height            = listBoxFileNames.Height + makeHigher;
                groupBoxCurrentCompareImage.Top    = groupBoxCurrentCompareImage.Top - makeHigher;
                groupBoxCurrentCompareImage.Height = groupBoxCurrentCompareImage.Height + makeHigher;
            }

            labelImageInfo.Text  = string.Empty;
            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
            Refill(Additions);

            Text = LanguageSettings.Current.VobSubEditCharacters.Title;
            labelChooseCharacters.Text        = LanguageSettings.Current.VobSubEditCharacters.ChooseCharacter;
            labelImageCompareFiles.Text       = LanguageSettings.Current.VobSubEditCharacters.ImageCompareFiles;
            groupBoxCurrentCompareImage.Text  = LanguageSettings.Current.VobSubEditCharacters.CurrentCompareImage;
            labelTextAssociatedWithImage.Text = LanguageSettings.Current.VobSubEditCharacters.TextAssociatedWithImage;
            checkBoxItalic.Text  = LanguageSettings.Current.VobSubEditCharacters.IsItalic;
            buttonUpdate.Text    = LanguageSettings.Current.VobSubEditCharacters.Update;
            buttonDelete.Text    = LanguageSettings.Current.VobSubEditCharacters.Delete;
            labelDoubleSize.Text = LanguageSettings.Current.VobSubEditCharacters.ImageDoubleSize;
            buttonImport.Text    = LanguageSettings.Current.SubStationAlphaStyles.Import;
            buttonOK.Text        = LanguageSettings.Current.General.Ok;
            buttonCancel.Text    = LanguageSettings.Current.General.Cancel;
            UiUtil.FixLargeFonts(this, buttonOK);
            buttonImport.Visible = binOcrDb != null;

            foreach (ToolStripItem toolStripItem in contextMenuStripLetters.Items)
            {
                if (toolStripItem is ToolStripDropDownItem i && i.HasDropDownItems)
                {
                    foreach (ToolStripItem item in i.DropDownItems)
                    {
                        item.Click += InsertLanguageCharacter;
                    }
                }
Exemplo n.º 9
0
        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 VobSubCharactersImport(BinaryOcrDb binaryOcrDb)
        {
            UiUtil.PreInitialize(this);
            InitializeComponent();
            UiUtil.FixFonts(this);

            _existingDb = binaryOcrDb;

            labelInfo.Text         = string.Empty;
            labelCurrentImage.Text = string.Empty;
            Text = $"Import OCR images into \"{Path.GetFileName(_existingDb.FileName)}\"";
            buttonCancel.Text = Configuration.Settings.Language.General.Cancel;
        }
Exemplo n.º 11
0
        private void buttonEditCharacterDatabase_Click(object sender, EventArgs e)
        {
            var fileName    = Path.Combine(Configuration.OcrDirectory, comboBoxCharacterDatabase.SelectedItem + ".db");
            var binaryOcrDb = new BinaryOcrDb(fileName);

            binaryOcrDb.LoadCompareImages();
            using (var formVobSubEditCharacters = new VobSubEditCharacters(null, binaryOcrDb))
            {
                if (formVobSubEditCharacters.ShowDialog() == DialogResult.OK)
                {
                    binaryOcrDb.Save();
                }
            }
        }
Exemplo n.º 12
0
        public void TestMethodBinOcrSaveLoadTestExceptions()
        {
            string tempFileName = FileUtil.GetTempFileName(".db");
            var    db           = new BinaryOcrDb(tempFileName);
            var    nbmp         = new NikseBitmap(2, 2);

            nbmp.SetPixel(0, 0, Color.Transparent);
            nbmp.SetPixel(1, 0, Color.Transparent);
            nbmp.SetPixel(1, 0, Color.Transparent);
            nbmp.SetPixel(1, 1, Color.White);

            var bob = new BinaryOcrBitmap(nbmp);

            bob.Text = "S";
            db.Add(bob);

            nbmp.SetPixel(0, 0, Color.White);
            var bob2 = new BinaryOcrBitmap(nbmp);

            bob2.X            = 2;
            bob2.Y            = 4;
            bob2.Text         = null;
            bob2.Italic       = true;
            bob2.ExpandCount  = 3;
            bob2.ExpandedList = new System.Collections.Generic.List <BinaryOcrBitmap>();
            bob2.ExpandedList.Add(bob2);
            try
            {
                db.Add(bob2);
            }
            catch
            {
                return;
            }
            Assert.Fail();

            try
            {
                File.Delete(tempFileName);
            }
            catch
            {
            }
        }
        internal void Initialize(string databaseFolderName, List <VobSubOcr.CompareMatch> matches, List <Bitmap> imageSources, BinaryOcrDb binOcrDb, List <ImageSplitterItem> splitterItems)
        {
            _binOcrDb        = binOcrDb;
            _matches         = matches;
            _imageSources    = imageSources;
            _splitterItems   = splitterItems;
            DeleteMultiMatch = false;

            listBoxInspectItems.Items.Clear();
            if (_binOcrDb == null)
            {
                ImageCompareDocument = new XmlDocument();
                _directoryPath       = Configuration.VobSubCompareDirectory + databaseFolderName + Path.DirectorySeparatorChar;
                if (!File.Exists(_directoryPath + "Images.xml"))
                {
                    ImageCompareDocument.LoadXml("<OcrBitmaps></OcrBitmaps>");
                }
                else
                {
                    ImageCompareDocument.Load(_directoryPath + "Images.xml");
                }
            }

            SyncListBoxToMatches();

            if (LastIndex > listBoxInspectItems.Items.Count)
            {
                LastIndex = listBoxInspectItems.Items.Count - 1;
            }

            if (listBoxInspectItems.Items.Count > 0)
            {
                listBoxInspectItems.SelectedIndex = LastIndex;
            }

            ShowCount();
        }
Exemplo n.º 14
0
        private void buttonTrain_Click(object sender, EventArgs e)
        {
            saveFileDialog1.DefaultExt = ".db";
            saveFileDialog1.Filter     = "*Binary Image Compare DB files|*.db";
            if (saveFileDialog1.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            var startFontSize = Convert.ToInt32(comboBoxSubtitleFontSize.Items[comboBoxSubtitleFontSize.SelectedIndex].ToString());
            var endFontSize   = Convert.ToInt32(comboBoxFontSizeEnd.Items[comboBoxFontSizeEnd.SelectedIndex].ToString());

            if (!File.Exists(textBoxInputFile.Text))
            {
                MessageBox.Show($"Input file '{textBoxInputFile.Text}' does not exist!");
                return;
            }

            if (listViewFonts.CheckedItems.Count == 1)
            {
                MessageBox.Show("Please select at least one font!");
                return;
            }

            var bicDb = new BinaryOcrDb(saveFileDialog1.FileName);
            int numberOfCharactersLeaned  = 0;
            int numberOfCharactersSkipped = 0;

            foreach (ListViewItem fontItem in listViewFonts.CheckedItems)
            {
                _subtitleFontName = fontItem.Text;
                for (_subtitleFontSize = startFontSize; _subtitleFontSize <= endFontSize; _subtitleFontSize++)
                {
                    var lines  = File.ReadAllLines(textBoxInputFile.Text).ToList();
                    var format = new SubRip();
                    var sub    = new Subtitle();
                    format.LoadSubtitle(sub, lines, textBoxInputFile.Text);
                    var charactersLearned = new List <string>();
                    foreach (var 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, bicDb, charactersLearned, s, false, false);
                                    TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, bicDb, charactersLearned, s, false, true);
                                    if (checkBoxBold.Checked)
                                    {
                                        TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, bicDb, charactersLearned, s, true, false);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            bicDb.Save();
            labelInfo.Text = "Training completed and saved in " + saveFileDialog1.FileName;
        }
Exemplo n.º 15
0
        private void buttonTrain_Click(object sender, EventArgs e)
        {
            if (buttonTrain.Text == LanguageSettings.Current.SpellCheck.Abort)
            {
                _abort = true;
                return;
            }

            _abort           = false;
            buttonTrain.Text = LanguageSettings.Current.SpellCheck.Abort;
            buttonOK.Enabled = false;

            saveFileDialog1.DefaultExt = ".db";
            saveFileDialog1.Filter     = "*Binary Image Compare DB files|*.db";
            if (saveFileDialog1.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            buttonTrain.Text = LanguageSettings.Current.SpellCheck.Abort;
            var startFontSize = Convert.ToInt32(comboBoxSubtitleFontSize.Items[comboBoxSubtitleFontSize.SelectedIndex].ToString());
            var endFontSize   = Convert.ToInt32(comboBoxFontSizeEnd.Items[comboBoxFontSizeEnd.SelectedIndex].ToString());

            if (!File.Exists(textBoxInputFile.Text))
            {
                MessageBox.Show($"Input file '{textBoxInputFile.Text}' does not exist!");
                return;
            }

            if (listViewFonts.CheckedItems.Count == 0)
            {
                MessageBox.Show("Please select at least one font!");
                return;
            }

            var bicDb = new BinaryOcrDb(saveFileDialog1.FileName);
            int numberOfCharactersLearned = 0;
            int numberOfCharactersSkipped = 0;

            foreach (ListViewItem fontItem in listViewFonts.CheckedItems)
            {
                _subtitleFontName = fontItem.Text;
                for (_subtitleFontSize = startFontSize; _subtitleFontSize <= endFontSize; _subtitleFontSize++)
                {
                    var lines  = File.ReadAllLines(textBoxInputFile.Text).ToList();
                    var format = new SubRip();
                    var sub    = new Subtitle();
                    format.LoadSubtitle(sub, lines, textBoxInputFile.Text);
                    var charactersLearned = new List <string>();
                    foreach (var p in sub.Paragraphs)
                    {
                        labelInfo.Refresh();
                        Application.DoEvents();
                        foreach (char ch in p.Text)
                        {
                            if (!char.IsWhiteSpace(ch))
                            {
                                var s = ch.ToString();
                                if (!charactersLearned.Contains(s))
                                {
                                    charactersLearned.Add(s);
                                    TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, s, false, false);
                                    if (checkBoxBold.Checked)
                                    {
                                        TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, s, true, false);
                                    }
                                    if (checkBoxItalic.Checked)
                                    {
                                        TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, s, false, true);
                                    }
                                }
                                labelInfo.Text = string.Format(LanguageSettings.Current.VobSubOcr.NowTraining, numberOfCharactersLearned, _subtitleFontName, numberOfCharactersSkipped);
                            }
                        }

                        foreach (var text in textBoxMerged.Text.Split(' '))
                        {
                            if (!string.IsNullOrWhiteSpace(text))
                            {
                                if (!charactersLearned.Contains(text) && text.Length > 1 && text.Length <= 3)
                                {
                                    charactersLearned.Add(text);
                                    TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, text, false, false);
                                    if (checkBoxBold.Checked)
                                    {
                                        TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, text, true, false);
                                    }
                                    if (checkBoxItalic.Checked)
                                    {
                                        TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, text, false, true);
                                    }
                                }
                            }
                        }

                        if (_abort)
                        {
                            break;
                        }
                    }
                    if (_abort)
                    {
                        break;
                    }
                }

                if (_abort)
                {
                    break;
                }
            }
            bicDb.Save();
            if (_abort)
            {
                labelInfo.Text = "Partially (aborted) training completed and saved in " + saveFileDialog1.FileName;
            }
            else
            {
                labelInfo.Text = "Training completed and saved in " + saveFileDialog1.FileName;
            }
            buttonOK.Enabled = true;
            buttonTrain.Text = "Start training";
            _abort           = false;
        }
Exemplo n.º 16
0
        private void BinaryOcrTrain_Shown(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_autoDetectFontText))
            {
                return;
            }

            BinaryOcrTrain_ResizeEnd(sender, e);
            SelectAll_Click(this, EventArgs.Empty);
            int numberOfCharactersLearned = 0;
            int numberOfCharactersSkipped = 0;

            foreach (ListViewItem fontItem in listViewFonts.CheckedItems)
            {
                _subtitleFontName = fontItem.Text;
                labelInfo.Text    = $"Checking font '{_subtitleFontName}'... {AutoDetectedFonts.Count} hits";
                labelInfo.Refresh();
                Application.DoEvents();

                for (_subtitleFontSize = 20; _subtitleFontSize <= 100; _subtitleFontSize++)
                {
                    if (!string.IsNullOrEmpty(_autoDetectFontText))
                    {
                        var s     = _autoDetectFontText;
                        var bicDb = new BinaryOcrDb(null);
                        TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, s, false, false);
                        if (bicDb.FindExactMatch(_autoDetectFontBob) >= 0)
                        {
                            AutoDetectedFonts.Add(_subtitleFontName + " " + _subtitleFontSize);
                        }
                        else
                        {
                            // allow for error %
                            var smallestDifference = int.MaxValue;
                            foreach (var compareItem in bicDb.CompareImages)
                            {
                                if (compareItem.Width == _autoDetectFontBob.Width && compareItem.Height == _autoDetectFontBob.Height) // precise math in size
                                {
                                    if (Math.Abs(compareItem.NumberOfColoredPixels - _autoDetectFontBob.NumberOfColoredPixels) < 3)
                                    {
                                        int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, _autoDetectFontBob);
                                        if (dif < smallestDifference)
                                        {
                                            if (!BinaryOcrDb.AllowEqual(compareItem, _autoDetectFontBob))
                                            {
                                                continue;
                                            }

                                            smallestDifference = dif;
                                            if (dif < 3)
                                            {
                                                break; // foreach ending
                                            }
                                        }
                                    }
                                }
                            }

                            if (smallestDifference < 13)
                            {
                                AutoDetectedFonts.Add($"{_subtitleFontName} {_subtitleFontSize} - diff={smallestDifference}");
                            }
                        }

                        bicDb = new BinaryOcrDb(null);
                        TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, s, false, true);
                        if (bicDb.FindExactMatch(_autoDetectFontBob) >= 0)
                        {
                            AutoDetectedFonts.Add(_subtitleFontName + " " + _subtitleFontSize + " italic");
                        }

                        if (checkBoxBold.Checked)
                        {
                            bicDb = new BinaryOcrDb(null);
                            TrainLetter(ref numberOfCharactersLearned, ref numberOfCharactersSkipped, bicDb, s, true, false);
                            if (bicDb.FindExactMatch(_autoDetectFontBob) >= 0)
                            {
                                AutoDetectedFonts.Add(_subtitleFontName + " " + _subtitleFontSize + " bold");
                            }
                        }
                    }
                }
            }

            DialogResult = DialogResult.OK;
        }
Exemplo n.º 17
0
        private void TrainLetter(ref int numberOfCharactersLeaned, ref int numberOfCharactersSkipped, BinaryOcrDb db, List <string> charactersLearned, string s, bool bold, bool italic)
        {
            Bitmap bmp  = GenerateImageFromTextWithStyle(s, bold, italic);
            var    nbmp = new NikseBitmap(bmp);

            nbmp.MakeTwoColor(280);
            var list = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, 10, false, false, 25);

            if (list.Count == 1)
            {
                var match = db.FindExactMatch(new BinaryOcrBitmap(list[0].NikseBitmap));
                if (match < 0)
                {
                    pictureBox1.Image = list[0].NikseBitmap.GetBitmap();
                    labelInfo.Refresh();
                    Application.DoEvents();
                    db.Add(new BinaryOcrBitmap(list[0].NikseBitmap, false, 0, s, 0, 0));
                    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++;
                }
            }
        }
Exemplo n.º 18
0
        private void TrainLetter(ref int numberOfCharactersLearned, ref int numberOfCharactersSkipped, BinaryOcrDb db, string s, bool bold, bool italic)
        {
            var bmp  = GenerateImageFromTextWithStyle("H  " + s, bold, italic);
            var nbmp = new NikseBitmap(bmp);

            nbmp.MakeTwoColor(280);
            var list = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, 10, false, false, 25, false);

            if (list.Count == 3)
            {
                var item  = list[2];
                var bob   = new BinaryOcrBitmap(item.NikseBitmap, italic, 0, s, item.X, item.Y);
                var match = db.FindExactMatch(bob);
                if (match < 0)
                {
                    db.Add(bob);
                    numberOfCharactersLearned++;
                    bmp.Dispose();
                }
                else
                {
                    numberOfCharactersSkipped++;
                }
            }
        }