/
main.go
99 lines (86 loc) · 2.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"time"
"github.com/baetyl/baetyl-go/v2/context"
"github.com/baetyl/baetyl-go/v2/errors"
"github.com/baetyl/baetyl-go/v2/http"
"github.com/baetyl/baetyl-go/v2/log"
"gocv.io/x/gocv"
)
func main() {
context.Run(func(ctx context.Context) error {
var cfg Config
err := ctx.LoadCustomConfig(&cfg)
if err != nil {
return errors.Trace(err)
}
bcfg, err := ctx.NewSystemBrokerClientConfig()
if err != nil {
return errors.Trace(err)
}
bcli, err := ctx.NewBrokerClient(bcfg)
if err != nil {
return errors.Trace(err)
}
bcli.Start(nil)
defer bcli.Close()
infer, err := NewInfer(cfg.Process.Infer)
if err != nil {
return errors.Trace(err)
}
defer infer.Close()
video, err := NewVideo(cfg.Video)
if err != nil {
return errors.Trace(err)
}
defer video.Close()
fcli, err := ctx.NewFunctionHttpClient()
if err != nil {
return errors.Trace(err)
}
var s time.Time
img := gocv.NewMat()
defer img.Close()
proc := NewProcess(cfg.Process, bcli, fcli)
for {
select {
case <-ctx.WaitChan():
return nil
default:
}
s = time.Now()
err := video.Read(&img)
if err != nil {
ctx.Log().Error("failed to read video", log.Error(err))
time.Sleep(time.Second) // TODO: configured
continue
}
if img.Empty() {
continue
}
ctx.Log().Sugar().Debugf("[Read ] elapsed time: %v", time.Since(s))
t := time.Now()
blob := proc.Before(img)
ctx.Log().Sugar().Debugf("[Before] elapsed time: %v", time.Since(t))
s = time.Now()
prob := infer.Run(blob)
ctx.Log().Sugar().Debugf("[Infer ] elapsed time: %v", time.Since(s))
s = time.Now()
err = proc.After(img, prob, infer.GetElapsedTime(), t)
if err != nil {
ctx.Log().Error("failed to process infer result", log.Error(err))
}
ctx.Log().Sugar().Debugf("[After ] elapsed time: %v", time.Since(s))
prob.Close()
blob.Close()
}
})
}
// TODO: remove
func newFunctionClient(cfg http.ClientConfig) (*http.Client, error) {
ops, err := cfg.ToClientOptions()
if err != nil {
return nil, err
}
return http.NewClient(ops), nil
}