mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-05 23:36:54 +08:00
Merge pull request #234 from WitzHsiao/go_binding
initial a wrapper for golang
This commit is contained in:
36
src/bindings/go/main.go
Normal file
36
src/bindings/go/main.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
import "github.com/openalpr/openalpr/src/bindings/go/openalpr"
|
||||
|
||||
func main() {
|
||||
alpr := openalpr.NewAlpr("us", "", "../../../runtime_data")
|
||||
defer alpr.Unload()
|
||||
|
||||
if !alpr.IsLoaded() {
|
||||
fmt.Println("OpenAlpr failed to load!")
|
||||
return
|
||||
}
|
||||
alpr.SetTopN(20)
|
||||
|
||||
fmt.Println(alpr.IsLoaded())
|
||||
fmt.Println(openalpr.GetVersion())
|
||||
|
||||
resultFromFilePath, err := alpr.RecognizeByFilePath("lp.jpg")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Printf("%+v\n", resultFromFilePath)
|
||||
fmt.Printf("\n\n\n")
|
||||
|
||||
imageBytes, err := ioutil.ReadFile("lp.jpg")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
resultFromBlob, err := alpr.RecognizeByBlob(imageBytes)
|
||||
fmt.Printf("%+v\n", resultFromBlob)
|
||||
}
|
12
src/bindings/go/make.sh
Normal file
12
src/bindings/go/make.sh
Normal file
@@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
|
||||
OPENALPR_INCLUDE_DIR=$(pwd)/../../openalpr
|
||||
OPENALPR_LIB_DIR=$(pwd)/../../build/openalpr
|
||||
|
||||
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.:${OPENALPR_LIB_DIR}
|
||||
|
||||
g++ -Wall -L${OPENALPR_LIB_DIR} -I${OPENALPR_INCLUDE_DIR} -shared -fPIC -o libopenalprgo.so openalprgo.cpp -lopenalpr
|
||||
|
||||
(cd openalpr && go install)
|
||||
|
||||
go run main.go
|
138
src/bindings/go/openalpr/openalpr.go
Normal file
138
src/bindings/go/openalpr/openalpr.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package openalpr
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -I${SRCDIR}/..
|
||||
#cgo LDFLAGS: -L${SRCDIR}/.. -lopenalprgo
|
||||
#include <openalprgo.h>
|
||||
#include <stdlib.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Alpr struct {
|
||||
//Country string
|
||||
//configFile string
|
||||
//runtimeDir string
|
||||
|
||||
cAlpr C.Alpr
|
||||
}
|
||||
|
||||
type AlprResults struct {
|
||||
EpochTime int64 `json:"epoch_time"`
|
||||
ImgWidth int `json:"img_witdh"`
|
||||
ImgHeight int `json:"img_height"`
|
||||
TotalProcessingTimeMs float32 `json:"processing_time_ms"`
|
||||
Plates []AlprPlateResult `json:"results"`
|
||||
RegionsOfInterest []AlprRegionOfInterest `json:"regions_of_interest"`
|
||||
}
|
||||
|
||||
type AlprPlate struct {
|
||||
Characters string `json:"plate"`
|
||||
OverallConfidence float32 `json:"confidence"`
|
||||
MatchesTemplate bool
|
||||
}
|
||||
|
||||
type AlprRegionOfInterest struct {
|
||||
X int `json:"x"`
|
||||
Y int `json:"y"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
}
|
||||
|
||||
type AlprCoordinate struct {
|
||||
X int `json:"x"`
|
||||
Y int `json:"y"`
|
||||
}
|
||||
|
||||
type AlprPlateResult struct {
|
||||
RequestedTopN int `json:"requested_topn"`
|
||||
BestPlate string `json:"plate"`
|
||||
TopNPlates []AlprPlate `json:"candidates"`
|
||||
ProcessingTimeMs float32 `json:"processing_time_ms"`
|
||||
PlatePoints []AlprCoordinate `json:"coordinates"`
|
||||
PlateIndex int `json:"plate_index"`
|
||||
RegionConfidence int `json:"region_confidence"`
|
||||
Region string `json:"region"`
|
||||
}
|
||||
|
||||
func bool2Cint(b bool) C.int {
|
||||
if b {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func cint2Bool(i C.int) bool {
|
||||
if i == 0 {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func NewAlpr(country string, configFile string, runtimeDir string) *Alpr {
|
||||
cstrCountry := C.CString(country)
|
||||
cstrConfigFile := C.CString(configFile)
|
||||
cstrRuntimeDir := C.CString(runtimeDir)
|
||||
defer C.free(unsafe.Pointer(cstrCountry))
|
||||
defer C.free(unsafe.Pointer(cstrConfigFile))
|
||||
defer C.free(unsafe.Pointer(cstrRuntimeDir))
|
||||
|
||||
alpr := C.AlprInit(cstrCountry, cstrConfigFile, cstrRuntimeDir)
|
||||
return &Alpr{cAlpr: alpr}
|
||||
}
|
||||
|
||||
func (alpr *Alpr) SetDetectRegion(detectRegion bool) {
|
||||
C.SetDetectRegion(alpr.cAlpr, bool2Cint(detectRegion))
|
||||
}
|
||||
|
||||
func (alpr *Alpr) SetTopN(topN int) {
|
||||
C.SetTopN(alpr.cAlpr, C.int(topN))
|
||||
}
|
||||
|
||||
func (alpr *Alpr) SetDefaultRegion(region string) {
|
||||
cstrRegion := C.CString(region)
|
||||
defer C.free(unsafe.Pointer(cstrRegion))
|
||||
C.SetDefaultRegion(alpr.cAlpr, cstrRegion)
|
||||
}
|
||||
|
||||
func (alpr *Alpr) IsLoaded() bool {
|
||||
return cint2Bool(C.IsLoaded(alpr.cAlpr))
|
||||
}
|
||||
|
||||
func GetVersion() string {
|
||||
return C.GoString(C.GetVersion())
|
||||
}
|
||||
|
||||
func (alpr *Alpr) RecognizeByFilePath(filePath string) (AlprResults, error) {
|
||||
cstrFilePath := C.CString(filePath)
|
||||
defer C.free(unsafe.Pointer(cstrFilePath))
|
||||
stringResult := C.GoString(C.RecognizeByFilePath(alpr.cAlpr, cstrFilePath))
|
||||
fmt.Println(stringResult)
|
||||
|
||||
var results AlprResults
|
||||
err := json.Unmarshal([]byte(stringResult), &results)
|
||||
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (alpr *Alpr) RecognizeByBlob(imageBytes []byte) (AlprResults, error) {
|
||||
stringImageBytes := string(imageBytes)
|
||||
cstrImageBytes := C.CString(stringImageBytes)
|
||||
defer C.free(unsafe.Pointer(cstrImageBytes))
|
||||
stringResult := C.GoString(C.RecognizeByBlob(alpr.cAlpr, cstrImageBytes, C.int(len(imageBytes))))
|
||||
|
||||
var results AlprResults
|
||||
err := json.Unmarshal([]byte(stringResult), &results)
|
||||
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (alpr *Alpr) Unload() {
|
||||
C.Unload(alpr.cAlpr)
|
||||
}
|
76
src/bindings/go/openalprgo.cpp
Normal file
76
src/bindings/go/openalprgo.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <alpr.h>
|
||||
#include "openalprgo.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
// Microsoft
|
||||
#define OPENALPR_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
// do nothing
|
||||
#define OPENALPR_EXPORT
|
||||
#endif
|
||||
|
||||
//using namespace alpr;
|
||||
OPENALPR_EXPORT Alpr AlprInit(char* country, char* configFile, char* runtimeDir) {
|
||||
alpr::Alpr* alpr = new alpr::Alpr(country, configFile, runtimeDir);
|
||||
return (void*)alpr;
|
||||
}
|
||||
|
||||
OPENALPR_EXPORT void SetDetectRegion(Alpr alpr, int detectRegion) {
|
||||
alpr::Alpr* cxxalpr = (alpr::Alpr*) alpr;
|
||||
cxxalpr->setDetectRegion(detectRegion);
|
||||
}
|
||||
|
||||
OPENALPR_EXPORT void SetTopN(Alpr alpr, int topN) {
|
||||
alpr::Alpr* cxxalpr = (alpr::Alpr*) alpr;
|
||||
cxxalpr->setTopN(topN);
|
||||
}
|
||||
|
||||
OPENALPR_EXPORT void SetDefaultRegion(Alpr alpr, char* region) {
|
||||
alpr::Alpr* cxxalpr = (alpr::Alpr*) alpr;
|
||||
cxxalpr->setDefaultRegion(region);
|
||||
}
|
||||
|
||||
OPENALPR_EXPORT int IsLoaded(Alpr alpr) {
|
||||
alpr::Alpr* cxxalpr = (alpr::Alpr*) alpr;
|
||||
return cxxalpr->isLoaded();
|
||||
}
|
||||
|
||||
OPENALPR_EXPORT void Unload(Alpr alpr) {
|
||||
alpr::Alpr* cxxalpr = (alpr::Alpr*) alpr;
|
||||
delete cxxalpr;
|
||||
}
|
||||
|
||||
OPENALPR_EXPORT char* RecognizeByFilePath(Alpr alpr, char* filePath) {
|
||||
alpr::Alpr* cxxalpr = (alpr::Alpr*) alpr;
|
||||
alpr::AlprResults result = cxxalpr->recognize(filePath);
|
||||
|
||||
std::string resultString = alpr::Alpr::toJson(result);
|
||||
char *cstr = new char[resultString.length() + 1];
|
||||
strcpy(cstr, resultString.c_str());
|
||||
return cstr;
|
||||
}
|
||||
|
||||
OPENALPR_EXPORT char* RecognizeByBlob(Alpr alpr, char* imageBytes, int len) {
|
||||
alpr::Alpr* cxxalpr = (alpr::Alpr*) alpr;
|
||||
std::vector<char> vec(imageBytes, imageBytes + len);
|
||||
alpr::AlprResults result = cxxalpr->recognize(vec);
|
||||
|
||||
std::string resultString = alpr::Alpr::toJson(result);
|
||||
char *cstr = new char[resultString.length() + 1];
|
||||
strcpy(cstr, resultString.c_str());
|
||||
return cstr;
|
||||
}
|
||||
|
||||
OPENALPR_EXPORT char* GetVersion() {
|
||||
std::string version = alpr::Alpr::getVersion();
|
||||
char *cstr = new char[version.length() + 1];
|
||||
strcpy(cstr, version.c_str());
|
||||
return cstr;
|
||||
}
|
||||
}
|
17
src/bindings/go/openalprgo.h
Normal file
17
src/bindings/go/openalprgo.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
typedef void* Alpr;
|
||||
|
||||
Alpr AlprInit(char* country, char* configFile, char* runtimeDir);
|
||||
void SetDetectRegion(Alpr alpr, int detectRegion);
|
||||
void SetTopN(Alpr alpr, int topN);
|
||||
void SetDefaultRegion(Alpr alpr, char* region);
|
||||
int IsLoaded(Alpr alpr);
|
||||
void Unload(Alpr alpr);
|
||||
char* RecognizeByFilePath(Alpr alpr, char* filePath);
|
||||
char* RecognizeByBlob(Alpr alpr, char* imageBytes, int len);
|
||||
char* GetVersion();
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user