Skip to content

Commit 0ab9f8c

Browse files
committed
initial
1 parent 2ede6cd commit 0ab9f8c

File tree

10 files changed

+198
-26
lines changed

10 files changed

+198
-26
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
*.so

Diff for: Dockerfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM hybridgroup/gocv AS dev
2+
ENV GOPROXY=https://goproxy.cn
3+
WORKDIR /app
4+
ADD . /app
5+
RUN cd /app && go build -o goapp
6+
CMD [ "goapp" ]
7+
8+
# FROM alpine:latest
9+
# RUN apk update && \
10+
# apk add ca-certificates && \
11+
# update-ca-certificates && \
12+
# apk add --no-cache beanstalkd && \
13+
# rm -rf /var/cache/apk/*
14+
15+
# COPY --from=development /app/goapp /app
16+
# WORKDIR /app
17+
# EXPOSE 11300
18+
19+

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 介绍
2+
这是一个go语言写的 beanstalkd 工作者程序,使用CGo连接虹软人脸识别 linux C++ SDK。
3+
4+
# 待完成部分
5+
1. 现在做成docker镜像时有1.3G大小,大部分是opencv以及各种依赖环境。除非改为静态链接到opencv,才能把镜像缩小。

Diff for: face-test.jpg

67.7 KB
Loading

Diff for: face.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
#include <stdlib.h>
88
// #include <string.h>
99
// #include <time.h>
10-
#include<opencv.hpp>
10+
#include "cv.hpp"
11+
#include "highgui.h"
12+
#include "cxcore.h"
13+
1114
#include "face.h"
1215

16+
using namespace cv;
1317
using namespace std;
1418

1519
void * initEngine (const char* APPID, const char* SDKKEY) {
@@ -43,7 +47,7 @@ void * initEngine (const char* APPID, const char* SDKKEY) {
4347

4448

4549
ASF_FaceFeature extract(const char * filePath, void* handle) {
46-
cv::Mat originalImg = cv::imread(filePath, cv::IMREAD_COLOR);
50+
cv::Mat originalImg = cv::imread(filePath); //, cv::IMREAD_COLOR);
4751
// cv::Mat originalImg = cv::imread(filePath, cv::IMREAD_UNCHANGED);
4852
//图像裁剪,宽度做四字节对齐
4953
int width = originalImg.cols - originalImg.cols%4;

Diff for: face.go

+66-11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,27 @@ package main
22

33
/*
44
#cgo CFLAGS: -I./ArcSoft_ArcFace_Linux_x64_V3.0/inc -I/usr/include -I/usr/local/include
5-
#cgo CXXFLAGS: -std=c++11 -I./ArcSoft_ArcFace_Linux_x64_V3.0/inc -I/usr/include -I/usr/local/include -I/usr/local/include/opencv4 -I/usr/local/include/opencv4/opencv2
6-
#cgo LDFLAGS: -L${SRCDIR}/ArcSoft_ArcFace_Linux_x64_V3.0/lib/linux_x64 -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs -lstdc++ -larcsoft_face -larcsoft_face_engine -Wl,-rpath=./
5+
#cgo CXXFLAGS: -std=c++11 -I./ArcSoft_ArcFace_Linux_x64_V3.0/inc -I/usr/include -I/usr/local/include -I/usr/include/opencv
6+
#cgo LDFLAGS: -lstdc++ -L${SRCDIR}/ArcSoft_ArcFace_Linux_x64_V3.0/lib/linux_x64 -larcsoft_face -larcsoft_face_engine
7+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_core.a
8+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_imgproc.a
9+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_highgui.a
10+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_flann.a
11+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_photo.a
12+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_features2d.a
13+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_contrib.a
14+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_legacy.a
15+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_ts.a
16+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_objdetect.a
17+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_ocl.a
18+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_stitching.a
19+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_superres.a
20+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_calib3d.a
21+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libopencv_gpu.a
22+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libpng.a
23+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libjpeg.a
24+
#cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -l:libtiff.a
25+
#cgo LDFLAGS: -Wl,-rpath=./
726
#include "face.h"
827
*/
928
import "C"
@@ -13,23 +32,59 @@ import (
1332
// "log"
1433
"unsafe"
1534
)
16-
17-
func initEngine() unsafe.Pointer {
18-
APPID := "64p4MfjsFyVXQM21jbTmHQbCeDcv4wwgVVaQMZaAhmGD"
19-
SDKKEY := "8tGcaiv4BzcG232WmGKWTa1izjsXWSLTg8CKJsGvESX4"
2035

21-
return C.initEngine(C.CString(APPID), C.CString(SDKKEY))
36+
type Handle unsafe.Pointer
37+
38+
func initEngine() Handle {
39+
APPID := "D617np8jyKt1jN9gMr7ENbTJ1XvRWsQAsvdAehMKvqzr"
40+
SDKKEY := "BTeyKLCYBzfrNLQduGUwfyRZkDQU4RB2b3UJZjjuppLU"
41+
42+
return Handle(C.initEngine(C.CString(APPID), C.CString(SDKKEY)))
2243
}
23-
func extract(filePath string, handle unsafe.Pointer ) []byte{
24-
// APPID := "64p4MfjsFyVXQM21jbTmHQbCeDcv4wwgVVaQMZaAhmGD"
25-
// SDKKEY := "8tGcaiv4BzcG232WmGKWTa1izjsXWSLTg8CKJsGvESX4"
44+
45+
func extract(filePath string, handle Handle ) []byte{
2646

2747
// handle := C.initEngine(C.CString(APPID), C.CString(SDKKEY))
28-
feature := C.extract(C.CString(filePath), handle)
48+
feature := C.extract(C.CString(filePath), unsafe.Pointer(handle))
2949

3050
if feature.featureSize > 0 {
3151
return C.GoBytes(unsafe.Pointer(feature.feature), feature.featureSize)
3252
}else{
3353
return nil
3454
}
55+
}
56+
57+
type EnginePool struct {
58+
cached chan Handle
59+
size int
60+
}
61+
62+
func NewEnginePool(size int) *EnginePool {
63+
x := EnginePool{}
64+
x.cached = make(chan Handle, size)
65+
x.size = size
66+
return &x
67+
}
68+
69+
func (c *EnginePool) Get() Handle {
70+
var res Handle
71+
select {
72+
case res = <-c.cached:
73+
// fmt.Println("---get--")
74+
default:
75+
// fmt.Println("---create one--")
76+
res = Handle(initEngine())
77+
}
78+
79+
return res
80+
}
81+
82+
func (p *EnginePool) Put(c Handle) {
83+
select {
84+
case p.cached <- c:
85+
// fmt.Println("---put--")
86+
default:
87+
c = nil
88+
// fmt.Println("---destroy--")
89+
}
3590
}

Diff for: face_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"strconv"
77
"bytes"
8+
"log"
89
)
910

1011
func TestInitEngine(t *testing.T) {
@@ -21,6 +22,7 @@ func TestExtract(t *testing.T) {
2122
featureStr := "0 128 250 68 0 0 160 65 192 75 193 188 247 97 131 189 110 2 2 62 205 127 186 61 232 47 120 189 171 83 77 60 165 60 210 189 183 66 187 60 102 7 9 61 103 87 30 189 181 246 109 189 85 90 100 61 145 166 223 189 117 219 193 188 26 253 48 190 196 232 28 58 48 31 15 61 226 248 188 59 113 64 199 61 225 154 209 61 65 13 201 188 86 10 97 189 86 127 120 61 229 201 183 189 68 210 76 189 162 125 157 189 200 180 207 187 178 196 195 189 99 83 205 188 154 212 104 61 122 191 16 189 26 48 162 61 145 105 241 189 173 146 240 187 219 166 142 61 191 173 244 61 100 174 225 189 252 207 153 61 231 74 150 189 127 234 100 188 73 50 60 61 53 202 0 61 23 160 249 59 206 144 57 188 102 213 132 61 31 249 192 188 112 140 131 188 249 224 152 189 19 194 134 188 214 176 121 61 37 89 50 59 38 178 122 189 126 116 63 187 47 195 96 59 70 206 30 61 183 119 159 61 104 77 12 61 0 195 19 61 134 172 133 188 220 224 183 61 28 129 45 189 112 21 54 189 11 75 224 189 44 60 159 187 133 44 229 59 137 110 52 61 81 110 46 188 219 153 238 61 92 205 148 189 164 84 153 187 61 61 8 61 117 230 247 59 110 64 22 61 84 149 248 189 163 234 19 190 40 6 222 61 63 71 245 60 239 103 177 61 33 35 194 186 196 49 41 60 236 5 178 61 37 97 117 61 67 208 156 61 240 233 184 189 93 237 210 60 213 92 187 189 92 155 21 61 80 245 169 189 236 41 196 60 56 17 231 188 179 239 15 60 176 104 134 60 104 121 18 189 15 54 144 189 139 71 251 60 225 196 75 190 104 192 14 188 217 29 135 188 218 223 236 188 43 70 70 189 197 26 137 189 51 152 56 61 149 217 234 60 175 86 58 189 4 105 228 188 219 148 84 61 61 146 55 60 85 13 131 61 208 63 181 189 107 111 116 61 211 110 115 189 206 0 27 188 112 113 137 59 234 61 205 59 37 254 8 62 161 55 188 60 230 31 86 62 33 192 26 61 245 169 253 188 129 232 182 61 205 158 12 190 58 203 132 189 196 159 174 61 181 155 151 61 56 184 188 61 240 93 246 189 10 96 31 189 206 15 183 188 219 139 35 61 112 67 138 61 24 166 232 61 231 128 5 60 177 243 44 189 159 47 160 61 53 241 189 188 4 96 235 60 94 80 220 60 31 200 36 188 157 225 197 61 147 51 174 188 188 107 154 189 183 129 140 189 63 172 21 189 252 160 18 59 14 184 125 60 171 51 245 188 86 106 244 189 7 51 160 60 6 47 74 189 50 137 244 60 203 106 161 60 205 121 62 189 72 232 159 61 7 113 171 61 10 76 188 188 88 228 229 189 114 73 117 60 63 128 150 189 4 0 146 189 141 93 21 187 9 204 202 188 27 124 75 60 229 132 178 61 247 46 159 189 171 146 215 187 131 61 76 189 171 209 75 61 69 48 172 186 8 77 237 59 37 141 27 189 200 153 36 189 194 188 163 61 97 129 195 188 81 129 239 189 70 81 189 188 35 113 55 60 44 210 175 187 41 63 157 189 232 0 33 190 14 156 242 61 40 64 136 188 199 118 56 189 245 159 183 188 139 74 151 189 169 29 10 61 148 197 128 61 41 220 35 189 27 48 247 60 220 146 130 187 181 141 193 60 191 234 184 188 85 226 200 61 80 107 255 61 93 50 90 189 64 69 86 61 6 170 161 58 91 12 118 61 169 4 12 61 106 222 113 61 125 211 190 60 21 200 56 189 247 2 205 189 245 194 194 189 113 243 53 61 18 150 66 189 57 127 88 61 79 82 130 189 5 23 252 57 27 224 4 62 165 241 4 61 36 196 179 61 156 140 70 60 168 137 94 61 128 110 65 61 92 118 33 62 14 165 129 60 110 169 148 61 98 139 89 61 234 44 132 189 3 0 0 189 135 87 170 59 95 246 137 188 190 70 236 189 253 68 145 189 115 91 145 189 174 225 203 188 44 13 63 60 80 34 201 188 186 39 187 60 48 84 231 58 117 122 44 61 251 188 171 188 80 4 234 187 250 74 215 61 244 85 116 189 33 116 89 189 74 80 12 61 1 18 198 61 80 92 88 189 88 189 187 189 58 24 139 189 181 71 202 61 199 203 137 189 104 242 40 61 39 86 31 61 36 166 8 62 171 82 167 189 66 27 157 61 164 235 130 189 223 216 118 59 170 39 124 59 60 205 196 189 253 174 228 60 197 174 178 187 241 36 218 60 242 111 218 189"
2223

2324
strArray := strings.Split(featureStr, " ")
25+
2426
featureBytes := make([]byte, len(strArray))
2527
for i, c := range(strArray) {
2628
val, _ := strconv.ParseInt(c, 10, 32)
@@ -32,5 +34,11 @@ func TestExtract(t *testing.T) {
3234
if ! bytes.Equal(featureBytes, results) {
3335
t.Errorf("extract features failed: feature length = %d, result length = %d", len(results), len(results) )
3436
}
37+
type Result struct {
38+
id int
39+
feature []byte
40+
}
41+
result := Result{id:220, feature: featureBytes}
42+
log.Println(result)
3543
}
3644

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/acteq/patrol-workqueue
33
go 1.14
44

55
require (
6+
github.com/lib/pq v1.8.0
67
github.com/prep/beanstalk v1.4.0
78
gocv.io/x/gocv v0.24.0
89
)

Diff for: go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
1111
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1212
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1313
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
14+
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365 h1:ECW73yc9MY7935nNYXUkK7Dz17YuSUI9yqRqYS8aBww=
15+
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
16+
github.com/lib/pq v1.8.0 h1:9xohqzkUwzR4Ga4ivdTcawVS89YSDVxXMa3xJX3cGzg=
17+
github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
1418
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1519
github.com/prep/beanstalk v1.4.0 h1:6a3i9mIYapisVTgE3m/n1CqXHo7pek3Dq7iqT1n1tHo=
1620
github.com/prep/beanstalk v1.4.0/go.mod h1:p9/gS0vcLhuhzkQV23rLaLmw10NvHJBZD9BcbHtabG8=
1721
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1822
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
23+
github.com/takuoki/gocase v1.0.0 h1:gPwLJTWVm2T1kUiCsKirg/faaIUGVTI0FA3SYr75a44=
24+
github.com/takuoki/gocase v1.0.0/go.mod h1:QgOKJrbuJoDrtoKswBX1/Dw8mJrkOV9tbQZJaxaJ6zc=
1925
go.opencensus.io v0.22.3 h1:8sGtKOrtQqkN1bp2AtX+misvLIlOmsEsNd+9NIcPEm8=
2026
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
2127
gocv.io/x/gocv v0.24.0 h1:xtm5AnFNUtFvSmU+R/CgX7FguL7EDGEubhDdviX2rPY=

Diff for: main.go

+85-13
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,30 @@ import (
88
"encoding/json"
99
"bytes"
1010
"io"
11+
"time"
12+
"os"
13+
"strings"
1114
)
1215

1316

1417
func main() {
15-
handle := initEngine()
16-
if handle == nil {
17-
log.Fatal("initEngine failed")
18+
pool := NewEnginePool(10)
19+
20+
env := getEnvironment()
21+
host := env["beanstalkd_host"]
22+
if host == "" {
23+
host = "localhost"
24+
}
25+
port := env["beanstalkd_port"]
26+
if port == "" {
27+
port = "11300"
28+
}
29+
tuneName := env["beanstalkd_tube"]
30+
if tuneName == "" {
31+
tuneName = "face-recognize"
1832
}
1933

20-
21-
consumer, err := beanstalk.NewConsumer([]string{"localhost:11300"}, []string{"face-recognize"}, beanstalk.Config{
34+
consumer, err := beanstalk.NewConsumer([]string{host + ":" + port}, []string{tuneName}, beanstalk.Config{
2235
// Multiply the list of URIs to create a larger pool of connections.
2336
Multiply: 3,
2437
// NumGoroutines is the number of goroutines that the Receive method will
@@ -28,18 +41,32 @@ func main() {
2841
if err != nil {
2942
// handle error
3043
log.Fatal(err)
31-
return
3244
}
3345

46+
producer, err := beanstalk.NewProducer([]string{host + ":" + port}, beanstalk.Config{
47+
// Multiply the list of URIs to create a larger pool of connections.
48+
Multiply: 3,
49+
})
50+
if err != nil {
51+
log.Fatal(err)
52+
}
53+
defer producer.Stop()
54+
params := beanstalk.PutParams{Priority: 1024, Delay: 0, TTR: 30 * time.Second}
55+
3456
type Message struct {
35-
Filepath string
57+
id int
58+
filepath string
59+
}
60+
type Result struct {
61+
id int
62+
feature []byte
3663
}
3764
ctx, cancel := context.WithCancel(context.Background())
3865
defer cancel()
66+
3967
for {
4068
consumer.Receive(ctx, func(ctx context.Context, job *beanstalk.Job) {
4169
// handle job
42-
fmt.Println("job", job.ID)
4370
dec := json.NewDecoder(bytes.NewReader(job.Body))
4471
var m Message
4572
for {
@@ -49,16 +76,61 @@ func main() {
4976
log.Println(err)
5077
break
5178
}
52-
fmt.Printf("%s\n", m.Filepath)
53-
featureBytes := extract(m.Filepath, handle)
54-
}
79+
log.Println(job.ID, m.filepath)
80+
handle := pool.Get()
81+
featureBytes := extract(m.filepath, handle)
82+
//convert
83+
result := Result{id: m.id, feature: featureBytes}
5584

85+
resultStr, err := json.Marshal(result)
86+
id, err := producer.Put(ctx, tuneName, resultStr, params)
87+
log.Println("send result back", id)
88+
if err != nil {
89+
log.Println(err)
90+
}
91+
pool.Put(handle)
92+
}
93+
5694
if err := job.Delete(ctx); err != nil {
5795
log.Println(err)
58-
// log.Fatal(err)
5996
}
60-
})
97+
})
98+
//runtime.Gosched()
6199
}
62100
}
63101

102+
func Dec2Hex(n int64) string {
103+
if n < 0 {
104+
log.Println("Decimal to hexadecimal error: the argument must be greater than zero.")
105+
return ""
106+
}
107+
if n == 0 {
108+
return "00"
109+
}
110+
hex := map[int64]int64{10: 65, 11: 66, 12: 67, 13: 68, 14: 69, 15: 70}
111+
s := ""
112+
for q := n; q > 0; q = q / 16 {
113+
m := q % 16
114+
if m > 9 && m < 16 {
115+
m = hex[m]
116+
s = fmt.Sprintf("%v%v", string(m), s)
117+
continue
118+
}
119+
s = fmt.Sprintf("%v%v", m, s)
120+
}
121+
return s
122+
}
64123

124+
func getEnvironment() map[string]string{
125+
var env = map[string]string{}
126+
for _, line:= range os.Environ() {
127+
pair := strings.Split(line, "=")
128+
if len(pair) > 1 {
129+
key := strings.Trim(pair[0], " ")
130+
if(len(key) > 0) {
131+
env[key] = strings.Trim(pair[1], " ")
132+
}
133+
}
134+
}
135+
return env
136+
}

0 commit comments

Comments
 (0)