diff --git a/openalprnet-cli/CommandLine.cs b/openalprnet-cli/CommandLine.cs
new file mode 100644
index 0000000..c861a6b
--- /dev/null
+++ b/openalprnet-cli/CommandLine.cs
@@ -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 .
+ */
+
+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 ([="value 1",value2,...])+ */
+
+ private static readonly Regex RexPattern =
+ new Regex(@"(?[^=]+)=?((?\""?)(?(?(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> handler, string shortForm)
+ {
+ Name = name;
+ Handler = handler;
+ ShortForm = shortForm;
+ }
+
+ public Switch(string name, Action> handler)
+ {
+ Name = name;
+ Handler = handler;
+ ShortForm = null;
+ }
+
+ public string Name { get; private set; }
+ public string ShortForm { get; private set; }
+ public Action> Handler { get; private set; }
+
+ public int InvokeHandler(string[] values)
+ {
+ Handler(values);
+ return 1;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/openalprnet-cli/Program.cs b/openalprnet-cli/Program.cs
index ddef99b..f6881e3 100644
--- a/openalprnet-cli/Program.cs
+++ b/openalprnet-cli/Program.cs
@@ -17,6 +17,7 @@
*/
using System;
+using System.Diagnostics;
using System.IO;
using System.Linq;
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)
{
- 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="),
+ 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("us", config);
- var loaded = alpr.isLoaded();
- var samplePath = Path.Combine(AssemblyDirectory, @"samples\us-1.jpg");
- var imgBytes = File.ReadAllBytes(samplePath);
+ 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 = "us";
- alpr.DetectRegion = true;
- //var results = alpr.recognize(samplePath);
- var results = alpr.recognize(imgBytes);
- var json = alpr.toJson();
- Console.WriteLine(json);
- var plates = results.First().topNPlates;
+ 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();
+ 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);
+ }
+ }
+ }
}
}
}
\ No newline at end of file
diff --git a/openalprnet-cli/openalprnet-cli.csproj b/openalprnet-cli/openalprnet-cli.csproj
index 4e641d8..2fb8ce5 100644
--- a/openalprnet-cli/openalprnet-cli.csproj
+++ b/openalprnet-cli/openalprnet-cli.csproj
@@ -43,6 +43,7 @@
+