diff --git a/openalpr-net/openalpr-net.cpp b/openalpr-net/openalpr-net.cpp index 3c6dc23..e5ccf90 100644 --- a/openalpr-net/openalpr-net.cpp +++ b/openalpr-net/openalpr-net.cpp @@ -31,37 +31,39 @@ using namespace msclr::interop; using namespace System::Collections::Generic; using namespace System::Runtime::InteropServices; using namespace System::Drawing; +using namespace alpr; namespace openalprnet { private ref class AlprHelper sealed { public: - static std::vector ToVector(array^ src) + static std::vector ToVector(array^ src) { - std::vector result(src->Length); - pin_ptr pin(&src[0]); - unsigned char *first(pin), *last(pin + src->Length); + std::vector result(src->Length); + pin_ptr pin(&src[0]); + char *first(pin), *last(pin + src->Length); std::copy(first, last, result.begin()); return result; } - static std::vector ToVector(List^ src) - { - std::vector result; - for each(System::Drawing::Rectangle^ rect in src) - { - AlprRegionOfInterest roi; - roi.x = rect->X; - roi.y = rect->Y; - roi.height = rect->Height; - roi.width = rect->Width; - result.push_back(roi); - } - - return result; - } +// static std::vector ToVector(List^ src) +// { +// std::vector result; +// +// for each(System::Drawing::Rectangle^ rect in src) +// { +// AlprRegionOfInterest roi; +// roi.x = rect->X; +// roi.y = rect->Y; +// roi.height = rect->Height; +// roi.width = rect->Width; +// result.push_back(roi); +// } +// +// return result; +// } static System::String^ ToManagedString(std::string s) { @@ -115,12 +117,12 @@ namespace openalprnet { bool m_matches_template; }; - public ref class AlprResultNet sealed + public ref class AlprPlateResultNet sealed { 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 { int get() { @@ -140,11 +142,6 @@ namespace openalprnet { } } - property int result_count { - int get() { - return m_Impl->result_count; - } - } property AlprPlateNet^ bestPlate { AlprPlateNet^ get() { @@ -182,9 +179,64 @@ namespace openalprnet { } 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^ regionsOfInterest { + List^ get() { + List^ list = gcnew List(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^ plates { + List^ get() { + List^ list = gcnew List(m_Impl->plates.size()); + for (std::vector::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: @@ -226,29 +278,17 @@ namespace openalprnet { } } - List^ recognize(System::String^ filepath) { - m_results = new std::vector(m_Impl->recognize(marshal_as(filepath))); - return this->processResults(); + AlprResultsNet^ recognize(System::String^ filepath) { + AlprResults results = m_Impl->recognize(marshal_as(filepath)); + return gcnew AlprResultsNet(&results); } - List^ recognize(System::String^ filepath, List^ regionsOfInterest) { - std::vector rois = AlprHelper::ToVector(regionsOfInterest); - m_results = new std::vector(m_Impl->recognize(marshal_as(filepath), rois)); - return this->processResults(); + AlprResultsNet^ recognize(cli::array^ imageBuffer) { + std::vector p = AlprHelper::ToVector(imageBuffer); + AlprResults results = m_Impl->recognize(p); + return gcnew AlprResultsNet(&results); } - List^ recognize(cli::array^ imageBuffer) { - std::vector p = AlprHelper::ToVector(imageBuffer); - m_results = new std::vector(m_Impl->recognize(p)); - return this->processResults(); - } - - List^ recognize(cli::array^ imageBuffer, List^ regionsOfInterest) { - std::vector rois = AlprHelper::ToVector(regionsOfInterest); - std::vector p = AlprHelper::ToVector(imageBuffer); - m_results = new std::vector(m_Impl->recognize(p, rois)); - return this->processResults(); - } bool isLoaded() { return m_Impl->isLoaded(); @@ -258,10 +298,10 @@ namespace openalprnet { return AlprHelper::ToManagedString(Alpr::getVersion()); } - System::String^ toJson() { - std::string json = m_Impl->toJson(*m_results, -1); - return AlprHelper::ToManagedString(json); - } +// System::String^ toJson(AlprResultsNet^ results) { +// std::string json = Alpr::toJson(marshal_as(results)); +// return AlprHelper::ToManagedString(json); +// } protected: // Deallocate the native object on the finalizer just in case no destructor is called @@ -271,20 +311,9 @@ namespace openalprnet { private: Alpr * m_Impl; - std::vector* m_results; int m_topN; bool m_detectRegion; System::String^ m_defaultRegion; - List^ processResults() { - std::vector& runList = *m_results; - std::vector::iterator itr; - List^ list = gcnew List(runList.size()); - for (itr = runList.begin(); itr != runList.end(); itr++) - { - list->Add(gcnew AlprResultNet(&*itr)); - } - return list; - } }; } \ No newline at end of file diff --git a/openalprnet-cli/Program.cs b/openalprnet-cli/Program.cs index f6881e3..c7e65c5 100644 --- a/openalprnet-cli/Program.cs +++ b/openalprnet-cli/Program.cs @@ -109,7 +109,8 @@ namespace openalprnet_cli private static void PerformAlpr(AlprNet alpr, byte[] buffer, bool benchmark, bool writeJson) { var sw = Stopwatch.StartNew(); - var results = alpr.recognize(buffer); + sbyte[] signedBuffer = (sbyte[])(Array)buffer; + var results = alpr.recognize(signedBuffer); sw.Stop(); if (benchmark) { @@ -118,14 +119,14 @@ namespace openalprnet_cli if (writeJson) { - Console.WriteLine(alpr.toJson()); + //Console.WriteLine(alpr.toJson()); } else { 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); foreach (var plate in result.topNPlates) {