/* * 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 . */ #include "stdafx.h" #include "openalpr-net.h" #include "alpr.h" #include #include #include #using //#include #include using namespace System; using namespace msclr::interop; using namespace System::Collections::Generic; using namespace System::Runtime::InteropServices; namespace openalprnet { ref class StringConverter { public: 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::copy(first, last, result.begin()); return result; } static System::String^ ToManagedString(std::string s) { return gcnew String(s.c_str()); } static std::string ToStlString(System::String^ s) { IntPtr ptr = Marshal::StringToHGlobalAnsi(s); if(ptr != IntPtr::Zero) { std::string tmp(reinterpret_cast(static_cast(ptr))); Marshal::FreeHGlobal(ptr); return tmp; } return std::string(); } }; public ref class AlprPlateNet { public: AlprPlateNet(AlprPlate plate){ //_characters = marshal_as(plate.characters); m_characters = StringConverter::ToManagedString(plate.characters); m_overall_confidence=plate.overall_confidence; m_matches_template=plate.matches_template; } property System::String^ characters { System::String^ get() { return m_characters; } } property float overall_confidence { float get() { return m_overall_confidence; } } property bool matches_template { bool get() { return m_matches_template; } } private: System::String^ m_characters; float m_overall_confidence; bool m_matches_template; }; public ref class AlprResultNet { public: AlprResultNet() : m_Impl( new AlprResult ) {} AlprResultNet(AlprResult* result) : m_Impl( result ) {} property int requested_topn { int get() { return m_Impl->requested_topn; } } property int regionConfidence { int get() { return m_Impl->regionConfidence; } } property System::String^ region { System::String^ get() { return StringConverter::ToManagedString(m_Impl->region); } } property int result_count { int get() { return m_Impl->result_count; } } property AlprPlateNet^ bestPlate { AlprPlateNet^ get() { AlprPlateNet^ result = gcnew AlprPlateNet(m_Impl->bestPlate); return result; } } property List^ topNPlates { List^ get() { List^ list = gcnew List(m_Impl->topNPlates.size()); for (std::vector::iterator itr = m_Impl->topNPlates.begin(); itr != m_Impl->topNPlates.end(); itr++) { list->Add(gcnew AlprPlateNet(*itr)); } return list; } } property float processing_time_ms { float get() { return m_Impl->processing_time_ms; } } /* AlprCoordinate plate_points[4]; */ private: AlprResult * m_Impl; }; public ref class AlprNet { public: // Allocate the native object on the C++ Heap via a constructor AlprNet(System::String^ country, System::String^ configFile) : m_Impl( new Alpr(marshal_as(country), marshal_as(configFile)) ) { } // Deallocate the native object on a destructor ~AlprNet(){ delete m_Impl; } property int TopN { int get() { return m_topN; } void set( int topn ){ m_topN = topn; m_Impl->setTopN(topn); } } property bool DetectRegion { bool get() { return m_detectRegion; } void set( bool detectRegion ) { m_detectRegion = detectRegion; m_Impl->setDetectRegion(detectRegion); } } property System::String^ DefaultRegion { System::String^ get() { return m_defaultRegion; } void set( System::String^ region ){ m_defaultRegion = region; m_Impl->setDefaultRegion(marshal_as(region)); } } //std::vector recognize(std::string filepath); //std::vector recognize(std::string filepath, std::vector regionsOfInterest); //std::vector recognize(std::vector imageBuffer); //std::vector recognize(std::vector imageBuffer, std::vector regionsOfInterest); List^ recognize(System::String^ filepath) { m_results = new std::vector(m_Impl->recognize(marshal_as(filepath))); 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; } List^ recognize(cli::array^ imageBuffer) { std::vector p = StringConverter::ToVector(imageBuffer); m_results = new std::vector(m_Impl->recognize(p)); 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; } bool isLoaded() { return m_Impl->isLoaded(); } static System::String^ getVersion() { return StringConverter::ToManagedString(Alpr::getVersion()); } System::String^ toJson() { std::string json = m_Impl->toJson(*m_results, -1); return StringConverter::ToManagedString(json); } protected: // Deallocate the native object on the finalizer just in case no destructor is called !AlprNet() { delete m_Impl; } private: Alpr * m_Impl; std::vector* m_results; int m_topN; bool m_detectRegion; System::String^ m_defaultRegion; }; }