Moved OpenALPR-Net to bindings/csharp/

This commit is contained in:
Matt Hill
2015-01-05 22:44:19 -05:00
parent 23a93d2411
commit 4f3c768b0f
15 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
/*
* Copyright (c) 2015 Dr. Masroor Ehsan
*
* This file is part of OpenAlpr.Net.
*
* OpenAlpr.Net is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License
* version 3 as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace openalprnet_cli
{
internal static class CommandLine
{
private const string NameGroup = "name"; // Names of capture groups
private const string ValueGroup = "value";
/* The regex that extracts names and comma-separated values for switches
in the form (<switch>[="value 1",value2,...])+ */
private static readonly Regex RexPattern =
new Regex(@"(?<name>[^=]+)=?((?<quoted>\""?)(?<value>(?(quoted)[^\""]+|[^,]+))\""?,?)*",
RegexOptions.Compiled | RegexOptions.CultureInvariant |
RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
public static void Process(this string[] args, Action printUsage, params Switch[] switches)
{
/* Run through all matches in the argument list and if any of the switches
match, get the values and invoke the handler we were given. We do a Sum()
here for 2 reasons; a) To actually run the handlers
and b) see if any were invoked at all (each returns 1 if invoked).
If none were invoked, we simply invoke the printUsage handler. */
if ((from arg in args
from Match match in RexPattern.Matches(arg)
from s in switches
where match.Success &&
((string.Compare(match.Groups[NameGroup].Value, s.Name, true) == 0) ||
(string.Compare(match.Groups[NameGroup].Value, s.ShortForm, true) == 0))
select s.InvokeHandler(match.Groups[ValueGroup].Value.Split(','))).Sum() == 0)
printUsage(); // We didn't find any switches
}
public class Switch // Class that encapsulates switch data.
{
public Switch(string name, Action<IEnumerable<string>> handler, string shortForm)
{
Name = name;
Handler = handler;
ShortForm = shortForm;
}
public Switch(string name, Action<IEnumerable<string>> handler)
{
Name = name;
Handler = handler;
ShortForm = null;
}
public string Name { get; private set; }
public string ShortForm { get; private set; }
public Action<IEnumerable<string>> Handler { get; private set; }
public int InvokeHandler(string[] values)
{
Handler(values);
return 1;
}
}
}
}

View File

@@ -0,0 +1,140 @@
/*
* Copyright (c) 2015 Dr. Masroor Ehsan
*
* This file is part of OpenAlpr.Net.
*
* OpenAlpr.Net is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License
* version 3 as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using openalprnet;
namespace openalprnet_cli
{
internal class Program
{
public static string AssemblyDirectory
{
get
{
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
var uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
private static bool StrToBool(string s)
{
return !string.IsNullOrEmpty(s) && s.Trim() == "1";
}
private static void Main(string[] args)
{
var region = "us";
var detectRegion = false;
var benchmark = false;
var json = false;
var filename = string.Empty;
args.Process(
() => Console.WriteLine("Usage: r=us/eu b=0/1 j=0/1 d=0/1 f=<filename>"),
new CommandLine.Switch("r",
val => { if (val.Any()) region = val.First().Trim().ToLower(); }),
new CommandLine.Switch("b",
val => { if (val.Any()) benchmark = StrToBool(val.First()); }),
new CommandLine.Switch("j",
val => { if (val.Any()) json = StrToBool(val.First()); }),
new CommandLine.Switch("d",
val => { if (val.Any()) detectRegion = StrToBool(val.First()); }),
new CommandLine.Switch("f",
val => { if (val.Any()) filename = val.First().Trim(); })
);
Console.WriteLine("OpenAlpr Version: {0}", AlprNet.getVersion());
var config = Path.Combine(AssemblyDirectory, "openalpr.conf");
var alpr = new AlprNet(region, config);
if (!alpr.isLoaded())
{
Console.WriteLine("OpenAlpr failed to loaded!");
return;
}
//var samplePath = Path.Combine(AssemblyDirectory, @"samples\eu-1.jpg");
//alpr.TopN = 3;
alpr.DefaultRegion = region;
alpr.DetectRegion = detectRegion;
if (Directory.Exists(filename))
{
var files = Directory.GetFiles(filename, "*.jpg", SearchOption.TopDirectoryOnly);
foreach (var fname in files)
{
PerformAlpr(alpr, fname, benchmark, json);
}
return;
}
if (!File.Exists(filename))
{
Console.WriteLine("The file doesn't exist!");
return;
}
var buffer = File.ReadAllBytes(filename);
PerformAlpr(alpr, buffer, benchmark, json);
}
private static void PerformAlpr(AlprNet alpr, string filename, bool benchmark, bool writeJson)
{
Console.WriteLine("Processing '{0}'...\n------------------", Path.GetFileName(filename));
var buffer = File.ReadAllBytes(filename);
PerformAlpr(alpr, buffer, benchmark, writeJson);
}
private static void PerformAlpr(AlprNet alpr, byte[] buffer, bool benchmark, bool writeJson)
{
var sw = Stopwatch.StartNew();
sbyte[] signedBuffer = (sbyte[])(Array)buffer;
var results = alpr.recognize(signedBuffer);
sw.Stop();
if (benchmark)
{
Console.WriteLine("Total Time to process image(s): {0} msec(s)", sw.ElapsedMilliseconds);
}
if (writeJson)
{
//Console.WriteLine(alpr.toJson());
}
else
{
var i = 0;
foreach (var result in results.plates)
{
Console.WriteLine("Plate {0}: {1} result(s)", i++, result.topNPlates.Count);
Console.WriteLine(" Processing Time: {0} msec(s)", result.processing_time_ms);
foreach (var plate in result.topNPlates)
{
Console.WriteLine(" - {0}\t Confidence: {1}\tMatches Template: {2}", plate.characters,
plate.overall_confidence, plate.matches_template);
}
}
}
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("openalprnet-cli")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("openalprnet-cli")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("42477de2-b13e-4959-a03e-ae43c65e68f3")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{BD50C6C1-EEB9-48D2-A87C-70F5342579DD}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>openalprnet_cli</RootNamespace>
<AssemblyName>openalprnet-cli</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CommandLine.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\openalpr-net\openalpr-net.vcxproj">
<Project>{4044340C-C435-4A1F-8F12-0806C38AE3B6}</Project>
<Name>openalpr-net</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>