mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-07 02:42:47 +08:00
added cmd line flags and params support
This commit is contained in:
82
openalprnet-cli/CommandLine.cs
Normal file
82
openalprnet-cli/CommandLine.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -17,6 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@@ -37,22 +38,102 @@ namespace openalprnet_cli
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool StrToBool(string s)
|
||||||
|
{
|
||||||
|
return !string.IsNullOrEmpty(s) && s.Trim() == "1";
|
||||||
|
}
|
||||||
|
|
||||||
private static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine(AlprNet.getVersion());
|
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 config = Path.Combine(AssemblyDirectory, "openalpr.conf");
|
||||||
var alpr = new AlprNet("us", config);
|
var alpr = new AlprNet(region, config);
|
||||||
var loaded = alpr.isLoaded();
|
if (!alpr.isLoaded())
|
||||||
var samplePath = Path.Combine(AssemblyDirectory, @"samples\us-1.jpg");
|
{
|
||||||
var imgBytes = File.ReadAllBytes(samplePath);
|
Console.WriteLine("OpenAlpr failed to loaded!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//var samplePath = Path.Combine(AssemblyDirectory, @"samples\eu-1.jpg");
|
||||||
//alpr.TopN = 3;
|
//alpr.TopN = 3;
|
||||||
alpr.DefaultRegion = "us";
|
alpr.DefaultRegion = region;
|
||||||
alpr.DetectRegion = true;
|
alpr.DetectRegion = detectRegion;
|
||||||
//var results = alpr.recognize(samplePath);
|
|
||||||
var results = alpr.recognize(imgBytes);
|
if (Directory.Exists(filename))
|
||||||
var json = alpr.toJson();
|
{
|
||||||
Console.WriteLine(json);
|
var files = Directory.GetFiles(filename, "*.jpg", SearchOption.TopDirectoryOnly);
|
||||||
var plates = results.First().topNPlates;
|
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();
|
||||||
|
var results = alpr.recognize(buffer);
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Plate {0}: {1} result(s)", i++, result.result_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -43,6 +43,7 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="CommandLine.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
Reference in New Issue
Block a user