我正在使用 Windows.Media.OCR 引擎來掃描這兩行

但是軟體會這樣掃描它們:

雖然我希望它掃描如下:
KIBA/USDT 0.00003826 6.31M KIBA 241.68459400 USDT
KIBA/USDT 0.00003470 17.13M KIBA 594.48387000 USDT
我正在使用的代碼是:
'require references: "C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd"
'"C:\ProgramFiles(x86)\ReferenceAssemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll"
' and windows 10 sdk
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.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
End Sub
End Class
為了按行而不是按列掃描,此代碼需要進行哪種更改?
編輯:二進制:
但我之前沒有發布它,因為無論如何我只需要從 0.000038 ecc 掃描到 0.0000%
uj5u.com熱心網友回復:
我選擇對輸出字串進行操作,而不是處理 OCR API。如果可能的話,在 OCR API 中修復問題可能是一個更好的解決方案,但我無法在我的系統中正確參考您的代碼。
所以你可以添加這個函式來轉置字串
Private Function transpose(input As String) As String
Dim numberOfColumns = 4 ' this must be known and could be a parameter to this function
Dim fixedInput = input.Replace(" KIBA", "|KIBA").Replace(" USDT", "|USDT")
Dim splitInput = fixedInput.Split(" "c)
Dim numberOfWords = splitInput.Count()
Dim numberOfRows = numberOfWords / numberOfColumns
Dim words As New List(Of String)()
For row = 0 To numberOfRows - 1
For col = 0 To numberOfColumns - 1
words.Add(splitInput(CInt(row numberOfRows * col)))
Next
Next
Dim sb As New System.Text.StringBuilder()
For i = 0 To words.Count() - 1
sb.Append(words(i).Replace("|", " "))
If (i <> words.Count() - 1) Then
sb.Append(If((i 1) Mod numberOfColumns = 0, Environment.NewLine, vbTab))
End If
Next
Return sb.ToString()
End Function
只需通過它傳遞您的 ocr 輸出字串。在這里,它在您的代碼中被呼叫
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.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 = transpose(ocrResult.Text)
End Sub
我用這個功能測驗過
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim input = "0.00003599 0.00003599 104.1K KIBA 23.22M KIBA 3.74655900 USDT 835.89654200 USDT 0.0000% 0.0000%"
Dim output = transpose(input)
End Sub
輸入:
0.00003599 0.00003599 104.1K KIBA 23.22M KIBA 3.74655900 USDT 835.89654200 USDT 0.0000% 0.0000%
輸出:
0.00003599 104.1K KIBA 3.74655900 USDT 0.0000%
0.00003599 23.22M KIBA 835.89654200 USDT 0.0000%
注意,你需要修復你的字串,通過用 管道替換空格來臨時用多個單詞替換任何句子,|這樣它們就不會被分割,如果你遇到更多這樣的例子,你可以Replace根據代碼繼續添加。如果管道被證明是有效字符,請將其替換為您永遠不會看到的其他字符。
Dim fixedInput = input.Replace(" KIBA", "|KIBA").Replace(" USDT", "|USDT")
...
sb.Append(words(i).Replace("|", " "))
另一種解決方案,再次通過轉置處理不正確的字串,但這次輸出將是一個您可以使用的類。
創建一個類來表示您的資料
Public Class KibaClass
Public Property Price As Decimal
Public Property VolumeKIBA As Decimal
Public Property VolumeUSDT As Decimal
Public Property Percent As Decimal
End Class
以及決議為此類的不同函式
Private Function transposeToClass(input As String) As IEnumerable(Of KibaClass)
Dim numberOfColumns = 4
Dim fixedInput = input.Replace(" KIBA", "|KIBA").Replace(" USDT", "|USDT").Trim()
Dim splitInput = fixedInput.Split(" "c)
Dim numberOfWords = splitInput.Count()
Dim numberOfRows = numberOfWords / numberOfColumns ' 2
Dim words As New List(Of String)()
For row = 0 To numberOfRows - 1
For col = 0 To numberOfColumns - 1
words.Add(splitInput(CInt(row numberOfRows * col)))
Next
Next
Dim kibas As New List(Of KibaClass)()
For row = 0 To numberOfRows - 1
Dim rowOffset = CInt(row * numberOfColumns)
Dim kiba = New KibaClass With {
.Percent = CDec(words(3 rowOffset).Replace("%", "")) / 100,
.Price = CDec(words(0 rowOffset))}
Dim multiplier As Double
Dim splitVolume = words(1 rowOffset).Split("|"c)(0)
Dim lastChar = Convert.ToChar(splitVolume.Last())
Dim volume = splitVolume
If Not Char.IsDigit(lastChar) Then
volume = splitVolume.Substring(0, splitVolume.Length - 1)
Select Case lastChar.ToString().ToUpper()
Case "T"
multiplier = 1000000000.0
Case "M"
multiplier = 1000000.0
Case "K"
multiplier = 1000.0
Case Else
multiplier = 1.0
End Select
End If
kiba.VolumeKIBA = CDec(CDbl(volume) * multiplier)
splitVolume = words(2 rowOffset).Split("|"c)(0)
lastChar = Convert.ToChar(splitVolume.Last())
volume = splitVolume
If Not Char.IsDigit(lastChar) Then
volume = splitVolume.Substring(0, splitVolume.Length - 1)
Select Case lastChar.ToString().ToUpper()
Case "T"
multiplier = 1000000000.0
Case "M"
multiplier = 1000000.0
Case "K"
multiplier = 1000.0
Case Else
multiplier = 1.0
End Select
End If
kiba.VolumeUSDT = CDec(CDbl(volume) * multiplier)
kibas.Add(kiba)
Next
Return kibas
End Function
Dim output1 = transposeToClass(input)
這包含您的類的 IEnumberable,您可以將其列舉到該物件的多個實體中,其屬性以正確的格式表示您最初 OCR 的列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/405792.html
標籤:
