Added plate_points property to AlprResultNet class

This commit is contained in:
Masroor Ehsan
2015-01-05 16:07:04 +06:00
parent cd5b5694fb
commit a2080d98e1

View File

@@ -1,257 +1,290 @@
/* /*
* Copyright (c) 2015 Dr. Masroor Ehsan * Copyright (c) 2015 Dr. Masroor Ehsan
* *
* This file is part of OpenAlpr.Net. * This file is part of OpenAlpr.Net.
* *
* OpenAlpr.Net is free software: you can redistribute it and/or modify * OpenAlpr.Net is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License * it under the terms of the GNU Affero General Public License
* version 3 as published by the Free Software Foundation * version 3 as published by the Free Software Foundation
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "stdafx.h" #include "stdafx.h"
#include "openalpr-net.h" #include "openalpr-net.h"
#include "alpr.h" #include "alpr.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <windows.h> #include <windows.h>
#using <mscorlib.dll> #using <mscorlib.dll>
//#include <msclr\marshal.h> //#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h> #include <msclr\marshal_cppstd.h>
using namespace System; using namespace System;
using namespace msclr::interop; 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;
namespace openalprnet {
namespace openalprnet {
ref class StringConverter
{ private ref class AlprHelper sealed
public: {
static std::vector<unsigned char> ToVector(array<unsigned char>^ src) public:
{ static std::vector<unsigned char> ToVector(array<unsigned char>^ src)
std::vector<unsigned char> result(src->Length); {
pin_ptr<unsigned char> pin(&src[0]); std::vector<unsigned char> result(src->Length);
unsigned char *first(pin), *last(pin + src->Length); pin_ptr<unsigned char> pin(&src[0]);
std::copy(first, last, result.begin()); unsigned char *first(pin), *last(pin + src->Length);
return result; std::copy(first, last, result.begin());
} return result;
}
static System::String^ ToManagedString(std::string s)
{ static std::vector<AlprRegionOfInterest> ToVector(List<System::Drawing::Rectangle>^ src)
return gcnew String(s.c_str()); {
} std::vector<AlprRegionOfInterest> result;
static std::string ToStlString(System::String^ s) for each(System::Drawing::Rectangle^ rect in src)
{ {
IntPtr ptr = Marshal::StringToHGlobalAnsi(s); AlprRegionOfInterest roi;
if(ptr != IntPtr::Zero) roi.x = rect->X;
{ roi.y = rect->Y;
std::string tmp(reinterpret_cast<char*>(static_cast<void*>(ptr))); roi.height = rect->Height;
Marshal::FreeHGlobal(ptr); roi.width = rect->Width;
return tmp; result.push_back(roi);
} }
return std::string();
} return result;
}; }
public ref class AlprPlateNet static System::String^ ToManagedString(std::string s)
{ {
public: return gcnew String(s.c_str());
AlprPlateNet(AlprPlate plate){ }
//_characters = marshal_as<System::String^>(plate.characters);
m_characters = StringConverter::ToManagedString(plate.characters); static std::string ToStlString(System::String^ s)
m_overall_confidence=plate.overall_confidence; {
m_matches_template=plate.matches_template; IntPtr ptr = Marshal::StringToHGlobalAnsi(s);
} if(ptr != IntPtr::Zero)
{
property System::String^ characters { std::string tmp(reinterpret_cast<char*>(static_cast<void*>(ptr)));
System::String^ get() { Marshal::FreeHGlobal(ptr);
return m_characters; return tmp;
} }
} return std::string();
}
property float overall_confidence { };
float get() {
return m_overall_confidence; public ref class AlprPlateNet sealed
} {
} public:
AlprPlateNet(AlprPlate plate){
property bool matches_template { //_characters = marshal_as<System::String^>(plate.characters);
bool get() { m_characters = AlprHelper::ToManagedString(plate.characters);
return m_matches_template; m_overall_confidence=plate.overall_confidence;
} m_matches_template=plate.matches_template;
} }
private: property System::String^ characters {
System::String^ m_characters; System::String^ get() {
float m_overall_confidence; return m_characters;
bool m_matches_template; }
}; }
public ref class AlprResultNet property float overall_confidence {
{ float get() {
public: return m_overall_confidence;
AlprResultNet() : m_Impl( new AlprResult ) {} }
}
AlprResultNet(AlprResult* result) : m_Impl( result ) {}
property bool matches_template {
property int requested_topn { bool get() {
int get() { return m_matches_template;
return m_Impl->requested_topn; }
} }
}
private:
property int regionConfidence { System::String^ m_characters;
int get() { float m_overall_confidence;
return m_Impl->regionConfidence; bool m_matches_template;
} };
}
public ref class AlprResultNet sealed
property System::String^ region { {
System::String^ get() { public:
return StringConverter::ToManagedString(m_Impl->region); AlprResultNet() : m_Impl( new AlprResult ) {}
}
} AlprResultNet(AlprResult* result) : m_Impl( result ) {}
property int result_count { property int requested_topn {
int get() { int get() {
return m_Impl->result_count; return m_Impl->requested_topn;
} }
} }
property AlprPlateNet^ bestPlate { property int regionConfidence {
AlprPlateNet^ get() { int get() {
AlprPlateNet^ result = gcnew AlprPlateNet(m_Impl->bestPlate); return m_Impl->regionConfidence;
return result; }
} }
}
property System::String^ region {
property List<AlprPlateNet^>^ topNPlates { System::String^ get() {
List<AlprPlateNet^>^ get() { return AlprHelper::ToManagedString(m_Impl->region);
List<AlprPlateNet^>^ list = gcnew List<AlprPlateNet^>(m_Impl->topNPlates.size()); }
for (std::vector<AlprPlate>::iterator itr = m_Impl->topNPlates.begin(); itr != m_Impl->topNPlates.end(); itr++) }
{
list->Add(gcnew AlprPlateNet(*itr)); property int result_count {
} int get() {
return list; return m_Impl->result_count;
} }
} }
property float processing_time_ms { property AlprPlateNet^ bestPlate {
float get() { AlprPlateNet^ get() {
return m_Impl->processing_time_ms; AlprPlateNet^ result = gcnew AlprPlateNet(m_Impl->bestPlate);
} return result;
} }
}
/*
AlprCoordinate plate_points[4]; property List<System::Drawing::Point>^ plate_points {
*/ List<System::Drawing::Point>^ get() {
private: List<System::Drawing::Point>^ list = gcnew List<System::Drawing::Point>(4);
AlprResult * m_Impl; for (int i = 0; i < 4; i++)
}; {
list->Add(System::Drawing::Point(m_Impl->plate_points[i].x, m_Impl->plate_points[i].y));
public ref class AlprNet { }
public: return list;
// Allocate the native object on the C++ Heap via a constructor }
AlprNet(System::String^ country, System::String^ configFile) : m_Impl( new Alpr(marshal_as<std::string>(country), marshal_as<std::string>(configFile)) ) { } }
// Deallocate the native object on a destructor property List<AlprPlateNet^>^ topNPlates {
~AlprNet(){ List<AlprPlateNet^>^ get() {
delete m_Impl; List<AlprPlateNet^>^ list = gcnew List<AlprPlateNet^>(m_Impl->topNPlates.size());
} for (std::vector<AlprPlate>::iterator itr = m_Impl->topNPlates.begin(); itr != m_Impl->topNPlates.end(); itr++)
{
property int TopN { list->Add(gcnew AlprPlateNet(*itr));
int get() { }
return m_topN; return list;
} }
void set( int topn ){ }
m_topN = topn;
m_Impl->setTopN(topn); property float processing_time_ms {
} float get() {
} return m_Impl->processing_time_ms;
}
property bool DetectRegion { }
bool get() {
return m_detectRegion; private:
} AlprResult * m_Impl;
void set( bool detectRegion ) { };
m_detectRegion = detectRegion;
m_Impl->setDetectRegion(detectRegion); public ref class AlprNet sealed
} {
} public:
// Allocate the native object on the C++ Heap via a constructor
property System::String^ DefaultRegion { AlprNet(System::String^ country, System::String^ configFile) : m_Impl( new Alpr(marshal_as<std::string>(country), marshal_as<std::string>(configFile)) ) { }
System::String^ get() {
return m_defaultRegion; // Deallocate the native object on a destructor
} ~AlprNet(){
void set( System::String^ region ){ delete m_Impl;
m_defaultRegion = region; }
m_Impl->setDefaultRegion(marshal_as<std::string>(region));
} property int TopN {
} int get() {
return m_topN;
//std::vector<AlprResult> recognize(std::string filepath); }
//std::vector<AlprResult> recognize(std::string filepath, std::vector<AlprRegionOfInterest> regionsOfInterest); void set( int topn ){
//std::vector<AlprResult> recognize(std::vector<unsigned char> imageBuffer); m_topN = topn;
//std::vector<AlprResult> recognize(std::vector<unsigned char> imageBuffer, std::vector<AlprRegionOfInterest> regionsOfInterest); m_Impl->setTopN(topn);
List<AlprResultNet^>^ recognize(System::String^ filepath) { }
m_results = new std::vector<AlprResult>(m_Impl->recognize(marshal_as<std::string>(filepath))); }
std::vector<AlprResult>& runList = *m_results;
std::vector<AlprResult>::iterator itr; property bool DetectRegion {
List<AlprResultNet^>^ list = gcnew List<AlprResultNet^>(runList.size()); bool get() {
for (itr = runList.begin(); itr != runList.end(); itr++) return m_detectRegion;
{ }
list->Add(gcnew AlprResultNet(&*itr)); void set( bool detectRegion ) {
} m_detectRegion = detectRegion;
return list; m_Impl->setDetectRegion(detectRegion);
} }
}
List<AlprResultNet^>^ recognize(cli::array<unsigned char>^ imageBuffer) {
std::vector<unsigned char> p = StringConverter::ToVector(imageBuffer); property System::String^ DefaultRegion {
m_results = new std::vector<AlprResult>(m_Impl->recognize(p)); System::String^ get() {
std::vector<AlprResult>& runList = *m_results; return m_defaultRegion;
std::vector<AlprResult>::iterator itr; }
List<AlprResultNet^>^ list = gcnew List<AlprResultNet^>(runList.size()); void set( System::String^ region ){
for (itr = runList.begin(); itr != runList.end(); itr++) m_defaultRegion = region;
{ m_Impl->setDefaultRegion(marshal_as<std::string>(region));
list->Add(gcnew AlprResultNet(&*itr)); }
} }
return list;
} List<AlprResultNet^>^ recognize(System::String^ filepath) {
m_results = new std::vector<AlprResult>(m_Impl->recognize(marshal_as<std::string>(filepath)));
bool isLoaded() { return this->processResults();
return m_Impl->isLoaded(); }
}
List<AlprResultNet^>^ recognize(System::String^ filepath, List<System::Drawing::Rectangle>^ regionsOfInterest) {
static System::String^ getVersion() { std::vector<AlprRegionOfInterest> rois = AlprHelper::ToVector(regionsOfInterest);
return StringConverter::ToManagedString(Alpr::getVersion()); m_results = new std::vector<AlprResult>(m_Impl->recognize(marshal_as<std::string>(filepath), rois));
} return this->processResults();
}
System::String^ toJson() {
std::string json = m_Impl->toJson(*m_results, -1); List<AlprResultNet^>^ recognize(cli::array<unsigned char>^ imageBuffer) {
return StringConverter::ToManagedString(json); std::vector<unsigned char> p = AlprHelper::ToVector(imageBuffer);
} m_results = new std::vector<AlprResult>(m_Impl->recognize(p));
return this->processResults();
protected: }
// Deallocate the native object on the finalizer just in case no destructor is called
!AlprNet() { List<AlprResultNet^>^ recognize(cli::array<unsigned char>^ imageBuffer, List<System::Drawing::Rectangle>^ regionsOfInterest) {
delete m_Impl; 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));
private: return this->processResults();
Alpr * m_Impl; }
std::vector<AlprResult>* m_results;
int m_topN; bool isLoaded() {
bool m_detectRegion; return m_Impl->isLoaded();
System::String^ m_defaultRegion; }
};
static System::String^ getVersion() {
return AlprHelper::ToManagedString(Alpr::getVersion());
}
System::String^ toJson() {
std::string json = m_Impl->toJson(*m_results, -1);
return AlprHelper::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<AlprResult>* m_results;
int m_topN;
bool m_detectRegion;
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;
}
};
} }