Ejemplo n.º 1
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++;
                }
            }
        }
Ejemplo n.º 2
0
        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(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++;
                }
            }
        }
Ejemplo n.º 3
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++;
                }
            }
        }
Ejemplo n.º 4
0
        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++;
            }
        }
Ejemplo n.º 5
0
        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++;
            }
        }
Ejemplo n.º 6
0
        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++;
                }
            }
        }
Ejemplo n.º 7
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;
        }
Ejemplo n.º 8
0
        private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            labelImageInfo.Text   = string.Empty;
            labelExpandCount.Text = string.Empty;

            if (listBoxInspectItems.SelectedIndex < 0)
            {
                return;
            }

            _selectedCompareNode            = null;
            _selectedCompareBinaryOcrBitmap = null;

            var img = _imageSources[listBoxInspectItems.SelectedIndex];

            pictureBoxInspectItem.Image = img;
            if (img != null)
            {
                pictureBoxInspectItem.Width  = img.Width + 2;
                pictureBoxInspectItem.Height = img.Height + 2;
            }

            pictureBoxCompareBitmap.Image       = null;
            pictureBoxCompareBitmapDouble.Image = null;

            int index = listBoxInspectItems.SelectedIndex;
            var match = _matches[index];

            _selectedMatch = match;
            if (!string.IsNullOrEmpty(match.Name))
            {
                if (_binOcrDb != null)
                {
                    bool bobFound = false;
                    foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImages)
                    {
                        if (match.Name == bob.Key)
                        {
                            textBoxText.Text                = bob.Text;
                            checkBoxItalic.Checked          = bob.Italic;
                            _selectedCompareBinaryOcrBitmap = bob;
                            SetDbItemView(bob.ToOldBitmap());
                            var matchBob = new BinaryOcrBitmap(new NikseBitmap(_imageSources[listBoxInspectItems.SelectedIndex]));
                            if (matchBob.Hash == bob.Hash && matchBob.Width == bob.Width && matchBob.Height == bob.Height && matchBob.NumberOfColoredPixels == bob.NumberOfColoredPixels)
                            {
                                buttonAddBetterMatch.Enabled = false; // exact match
                            }
                            else
                            {
                                buttonAddBetterMatch.Enabled = true;
                            }
                            bobFound = true;
                            break;
                        }
                    }
                    if (!bobFound)
                    {
                        foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImagesExpanded)
                        {
                            if (match.Name == bob.Key)
                            {
                                textBoxText.Text                = bob.Text;
                                checkBoxItalic.Checked          = bob.Italic;
                                _selectedCompareBinaryOcrBitmap = bob;

                                var oldBitmap = bob.ToOldBitmap();
                                SetDbItemView(oldBitmap);

                                int dif = 1;
                                if (oldBitmap.Width == match.ImageSplitterItem.NikseBitmap.Width && oldBitmap.Height == match.ImageSplitterItem.NikseBitmap.Height)
                                {
                                    dif = NikseBitmapImageSplitter.IsBitmapsAlike(match.ImageSplitterItem.NikseBitmap, oldBitmap);
                                }
                                buttonAddBetterMatch.Enabled = dif > 0; // if exact match then don't allow "Add better match"
                                labelExpandCount.Text        = $"Expand count: {bob.ExpandCount}";
                                break;
                            }
                        }
                    }
                }
                else
                {
                    foreach (XmlNode node in ImageCompareDocument.DocumentElement.ChildNodes)
                    {
                        if (node.Attributes["Text"] != null && node.InnerText == match.Name)
                        {
                            string text = node.Attributes["Text"].InnerText;
                            textBoxText.Text       = text;
                            checkBoxItalic.Checked = node.Attributes["Italic"] != null;
                            string databaseName = Path.Combine(_directoryPath, "Images.db");
                            using (var f = new FileStream(databaseName, FileMode.Open))
                            {
                                try
                                {
                                    string name = node.InnerText;
                                    int    pos  = Convert.ToInt32(name);
                                    f.Position = pos;
                                    var mbmp   = new ManagedBitmap(f);
                                    var bitmap = mbmp.ToOldBitmap();
                                    SetDbItemView(mbmp.ToOldBitmap());
                                    labelImageInfo.Text = string.Format(Configuration.Settings.Language.VobSubEditCharacters.Image + " - {0}x{1}", bitmap.Width, bitmap.Height);
                                }
                                catch (Exception exception)
                                {
                                    labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.Image;
                                    MessageBox.Show(exception.Message);
                                }
                            }

                            _selectedCompareNode = node;
                            break;
                        }
                    }
                }
            }

            buttonAddBetterMatch.Text = Configuration.Settings.Language.VobSubOcrCharacterInspect.AddBetterMatch;
            if (_selectedMatch.Text == Configuration.Settings.Language.VobSubOcr.NoMatch)
            {
                buttonUpdate.Enabled                  = false;
                buttonDelete.Enabled                  = false;
                buttonAddBetterMatch.Enabled          = true;
                buttonAddBetterMatch.Text             = Configuration.Settings.Language.VobSubOcrCharacterInspect.Add;
                textBoxText.Enabled                   = true;
                textBoxText.Text                      = string.Empty;
                checkBoxItalic.Enabled                = true;
                pictureBoxCompareBitmap.Visible       = true;
                pictureBoxCompareBitmapDouble.Visible = true;
                labelDoubleSize.Visible               = true;
            }
            else if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null)
            {
                buttonUpdate.Enabled                  = false;
                buttonDelete.Enabled                  = false;
                buttonAddBetterMatch.Enabled          = true;
                textBoxText.Enabled                   = true;
                textBoxText.Text                      = string.Empty;
                checkBoxItalic.Enabled                = false;
                pictureBoxCompareBitmap.Visible       = false;
                pictureBoxCompareBitmapDouble.Visible = false;
                labelDoubleSize.Visible               = false;
                if (img == null)
                {
                    buttonAddBetterMatch.Enabled = false;
                }
            }
            else
            {
                buttonUpdate.Enabled = true;
                buttonDelete.Enabled = true;
                if (_selectedCompareNode != null)
                {
                    buttonAddBetterMatch.Enabled = true;
                }
                textBoxText.Enabled                   = true;
                checkBoxItalic.Enabled                = true;
                pictureBoxCompareBitmap.Visible       = true;
                pictureBoxCompareBitmapDouble.Visible = true;
                labelDoubleSize.Visible               = true;
            }
        }
Ejemplo n.º 9
0
        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++;
            }
        }