Open Alpr cannot load its dependency

1.7k Views Asked by At

Error message: Could not load file or assembly 'openalpr-net, Version=2.3.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

But when I run the alpr.exe by commend line, it does not have any issues.

Here is my code:

    private void CallAlpr()
    {
        var openFileDialog = new OpenFileDialog();

        if (openFileDialog.ShowDialog() == true)
        {
            OpenAlpr.Output(openFileDialog.FileName);
        }

        Console.Read();
    }

OpenAlpr class:

 using ......
 using openalprnet;
 using System.Drawing;

public static List<AlprPlateResultNet> RecognizePlate(string imagePath)
        {
            var alpr = new AlprNet("en", "/openalpr_64/openalpr.conf", "/openalpr_64/runtime_data");

            if (!alpr.IsLoaded())
            {
                throw new Exception("OpenAlpr failed to load!");
            }

            alpr.DefaultRegion = "md";

            var results = alpr.Recognize(imagePath);
            return results?.Plates;
        }

        public static void Output(string imagePath)
        {
            var plates = RecognizePlate(imagePath);
            var i = 0;
            foreach (var result in plates)
            {
                Console.WriteLine("Plate {0}:{1} result(s)", i++, result.TopNPlates.Count);
                Console.WriteLine("   Processing Time: {0} msec(s)", result.ProcessingTimeMs);

                foreach (var plate in result.TopNPlates)
                {
                    Console.WriteLine("   - {0}\t Confidence: {1}\tMatches Template: {2}", 
                        plate.Characters, plate.OverallConfidence, plate.MatchesTemplate);
                }
            }
        }

I have the folder: openalpr_64 under my project folder.

2

There are 2 best solutions below

1
On BEST ANSWER

I solved this problem by copying all files and folders from the zip file to Debug folder and made sure the the .config and runtime_data pointed to the right path.

3
On

In my experience I've only seen that specific message when attempting to use C# DLLs / .EXEs that are not all either 32Bit or 64bit.

Check that your project build is not set to 'Any CPU' (which will default to 64 bit on a 64 bit os).

At the very least, examine the 32/64 bit'ness of your .EXE and library .DLL.

Good luck!