Cannot find a way to make tessnet2 work - c#

I have created a console application.
Added a reference to tessnet2_32.
Ocr ocr = new Ocr();
using (Bitmap bmp = new Bitmap(filename))
{
tessnet2.Tesseract tessocr = new tessnet2.Tesseract();
tessocr.Init(#"C:\temp\tessdata", "eng", false);
...
I also tried changing "C:\temp\tessdata" to
C:\work\ConsoleApplication3\ConsoleApplication3
C:\work\ConsoleApplication3\ConsoleApplication3\tessdata
C:\work\ConsoleApplication3\ConsoleApplication3\bin\debug
C:\work\ConsoleApplication3\ConsoleApplication3\bin
C:\work\ConsoleApplication3\ConsoleApplication3\bin\debug\tessdata
C:\work\ConsoleApplication3\ConsoleApplication3\bin\tessdata
C:\work\ConsoleApplication3\ConsoleApplication3\debug\tessdata
C:\work\ConsoleApplication3\tessdata
C:\work\ConsoleApplication3\
The tessdata folder itself contained 9 failed and was added to all of these locations:
eng.cube.bigrams
eng.cube.fold
eng.cube.lm
eng.cube.bigrams
eng.cube.params
eng.cube.size
eng.cube.word-freq
eng.tesseract_cube.nn
eng.traineddata
But it just always exists at that .Init line with a message:
The file 'z:\dev\interne\cs\tesseract-ocr-svn\dotnet\tessnet2.cpp' does not exist.
I cannot imagine why it is trying to access some Z disk while I only have C. Or I just completely misunderstand the error.
Can someone be kind enough to post step by step telling what to do and/or what I am doing wrong? I feel completely lost even after reading 30+ google links.

You use the wrong version of language data file; what you have is for Tesseract 3.0x. tessnet2 is .NET wrapper for Tesseract 2.04, so you will need to load compatible data file.
Try download tesseract-2.00.eng.tar.gz from https://sourceforge.net/projects/tesseract-ocr-alt/files/.

Related

How to use OCR to read text from image

I'm trying to use IronOCR to read the text from an image. I manually download their dll and include in my project and follow the example that they provided on their website. However, there is no text return at all. Even I'm trying on different file or their sample image also no result. Is there any step that I'm missing?
Here I attach the image that i'm using to try:myImage
Try the following code with the latest version of IronOCR (currently 2021.2.1) which was updated to use Tesseract 4 & 5. Returned a perfect result on your example image.
var Ocr = new IronTesseract();
using (var Input = new OcrInput(#"F:\input_image.png"))
{
Input.Deskew();
var Result = Ocr.Read(Input);
}
Deskew filter gave the best results but there are other that may be useful listed here:
https://ironsoftware.com/csharp/ocr/examples/ocr-image-filters-for-net-tesseract/

Load Bitmap into memory in Unity using System.Drawing

Using Unity 2017.4.33f1-Personal.
I'm trying to use the Image.FromFile method in the System.Drawing namespace to load a Bitmap in a GameObject's logic. Please note that I am not interested in saving this bmp as an asset or anything like that. I am only looking to load it into RAM where I can do some calculations. I have tried the following:
var result = Image.FromFile(
AssetDatabase.GetAssetPath(mainImg)
) as Bitmap;
But I get the following error:
InvalidProgramException: Invalid IL code in System.Drawing.Image:FromFile (string): IL_0000: ret
FYI,
mainImg is a reference to a Texture2D resource
AssetDatabase.GetAssetPath(mainImg) resolves to Assets/CustomPackages/SeededConstruction/PuzzleBuilder/Input/main.bmp
I've also tried a bunch of other goofy things, including getting the absolute path and replacing forward slashes with backslashes like this:
var result = Image.FromFile(
Directory.GetCurrentDirectory()
+ "\\"
+ AssetDatabase.GetAssetPath(mainTileset)
.Replace('/', '\\')
) as Bitmap;
In this case, the path resolves to D:\_Projects\2dGenerator\PlatformGeneration\Assets\CustomPackages\SeededConstruction\PuzzleBuilder\Input\main.bmp. However, this returns the same error as above.
What am I doing wrong here? It seems like this methodology works fine in normal .Net applications, why not in Unity?
It turns out it was not any kind of filepath issue. In order to properly reference System.Drawing in Unity 2017.4.33f1-Personal, I took the following steps:
Go to Edit -> Project Settings -> Player. Change the API Compatability Level from .NET 2.0 Subset to .NET 2.0.
Add an mcs.rsp to my unity projects root Asset folder. Open in it in a text editor and on the first line, enter: -r:System.Drawing.dll. Save.
Close and reopen Unity.

Decoding ogg files to wav in WP8

I want to convert ogg file to wav and then play it on wp8 devives.
I've already checked many solutions but none of them worked. This looks promising but something doesn't work:
string _audioPath = "/SomeProject;component/Sounds/a_dog.ogg";
var stream = Application.GetResourceStream(new Uri(_audioPath, UriKind.Relative)).Stream;
using (var vorbis = new NVorbis.VorbisReader(stream, true))
{
float[] buf = new float[vorbis.TotalSamples];
vorbis.ReadSamples(buf, 0, (int)vorbis.TotalSamples);
}
When I execute it I see FileNotFoundException at VorbisReader contruction. I also checked if stream is readable and it is. I was able to get the file content using Read method.
Do you have any ideas why it doesn't work? Maybe you know some other library for wp8 which can decode ogg files?
[EDIT] I downloaded source code of NVorbis and used it directly from my project, and when i do this I don't get any FileNotFoundExceptions and everything seems to work. Maybe this exception is caused by missing library? I've got NVorbis reference added...
I think you are running into the same issue as this guy. Short version: I believe your Pack URI should be "pack://application:,,,/SomeProject;Component/Sounds/a_dog.ogg". NVorbis is not actually throwing the exception itself, but is instead causing the resource stream to do so...

C# import of Adobe Illustrator (.AI) files render to Bitmap?

Anyone knows how to load a .AI file (Adobe Illustrator) and then rasterize/render the vectors into a Bitmap so I could generate eg. a JPG or PNG from it?
I would like to produce thumbnails + render the big version with transparent background in PNG if possible.
Ofcause its "possible" if you know the specs of the .AI, but has anyone any knowledge or code to share for a start? or perhaps just a link to some components?
C# .NET please :o)
Code is most interesting as I know nothing about reading vector points and drawing splines.
Well, if Gregory is right that ai files are pdf-compatible, and you are okay with using GPL code, there is a project called GhostscriptSharp on github that is a .NET interface to the Ghostscript engine that can render PDF.
With the newer AI versions, you should be able to convert from PDF to image. There are plenty of libraries that do this that are cheap, so I would choose buy over build on this one. If you need to convert the older AI files, all bets are off. I am not sure what format they were in.
private void btnGetAIThumb_Click(object sender, EventArgs e)
{
Illustrator.Application app = new Illustrator.Application();
Illustrator.Document doc = app.Open(#"F:/AI_Prog/2009Calendar.ai", Illustrator.AiDocumentColorSpace.aiDocumentRGBColor, null);
doc.Export(#"F:/AI_Prog/2009Calendar.png",Illustrator.AiExportType.aiPNG24, null);
doc.Close(Illustrator.AiSaveOptions.aiDoNotSaveChanges);
doc = null; //
}
Illustrator.AiExportType.aiPNG24 can be set as JPEG,GIF,Flash,SVG and Photoshop format.
I Have Tested that with Pdf2Png and it worked fine with both .PDF and .ai files.
But I don't know how it will work with transparents.
string pdf_filename = #"C:\test.ai";
//string pdf_filename = #"C:\test.pdf";
string png_filename = "converted.png";
List<string> errors = cs_pdf_to_image.Pdf2Image.Convert(pdf_filename, png_filename);

Reading PSD file format

I wonder if this is even possible. I have an application that adds a context menu when you right click a file. It all works fine but here is what I'd like to do:
If the file is a PSD then I want the program to extract the image. Is this possible to do without having Photoshop installed?
Basically I want the user to right click and click "image" which would save a .jpg of the file for them.
edit: will be using c#
Thanks
The ImageMagick libraries (which provide bindings for C#) also support the PSD format. They might be easier to get started with than getting into the Paint.NET code and also come with a quite free (BSD-like) license.
A simple sample (found at http://midimick.com/magicknet/magickDoc.html) using MagickNet would look like this:
using System;
static void Main(string[] args)
{
MagickNet.Magick.Init();
MagicNet.Image img = new MagicNet.Image("file.psd");
img.Resize(System.Drawing.Size(100,100));
img.Write("newFile.png");
MagickNet.Magick.Term();
}
Note: MagickNet has moved to http://www.codeproject.com/KB/dotnet/ImageMagick_in_VBNET.aspx
Well, there's a PSD plugin for Paint.NET which I think is Open-Source which you might want to take a look at for starters:
http://frankblumenberg.de/doku/doku.php?id=paintnet:psdplugin#download
This guy do it easier:
http://www.codeproject.com/KB/graphics/simplepsd.aspx
With a C# library and a sample project.
I've tried with PS2 files and works ok.
I have written a PSD parser which extracts raster format layers from all versions of PSD and PSB. http://www.telegraphics.com.au/svn/psdparse/trunk
You can use GroupDocs.Viewer for .NET API to render your PSD files as images (JPG, PNG, BMP) in your application using a few lines of code.
C#
ViewerConfig config = new ViewerConfig();
config.StoragePath = "D:\\storage\\";
// Create handler
ViewerImageHandler imageHandler = new ViewerImageHandler(config);
// Guid implies that unique document name
string guid = "sample.psd";
// Get document pages as images
List<PageImage> pages = imageHandler.GetPages(guid);
foreach (PageImage page in pages)
{
// Access each image using page.Stream
}
For more details and sample code, please visit here.
Disclosure: I work as a Developer Evangelist at GroupDocs.
For people who are reading this now: the link from accepted answer doesn't seem to work anymore (at least for me). Would add a comment there, but not allowed to comment yet - hence I'm adding a new answer.
The working link where you can find the psdplugin code for Paint.Net: https://github.com/PsdPlugin/PsdPlugin
Here is my own psd parser and exporter:
http://papirosnik.info/psdsplit/.
It allows to correctly parse psd with rgb color 8, 16- and 32-bit for channel, process user masks, export selected layers into jpeg, png, jng, bmp, tiff; create xml layout of exported layers and groups and also create a texture atlas and animations set from given layers.
It's entirely written in C#. If you want its sources inform me via support link on About dialog in the application.
ImageMagick.NET - http://imagemagick.codeplex.com/ - is the later version of the link 0xA3 gave, with some slightly different syntax. (Note, this is untested):
using ImageMagickNET;
public void Test() {
MagickNet.InitializeMagick();
ImageMagickNET.Image img = new ImageMagickNET.Image("file.psd");
img.Resize(new Geometry(100, 100, 0, 0, false, false);
img.Write("newFile.png");
}
I got extraction from psd working. see my answer here
How to extract layers from a Photoshop file? C#
may help someone else.
FastStone does this pretty efficiently.
They do not have their libraries availaible, but I guess you can contact them and see if they can help.
Check out their website: http://www.faststone.org/download.htm
I've had great success with Aspose's Imaging component which can load and save PSD files without Photoshop: https://products.aspose.com/imaging/net

Categories