Initializing a Tesseract - vb.net

I try to get some text of an image by using OCR. I have to initialize a Tesseract for that and this was my try:
Imports Emgu.CV
Imports Emgu.Util
Imports Emgu.CV.Structure
Imports Emgu.CV.OCR
Imports Emgu.CV.UI
Imports Emgu.CV.CvEnum
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lolProcess() As Process = Process.GetProcessesByName("lolClient")
Dim snap As New CScreenFromWindow
Dim OCRz As Tesseract = New Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_ONLY)
OCRz.Recognize(New Image(Of Bgr, Byte)(snap.GetFromAll(lolProcess(0))))
MsgBox(OCRz.GetText())
End Sub
End Class
When launching my code I get the following error:
"System.TypeInitializationException"
"Emgu.CV.OCR.Tesseract" caused an exception
I have googled a lot, but can't find my mistake. I've downloaded EMGU from this link and installed the .exe. Then I added every .dll from the bin-directory as a reference to my project. I also added all opencv_XXXXX.dll-files to my project. Then I also added the tessdata-directory to my project. I've marked all the added dlls as "Always copy to output-directory".
There are opencv_XXXXXX.dll-files for x86 and x64. I tried to swap them to x64 but those also don't work.
Does anybody see my mistake?
This are the error-messages and stack-traces:
System.ArgumentException: Unable to create ocr model using Path tessdata and language eng.
bei Emgu.CV.OCR.Tesseract.Init(String dataPath, String language, OcrEngineMode mode) in c:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\Emgu.CV.OCR\Tesseract.cs:Zeile 226.
bei Emgu.CV.OCR.Tesseract..ctor(String dataPath, String language, OcrEngineMode mode) in c:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\Emgu.CV.OCR\Tesseract.cs:Zeile 118.
bei Dodgemaster.Form1.Button1_Click(Object sender, EventArgs e) in X:\Dokumente\Visual Studio 2013\Projects\Dodgemaster\Dodgemaster\Form1.vb:Zeile 16.
A Hello World test-programm works fine. So it can't be something wrong with the references, right?

When you have referenced the .dlls try changing the "copy to output directory" value in the properties to "Copy always"
Also try running it on 3.5 .NET framework.
Failing that try following this:
https://www.youtube.com/watch?v=RqvvXJXuRYY&list=UUxAnMtjN08ryThpgYTBmILg
Really helpful tutorial.

After several reinstalls of different emgu-installs I finally found one, that works. It's an older version, not the current one, but it is for x64. Having in mind that EVERY needed file/dll HAS TO be for x64 and working excactly as in the video, I finally got it work.
Seemed to be an x86/x64 issue.

You have to change the DPath from "tessdata" to "".
Dim OCRz As Tesseract = New Tesseract("", "eng", OcrEngineMode.TesseractOnly)
That's how I got mine to work.(libemgucv-windows-universal-cuda-3.0.0.2158)

Related

Cropping a PDF to png using Ghostscript.net

I'm using visual studio 2015 with VB language Web application, my issue in brief I used Ghostscript to extract pdf first page to png its work fine but if pdf file name contain space its does not extract png and no error show up or if written on other language the error " Page number is not in pages number range!".
I appreciate any help.
test.pdf ---> work fine
new york.pdf --->nothing happen no error
show up
pdf file name not written in English --->error "Page number is
not in pages number range!"
code is
Imports Ghostscript.NET
Imports Ghostscript.NET.Rasterizer
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Drawing
Partial Class Default6
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim inputPdfPath As String = "d:\test\test.pdf"
Dim outputPath As String = "d:\test\"
Using Rasterizer = New GhostscriptRasterizer
Rasterizer.CustomSwitches.Add("-dUseTrimBox")
Rasterizer.CustomSwitches.Add("-g683x960")
Rasterizer.CustomSwitches.Add("-f")
Rasterizer.Open(inputPdfPath)
Dim pageFilePath As String = Path.Combine(outputPath, "Page1" + ".jpg")
Dim img As Image = Rasterizer.GetPage(100, 1)
img.Save(pageFilePath, ImageFormat.Png)
Console.WriteLine(pageFilePath)
End Using
End Sub
End Class
First posted in the comments section, I'm including information as answer, as it worked for the user.
Here is a link useful to solve the blank spaces trouble:
https://www.vbforums.com/showthread.php?703121-RESOLVED-How-to-Shell-from-a-directory-with-spaces-in-path
For non-english characters, please try with the following approach:
Illegal characters in path (Chinese characters)
If the last solution does not work, another alternative is to loop through files in directory and identify the one you are looking for by another meta-data ou file info. In this case, please check this link:
vb.net how to loop through a directory listing?

Getting 'BadImageFormatException' when loading libsodium.dll in Visual Studio 17

I have the libsodium-net NuGet loaded into my PoC solution and have the 32-bit and 64-bit libsodium.dll files in their respective directories (System32 and SysWOW64). Whenever I go to run the program in debug mode, I get the BadImageFormatException error. Is it enough to have the libsodium.dll files in their respective System directories and I'm just missing something else, or do I need to have copies of those dll files in my solution as well?
I have tried compiling using both x86 and x64 build configurations since I read here: BadImageFormatException during .Net assembly load issue that, that could be the cause of this error but I still got the same error in both areas.
I also tried the answer I found here: How to include libsodium.net on ASP.NET for incorporating libsodium-net into VS, where it said to install Visual C++ Redistributable 2015 as well, but the problem there is when I tried that, I got an error from the installer stating that it was already installed on my computer. When I checked though, all I had were the 2008, 2013 and 2017 versions, not 2015, so I'm still at a loss on why I couldn't install the 2015 Redistributable but that's for another time.
Here's my procedure and at least according to intellisense, everything here's hooked up correctly.
Option Explicit On
Imports Sodium
Imports System.Text
Public Class Form1
Dim textToEncrypt, decrypted As String
Shared encoder As New UTF8Encoding
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim key, hashedBytes, salt As Byte()
textToEncrypt = TextBox1.Text
Dim textBytes As Byte() = encoder.GetBytes(textToEncrypt)
hashedBytes = CryptoHash.Sha512(textToEncrypt)
salt = PasswordHash.ScryptGenerateSalt()
key = SecretBox.GenerateKey()
Dim passEnc As Byte() = PasswordHash.ScryptHashBinary(textBytes, salt, PasswordHash.Strength.Medium)
TextBox2.Text = Convert.ToBase64String(passEnc)
TextBox6S.Text = Convert.ToBase64String(salt)
TextBox3PBK.Text = Convert.ToBase64String(key)
TextBox5H.Text = Convert.ToBase64String(hashedBytes)
End Sub
End Class
As you can see in the above procedure, this program's fairly straight forward and should just take an input, run it through a couple of Sodium functions, then convert to Base 64 strings and print the results to some text boxes but I get the BadImageFormatException error on each Sodium function the procedure calls.

no such table: SQLite in VB.net: UWP

I'm new to EF Core and SQLite and I followed the steps at https://learn.microsoft.com/en-us/ef/core/get-started/uwp/getting-started. Everything works just fine in C#, however when translating it to VB, I get the following exception:
Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 1: 'no such table: Blogs'.'
I've tried changing the "Data Source=Blogging.db" to an absolute path, but if I do I get
Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'
This is what I am using right now (in the context):
Protected Overrides Async Sub OnConfiguring(optionsBuilder As DbContextOptionsBuilder)
Dim AssetsFolder As StorageFolder = _
Await Package.Current.InstalledLocation.GetFolderAsync("Assets")
optionsBuilder.UseSqlite(String.Format("Data Source={0}", _
Path.Combine(AssetsFolder.Path, "blogging.db")))
End Sub
And in the App.xaml.vb:
Sub New()
InitializeComponent()
Using db = New BloggingContext()
db.Database.Migrate()
End Using
End Sub
Any help would be much appreciated. If you require more info, please let me know and I'll gladly provide it.
Crix
The problem seems to be due to the created .cs files. When you convert the three files to .vb files, the desired tables are created and an entry is created in the EFMigrationsHistory table. I have manually converted the following files from cs to vb:
20170819131117_Inital.cs
20170819131117_Inital.Designer.cs
DB_ModelModelSnapshot.cs
And the cs files taken from the project and the vb added. If someone now knows how to run the add-migration command for vb, that would be very helpful.

How to get custom dll's working in a ssis script component?

I try to use my own .dll in a script compoment within ssis. The normal procedure gives me an error: "could not load file or assembly 'xxx' or one of its dependencies. The system cannot find the file specified."
What I tried yet is I went to project -> Open in Explorer and put my .dll into the bin folder but that same error occures.
I found this C# Code and converted it to vb.net:
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute> _
Public Partial Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Shared Sub New()
AppDomain.CurrentDomain.AssemblyResolve += New ResolveEventHandler(AddressOf CurrentDomain_AssemblyResolve)
End Sub
Private Shared Function CurrentDomain_AssemblyResolve(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
If args.Name.Contains("ssisHelper") Then
Dim path As String = "c:\temp\"
Return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "ssisHelper.dll"))
End If
Return Nothing
End Function
End Class
But I do not have Micorosoft.SqlServer.Dts.**Tasks**. Anyone who can help me either get this script working or can provide another solution to get my dll running within the script compoment?
And before adding it to the GAC you need to strong name it, example here: http://microsoft-ssis.blogspot.com/2011/05/referencing-custom-assembly-inside.html
You need to add the assembly to the GAC (Global Assembly Cache) by running gacutil -i assembly.dll
More information can be found here
https://msdn.microsoft.com/en-us/library/dkkx7f79(v=vs.100).aspx

Why is EnvDTE.ProjectItem.FileCodeModel = Nothing?

I wrote a VSMacro (in VS2010) that parses a solution and adds try and catch statements to cpp files. It's been working fine up until now. I had to reformat my pc and reinstall VS2010 and now it crashes. Here's a snippet from the macro.
Sub Foo(ByVal file As EnvDTE.ProjectItem)
Dim fileCM As EnvDTE.FileCodeModel
fileCM = file.FileCodeModel
End Sub
I've checked to see if that file is a valid object, and it does point to a cpp file. But for some reason file.FileCodeModel = Nothing. Why would FileCodeModel be Nothing?
There are several scenarios in Visual Studio where a ProjectItem will not return a FileCodeModel object. For example if the ProjectItem points to a folder or a non-code file item in solution explorer.
Can you verify what the ProjectItem represents in this scenario. In particular
Is it pointing to a file?
What type of project is this in (Web, console app, etc ...)?
Usually, it happens when the project is not compilable. You need to try to build the solution first.