拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 荧屏光学字符识别

荧屏光学字符识别

白鹭 - 2022-02-11 2373 0 0

我正在尝试决议数字和荧屏上的一些字母。网上有很多主题,但 99% 都是过时的,而且他们都在问如何从影像中获取文本(我不需要)。就我而言,我只想要一个透明的图片框,可以扫描下面的任何文本。我找到了一个很 老的youtube教程

写在生物上的代码是


Imports Emgu.CV
Imports Emgu.Util
Imports Emgu.CV.OCR
Imports Emgu.CV.Structure
   
Public Class Form1
   
Dim OCRz As Tesseract = New Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_ONLY)
Dim pic As Bitmap = New Bitmap(270, 100)
Dim gfx As Graphics = Graphics.FromImage(pic)
   
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
   
'If Windows XP
gfx.CopyFromScreen(New Point(Me.Location.X   PictureBox1.Location.X   4, Me.Location.Y   PictureBox1.Location.Y   30), New Point(0, 0), pic.Size)
PictureBox1.Image = pic
   
'If Windows 7
'gfx.CopyFromScreen(MousePositi-on, New Point(0, 0), pic.Size)
   
End Sub
   
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   
OCRz.Recognize(New Image(Of Bgr, Byte)(pic))
RichTextBox1.Text = OCRz.GetText
   
End Sub
End Class

这是一个 8 y 的旧代码,所以我认为一定有更好的东西。我从 nuget 下载了 emgu.cv,这是第一个也是下载次数最多的软件包,但在运行时出现错误“'无法使用路径 'tessdata' 和语言 'eng' 创建 ocr 模型。'”在编译时我收到错误“' Tesseract' 未定义。”。这真的很令人沮丧,因为我到处寻找,也在 c# 论坛中寻找,但没有一个可以帮助我。你有什么解决办法吗?我会很感激你的帮助。谢谢

uj5u.com热心网友回复:

那个代码很旧,我觉得它不能正常作业。你考虑过Windows.Media.OCR吗? 需要Windows v.10 SDK

使用的控制元件:

  • 1 个透明图片框(使用与表单透明键相同的背景色,我使用的是灰色)
  • 1 个富文本框
  • 1 个按钮

添加作为参考:

Imports Windows.Media.Ocr
Imports System.IO
Imports System.Runtime.InteropServices.WindowsRuntime
Public Class Form1
    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim softwareBmp As Windows.Graphics.Imaging.SoftwareBitmap
        Using bmp As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
            Using g As Graphics = Graphics.FromImage(bmp)
                Dim pt As Point = Me.PointToScreen(New Point(PictureBox1.Left, PictureBox1.Top))
                g.CopyFromScreen(pt.X, pt.Y, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy)
                Using memStream = New Windows.Storage.Streams.InMemoryRandomAccessStream()
                    bmp.Save(memStream.AsStream(), System.Drawing.Imaging.ImageFormat.Bmp)
                    Dim decoder As Windows.Graphics.Imaging.BitmapDecoder = Await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(memStream)
                    softwareBmp = Await decoder.GetSoftwareBitmapAsync()
                End Using
            End Using
        End Using

        Dim ocrEng = OcrEngine.TryCreateFromUserProfileLanguages()
        'If you want to scan only letters, set the language
        'Dim ocrEng = OcrEngine.TryCreateFromLanguage(New Windows.Globalization.Language("en-US"))

        
        Dim languages As IReadOnlyList(Of Windows.Globalization.Language) = ocrEng.AvailableRecognizerLanguages
        For Each language In languages
            Console.WriteLine(language.LanguageTag)
        Next
        Dim r = ocrEng.RecognizerLanguage
        Dim n = ocrEng.MaxImageDimension

        Dim ocrResult = Await ocrEng.RecognizeAsync(softwareBmp)
        RichTextBox1.Text = ocrResult.Text
        
        'Follow lines are just to test how the OCR engine has cutted the lines from the whole text
         Dim lines As IReadOnlyList(Of OcrLine) = ocrResult.Lines
        For Each line In lines
        Console.WriteLine(line.Text)
          Next
    End Sub
End Class
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *