Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General update to make it work with the new version of OpenALPR (2.3.0) #22

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
build
!lib/*
!release/**
!openalpr_runtime/**
!openalpr_runtime/**
*.log
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ node-openalpr

This package binds [OpenALPR](https://github.com/openalpr/openalpr) with Node.js

Version: 1.1.0 - Released January 4th, 2016
Version: 1.1.3 - Released August 2nd, 2017

```
Changelog:

1.1.3 - Updates to be compatible with OpenALPR 2.3.0
1.1.2 - Add country feature
1.1.1 - Fix for building on OSX
1.1.0 - Updated OpenALPR to version 2.2, should fix Linux and OSX builds
1.0.9 - Updated OpenALPR build and US region data
Expand Down Expand Up @@ -53,7 +55,12 @@ function identify (id, path) {
}));
}

openalpr.Start ();
// Basic starting
openalpr.Start();

// European detection starting
// openalpr.Start(null, null, null, true, 'eu');

openalpr.GetVersion ();

for (var i = 0; i < 350; i++) {
Expand Down
Empty file modified includes/TRexpp.h
100755 → 100644
Empty file.
9 changes: 9 additions & 0 deletions includes/alpr.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ namespace alpr
Alpr(const std::string country, const std::string configFile = "", const std::string runtimeDir = "");
virtual ~Alpr();

// Set the country used for plate recognition
void setCountry(std::string country);

// Update the prewarp setting without reloading the library
void setPrewarp(std::string prewarp_config);
// Update the detection mask without reloading the library
void setMask(unsigned char* pixelData, int bytesPerPixel, int imgWidth, int imgHeight);

void setDetectRegion(bool detectRegion);
void setTopN(int topN);
void setDefaultRegion(std::string region);
Expand All @@ -155,6 +163,7 @@ namespace alpr


static std::string toJson(const AlprResults results);
static std::string toJson(const AlprPlateResult result);
static AlprResults fromJson(std::string json);

bool isLoaded();
Expand Down
16 changes: 11 additions & 5 deletions includes/alpr_impl.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@

#include "licenseplatecandidate.h"
#include "../statedetection/state_detector.h"
#include "segmentation/charactersegmenter.h"
#include "ocr.h"
#include "ocr/ocr.h"
#include "ocr/ocrfactory.h"

#include "constants.h"

Expand Down Expand Up @@ -83,20 +83,24 @@ namespace alpr
AlprFullDetails recognizeFullDetails(cv::Mat img, std::vector<cv::Rect> regionsOfInterest);

AlprResults recognize( std::vector<char> imageBytes );
AlprResults recognize( std::vector<char> imageBytes, std::vector<AlprRegionOfInterest> regionsOfInterest );
AlprResults recognize( std::vector<char> imageBytes, std::vector<AlprRegionOfInterest> regionsOfInterest );
AlprResults recognize( unsigned char* pixelData, int bytesPerPixel, int imgWidth, int imgHeight, std::vector<AlprRegionOfInterest> regionsOfInterest );
AlprResults recognize( cv::Mat img );
AlprResults recognize( cv::Mat img, std::vector<cv::Rect> regionsOfInterest );

void applyRegionTemplate(AlprPlateResult* result, std::string region);

AlprFullDetails analyzeSingleCountry(cv::Mat colorImg, cv::Mat grayImg, std::vector<cv::Rect> regionsOfInterest);

void setCountry(std::string country);
void setPrewarp(std::string prewarp_config);
void setMask(unsigned char* pixelData, int bytesPerPixel, int imgWidth, int imgHeight);

void setDetectRegion(bool detectRegion);
void setTopN(int topn);
void setDefaultRegion(std::string region);

static std::string toJson( const AlprResults results );
static std::string toJson( const AlprPlateResult result );

static AlprResults fromJson(std::string json);
static std::string getVersion();

Expand All @@ -116,6 +120,8 @@ namespace alpr
bool detectRegion;
std::string defaultRegion;

void loadRecognizers();

cv::Mat getCharacterTransformMatrix(PipelineData* pipeline_data );
std::vector<AlprCoordinate> getCharacterPoints(cv::Rect char_rect, cv::Mat transmtx);
std::vector<cv::Rect> convertRects(std::vector<AlprRegionOfInterest> regionsOfInterest);
Expand Down
Empty file modified includes/binarize_wolf.h
100755 → 100644
Empty file.
Empty file modified includes/cjson.h
100755 → 100644
Empty file.
Empty file modified includes/colorfilter.h
100755 → 100644
Empty file.
9 changes: 9 additions & 0 deletions includes/config.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace alpr
Config(const std::string country, const std::string config_file = "", const std::string runtime_dir = "");
virtual ~Config();

bool load_countries(const std::string countries);

bool loaded;

std::string config_file_path;
Expand All @@ -55,8 +57,14 @@ namespace alpr
int maxDetectionInputWidth;
int maxDetectionInputHeight;

float contrastDetectionThreshold;

bool skipDetection;

std::string detection_mask_image;

int analysis_count;

bool auto_invert;
bool always_invert;

Expand Down Expand Up @@ -153,6 +161,7 @@ namespace alpr
float stateIdImagePercent;

std::vector<std::string> parse_country_string(std::string countries);
bool country_is_loaded(std::string country);

void loadCommonValues(std::string configFile);
void loadCountryValues(std::string configFile, std::string country);
Expand Down
5 changes: 5 additions & 0 deletions includes/constants.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#define CASCADE_DIR "/region/"
#define POSTPROCESS_DIR "/postprocess"

#define DEFAULT_SHARE_DIR INSTALL_PREFIX "/share/openalpr"

#define DEFAULT_RUNTIME_DATA_DIR DEFAULT_SHARE_DIR "/runtime_data"
#define CONFIG_FILE_TEMPLATE_LOCATION DEFAULT_SHARE_DIR "/config/openalpr.defaults.conf"

#ifndef DEFAULT_CONFIG_FILE
#define DEFAULT_CONFIG_FILE "/etc/openalpr/openalpr.conf"
#endif
Expand Down
2 changes: 0 additions & 2 deletions includes/licenseplatecandidate.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "edges/platelines.h"
#include "transformation.h"
#include "textdetection/characteranalysis.h"
#include "segmentation/charactersegmenter.h"
#include "edges/platecorners.h"
#include "config.h"
#include "pipeline_data.h"
Expand All @@ -55,7 +54,6 @@ namespace alpr
PipelineData* pipeline_data;
Config* config;

CharacterSegmenter* charSegmenter;

cv::Mat filterByCharacterHue(std::vector<std::vector<cv::Point> > charRegionContours);
std::vector<cv::Point> findPlateCorners(cv::Mat inputImage, PlateLines plateLines, CharacterAnalysis textAnalysis); // top-left, top-right, bottom-right, bottom-left
Expand Down
Empty file modified includes/motiondetector.h
100755 → 100644
Empty file.
47 changes: 20 additions & 27 deletions includes/ocr.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,37 @@
*/

#ifndef OPENALPR_OCR_H
#define OPENALPR_OCR_H
#define OPENALPR_OCR_H

#include <iostream>
#include <stdio.h>

#include "utility.h"
#include "postprocess/postprocess.h"
#include "config.h"
#include "pipeline_data.h"

#include "constants.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "support/filesystem.h"
#include "support/version.h"

#include "tesseract/baseapi.h"

namespace alpr
{

class OCR
struct OcrChar
{
std::string letter;
int char_index;
float confidence;
};

class OCR {
public:
OCR(Config* config);
virtual ~OCR();

public:
OCR(Config* config);
virtual ~OCR();

void performOCR(PipelineData* pipeline_data);

PostProcess postProcessor;
void performOCR(PipelineData* pipeline_data);

private:
Config* config;
PostProcess postProcessor;

tesseract::TessBaseAPI tesseract;
protected:
virtual std::vector<OcrChar> recognize_line(int line_index, PipelineData* pipeline_data)=0;
virtual void segment(PipelineData* pipeline_data)=0;

Config* config;

};

}

#endif // OPENALPR_OCR_H
#endif /* OPENALPR_OCR_H */

13 changes: 11 additions & 2 deletions includes/prewarp.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "config.h"
#include "utility.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "detection/detector.h"
#include "detection/detector_types.h"

namespace alpr
{
Expand All @@ -34,20 +34,29 @@ namespace alpr
PreWarp(Config* config);
virtual ~PreWarp();

void initialize(std::string prewarp_config);
void clear();

cv::Mat warpImage(cv::Mat image);
std::vector<cv::Point2f> projectPoints(std::vector<cv::Point2f> points, bool inverse);
std::vector<cv::Rect> projectRects(std::vector<cv::Rect> rects, int maxWidth, int maxHeight, bool inverse);
cv::Rect projectRect(cv::Rect rect, int maxWidth, int maxHeight, bool inverse);
void projectPlateRegions(std::vector<PlateRegion>& plateRegions, int maxWidth, int maxHeight, bool inverse);

void setTransform(float w, float h, float rotationx, float rotationy, float rotationz, float panX, float panY, float stretchX, float dist);

bool valid;

std::string toString();

private:
Config* config;
cv::Mat transform;

cv::Mat getTransform(float w, float h, float rotationx, float rotationy, float rotationz, float panX, float panY, float stretchX, float dist);

float w, h, rotationx, rotationy, rotationz, stretchX, dist, panX, panY;

cv::Mat findTransform(float w, float h, float rotationx, float rotationy, float rotationz, float panX, float panY, float stretchX, float dist);
};

}
Expand Down
35 changes: 33 additions & 2 deletions includes/result_aggregator.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


#include "alpr_impl.h"

#include "prewarp.h"

// Runs the analysis for multiple training sets, and aggregates the results into the best matches

Expand All @@ -37,22 +37,53 @@ struct PlateShapeInfo
namespace alpr
{

enum ResultMergeStrategy
{
MERGE_COMBINE, // Used when running an analysis multiple times for accuracy improvement. Merges results together
MERGE_PICK_BEST // Used when analyzing multiple countries. Chooses results from one country or the other
};

struct ResultPlateScore
{
AlprPlate plate;
float score_total;
int count;
};

struct ResultRegionScore
{
std::string region;
float confidence;

};

class ResultAggregator
{
public:
ResultAggregator();
ResultAggregator(ResultMergeStrategy merge_strategy, int topn, Config* config);

virtual ~ResultAggregator();

void addResults(AlprFullDetails full_results);

AlprFullDetails getAggregateResults();

cv::Mat applyImperceptibleChange(cv::Mat image, int index);

private:

int topn;
PreWarp* prewarp;
Config* config;

std::vector<AlprFullDetails> all_results;

PlateShapeInfo getShapeInfo(AlprPlateResult plate);

ResultMergeStrategy merge_strategy;

ResultRegionScore findBestRegion(std::vector<AlprPlateResult> cluster);

std::vector<std::vector<AlprPlateResult> > findClusters();
int overlaps(AlprPlateResult plate, std::vector<std::vector<AlprPlateResult> > clusters);
};
Expand Down
Empty file modified includes/transformation.h
100755 → 100644
Empty file.
7 changes: 7 additions & 0 deletions includes/utility.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,20 @@ namespace alpr

cv::Mat addLabel(cv::Mat input, std::string label);

// Given 4 random points (Point2f array), order them as top-left, top-right, bottom-right, bottom-left
// Useful for orienting a rotatedrect
std::vector<cv::Point> sortPolygonPoints(cv::Point2f* polygon_points, cv::Size surrounding_image);
cv::Point findClosestPoint(cv::Point2f* polygon_points, int num_points, cv::Point position);

int levenshteinDistance (const std::string &s1, const std::string &s2, int max);
std::string toString(int value);
std::string toString(long value);
std::string toString(unsigned int value);
std::string toString(float value);
std::string toString(double value);

std::string replaceAll(std::string str, const std::string& from, const std::string& to);

std::string &ltrim(std::string &s);
std::string &rtrim(std::string &s);
std::string &trim(std::string &s);
Expand Down
Loading