Updated C# bindings for OpenALPR 2.0 API

This commit is contained in:
Matt Hill
2015-01-05 22:41:23 -05:00
parent 9b44f9e67b
commit 23a93d2411
2 changed files with 96 additions and 66 deletions

View File

@@ -31,37 +31,39 @@ using namespace msclr::interop;
using namespace System::Collections::Generic; using namespace System::Collections::Generic;
using namespace System::Runtime::InteropServices; using namespace System::Runtime::InteropServices;
using namespace System::Drawing; using namespace System::Drawing;
using namespace alpr;
namespace openalprnet { namespace openalprnet {
private ref class AlprHelper sealed private ref class AlprHelper sealed
{ {
public: public:
static std::vector<unsigned char> ToVector(array<unsigned char>^ src) static std::vector<char> ToVector(array<char>^ src)
{ {
std::vector<unsigned char> result(src->Length); std::vector<char> result(src->Length);
pin_ptr<unsigned char> pin(&src[0]); pin_ptr<char> pin(&src[0]);
unsigned char *first(pin), *last(pin + src->Length); char *first(pin), *last(pin + src->Length);
std::copy(first, last, result.begin()); std::copy(first, last, result.begin());
return result; return result;
} }
static std::vector<AlprRegionOfInterest> ToVector(List<System::Drawing::Rectangle>^ src)
{
std::vector<AlprRegionOfInterest> result;
for each(System::Drawing::Rectangle^ rect in src) // static std::vector<AlprRegionOfInterest> ToVector(List<System::Drawing::Rectangle>^ src)
{ // {
AlprRegionOfInterest roi; // std::vector<AlprRegionOfInterest> result;
roi.x = rect->X; //
roi.y = rect->Y; // for each(System::Drawing::Rectangle^ rect in src)
roi.height = rect->Height; // {
roi.width = rect->Width; // AlprRegionOfInterest roi;
result.push_back(roi); // roi.x = rect->X;
} // roi.y = rect->Y;
// roi.height = rect->Height;
return result; // roi.width = rect->Width;
} // result.push_back(roi);
// }
//
// return result;
// }
static System::String^ ToManagedString(std::string s) static System::String^ ToManagedString(std::string s)
{ {
@@ -115,12 +117,12 @@ namespace openalprnet {
bool m_matches_template; bool m_matches_template;
}; };
public ref class AlprResultNet sealed public ref class AlprPlateResultNet sealed
{ {
public: public:
AlprResultNet() : m_Impl( new AlprResult ) {} AlprPlateResultNet() : m_Impl( new AlprPlateResult ) {}
AlprResultNet(AlprResult* result) : m_Impl( result ) {} AlprPlateResultNet(AlprPlateResult* result) : m_Impl( result ) {}
property int requested_topn { property int requested_topn {
int get() { int get() {
@@ -140,11 +142,6 @@ namespace openalprnet {
} }
} }
property int result_count {
int get() {
return m_Impl->result_count;
}
}
property AlprPlateNet^ bestPlate { property AlprPlateNet^ bestPlate {
AlprPlateNet^ get() { AlprPlateNet^ get() {
@@ -182,9 +179,64 @@ namespace openalprnet {
} }
private: private:
AlprResult * m_Impl; AlprPlateResult * m_Impl;
}; };
public ref class AlprResultsNet sealed
{
public:
AlprResultsNet() : m_Impl( new AlprResults ) {}
AlprResultsNet(AlprResults* results) : m_Impl( results ) {}
property int img_width {
int get() {
return m_Impl->img_width;
}
}
property int img_height {
int get() {
return m_Impl->img_height;
}
}
property float total_processing_time_ms {
float get() {
return m_Impl->total_processing_time_ms;
}
}
property List<System::Drawing::Rectangle>^ regionsOfInterest {
List<System::Drawing::Rectangle>^ get() {
List<System::Drawing::Rectangle>^ list = gcnew List<System::Drawing::Rectangle>(m_Impl->regionsOfInterest.size());
for (unsigned int i = 0; i < m_Impl->regionsOfInterest.size(); i++)
{
list->Add(System::Drawing::Rectangle(m_Impl->regionsOfInterest[i].x, m_Impl->regionsOfInterest[i].y, m_Impl->regionsOfInterest[i].width, m_Impl->regionsOfInterest[i].height));
}
return list;
}
}
property List<AlprPlateResultNet^>^ plates {
List<AlprPlateResultNet^>^ get() {
List<AlprPlateResultNet^>^ list = gcnew List<AlprPlateResultNet^>(m_Impl->plates.size());
for (std::vector<AlprPlateResult>::iterator itr = m_Impl->plates.begin(); itr != m_Impl->plates.end(); itr++)
{
list->Add(gcnew AlprPlateResultNet(&*itr));
}
return list;
}
}
private:
AlprResults * m_Impl;
};
public ref class AlprNet sealed public ref class AlprNet sealed
{ {
public: public:
@@ -226,29 +278,17 @@ namespace openalprnet {
} }
} }
List<AlprResultNet^>^ recognize(System::String^ filepath) { AlprResultsNet^ recognize(System::String^ filepath) {
m_results = new std::vector<AlprResult>(m_Impl->recognize(marshal_as<std::string>(filepath))); AlprResults results = m_Impl->recognize(marshal_as<std::string>(filepath));
return this->processResults(); return gcnew AlprResultsNet(&results);
} }
List<AlprResultNet^>^ recognize(System::String^ filepath, List<System::Drawing::Rectangle>^ regionsOfInterest) { AlprResultsNet^ recognize(cli::array<char>^ imageBuffer) {
std::vector<AlprRegionOfInterest> rois = AlprHelper::ToVector(regionsOfInterest); std::vector<char> p = AlprHelper::ToVector(imageBuffer);
m_results = new std::vector<AlprResult>(m_Impl->recognize(marshal_as<std::string>(filepath), rois)); AlprResults results = m_Impl->recognize(p);
return this->processResults(); return gcnew AlprResultsNet(&results);
} }
List<AlprResultNet^>^ recognize(cli::array<unsigned char>^ imageBuffer) {
std::vector<unsigned char> p = AlprHelper::ToVector(imageBuffer);
m_results = new std::vector<AlprResult>(m_Impl->recognize(p));
return this->processResults();
}
List<AlprResultNet^>^ recognize(cli::array<unsigned char>^ imageBuffer, List<System::Drawing::Rectangle>^ regionsOfInterest) {
std::vector<AlprRegionOfInterest> rois = AlprHelper::ToVector(regionsOfInterest);
std::vector<unsigned char> p = AlprHelper::ToVector(imageBuffer);
m_results = new std::vector<AlprResult>(m_Impl->recognize(p, rois));
return this->processResults();
}
bool isLoaded() { bool isLoaded() {
return m_Impl->isLoaded(); return m_Impl->isLoaded();
@@ -258,10 +298,10 @@ namespace openalprnet {
return AlprHelper::ToManagedString(Alpr::getVersion()); return AlprHelper::ToManagedString(Alpr::getVersion());
} }
System::String^ toJson() { // System::String^ toJson(AlprResultsNet^ results) {
std::string json = m_Impl->toJson(*m_results, -1); // std::string json = Alpr::toJson(marshal_as<AlprResults>(results));
return AlprHelper::ToManagedString(json); // return AlprHelper::ToManagedString(json);
} // }
protected: protected:
// Deallocate the native object on the finalizer just in case no destructor is called // Deallocate the native object on the finalizer just in case no destructor is called
@@ -271,20 +311,9 @@ namespace openalprnet {
private: private:
Alpr * m_Impl; Alpr * m_Impl;
std::vector<AlprResult>* m_results;
int m_topN; int m_topN;
bool m_detectRegion; bool m_detectRegion;
System::String^ m_defaultRegion; System::String^ m_defaultRegion;
List<AlprResultNet^>^ processResults() {
std::vector<AlprResult>& runList = *m_results;
std::vector<AlprResult>::iterator itr;
List<AlprResultNet^>^ list = gcnew List<AlprResultNet^>(runList.size());
for (itr = runList.begin(); itr != runList.end(); itr++)
{
list->Add(gcnew AlprResultNet(&*itr));
}
return list;
}
}; };
} }

View File

@@ -109,7 +109,8 @@ namespace openalprnet_cli
private static void PerformAlpr(AlprNet alpr, byte[] buffer, bool benchmark, bool writeJson) private static void PerformAlpr(AlprNet alpr, byte[] buffer, bool benchmark, bool writeJson)
{ {
var sw = Stopwatch.StartNew(); var sw = Stopwatch.StartNew();
var results = alpr.recognize(buffer); sbyte[] signedBuffer = (sbyte[])(Array)buffer;
var results = alpr.recognize(signedBuffer);
sw.Stop(); sw.Stop();
if (benchmark) if (benchmark)
{ {
@@ -118,14 +119,14 @@ namespace openalprnet_cli
if (writeJson) if (writeJson)
{ {
Console.WriteLine(alpr.toJson()); //Console.WriteLine(alpr.toJson());
} }
else else
{ {
var i = 0; var i = 0;
foreach (var result in results) foreach (var result in results.plates)
{ {
Console.WriteLine("Plate {0}: {1} result(s)", i++, result.result_count); Console.WriteLine("Plate {0}: {1} result(s)", i++, result.topNPlates.Count);
Console.WriteLine(" Processing Time: {0} msec(s)", result.processing_time_ms); Console.WriteLine(" Processing Time: {0} msec(s)", result.processing_time_ms);
foreach (var plate in result.topNPlates) foreach (var plate in result.topNPlates)
{ {