/
main.go
216 lines (170 loc) · 5.54 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"context"
"fmt"
"io/fs"
stdlog "log"
"net/http"
"os"
"os/signal"
"path/filepath"
"sync"
"sync/atomic"
"syscall"
"time"
log "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/namsral/flag"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gocv.io/x/gocv"
"golang.org/x/sync/errgroup"
)
var (
version = "no version from LDFLAGS"
source = flag.String("source", "", "Source directory for the images")
dest = flag.String("dest", "", "Destination directory for the images")
width = flag.Int("width", 400, "Size of the target tiles x")
height = flag.Int("height", 400, "Size of the target tiles y")
resize = flag.Int("resize", 2, "Divide size tilling")
smallerTile = flag.Bool("smallerTile", false, "Allow tiling of remaining on the borders")
workerCount = flag.Int("workerCount", 8, "Parallel worker count")
validationTileCount = flag.Int("validationTileCount", 0, "Number of validation tiles")
validationOnly = flag.Bool("validationOnly", false, "Generate validation tiles only")
rejectBlurry = flag.Bool("rejectBlurry", false, "Reject blurry source image")
rejectBlurryThresold = flag.Float64("rejectBlurryThresold", 6_000, "Thresold before rejecting blurry images")
logLevel = flag.String("logLevel", "INFO", "DEBUG|INFO|WARN|ERROR")
httpMetricsPort = flag.Int("httpMetricsPort", 34130, "http port")
httpMetricsServer *http.Server
)
func main() {
flag.Parse()
logger := log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
logger = log.With(logger, "caller", log.Caller(5), "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "app", "ml-image-tile")
logger = NewLevelFilterFromString(logger, *logLevel)
stdlog.SetOutput(log.NewStdlibAdapter(logger))
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
// catch termination
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(interrupt)
g, ctx := errgroup.WithContext(ctx)
// web server metrics
g.Go(func() error {
httpMetricsServer = &http.Server{
Addr: fmt.Sprintf(":%d", *httpMetricsPort),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
level.Info(logger).Log("msg", fmt.Sprintf("HTTP Metrics server listening at :%d", *httpMetricsPort))
versionGauge.WithLabelValues(version).Add(1)
// Register Prometheus metrics handler.
http.Handle("/metrics", promhttp.Handler())
if err := httpMetricsServer.ListenAndServe(); err != http.ErrServerClosed {
return err
}
return nil
})
queue := make(chan string, *workerCount)
g.Go(func() error {
var wg sync.WaitGroup
worker := func(jobs <-chan string) {
laplacian := gocv.NewMat()
defer laplacian.Close()
result := gocv.NewMat()
defer result.Close()
mean := gocv.NewMat()
defer mean.Close()
for {
path, ok := <-jobs
if !ok {
break
}
level.Debug(logger).Log("msg", "processing", "path", path)
if *rejectBlurry {
img := gocv.IMRead(path, gocv.IMReadAnyDepth)
if img.Empty() {
level.Error(logger).Log("msg", "error reading image for blur", "path", path)
atomic.AddUint64(&errCounter, 1)
img.Close()
continue
}
gocv.Laplacian(img, &laplacian, img.Type(), 5, 1, 0, gocv.BorderDefault)
gocv.MeanStdDev(laplacian, &mean, &result)
deviation := result.Mean().Val1 * result.Mean().Val1
if deviation < *rejectBlurryThresold {
atomic.AddUint64(&rejectedBlurryCounter, 1)
level.Warn(logger).Log("msg", "rejected blurry image", "path", path, "deviation", deviation)
img.Close()
continue
}
img.Close()
}
atomic.AddUint64(&fileCounter, 1)
if !*validationOnly {
err := processImageBimg(logger, path, *source, *dest, *smallerTile, *resize, *width, *height)
if err != nil {
level.Error(logger).Log("msg", "error processing tile", "path", path, "err", err)
atomic.AddUint64(&errCounter, 1)
continue
}
}
if *validationTileCount > 0 {
err := randomTileImageBimg(logger, path, *source, *dest, *validationTileCount, *resize, *width, *height)
if err != nil {
level.Error(logger).Log("msg", "error processing random tile", "path", path, "err", err)
atomic.AddUint64(&errCounter, 1)
continue
}
}
}
level.Debug(logger).Log("msg", "stopping worker")
wg.Done()
}
for w := 0; w < *workerCount; w++ {
wg.Add(1)
go worker(queue)
}
wg.Wait()
return fmt.Errorf("finished work")
})
g.Go(func() error {
err := filepath.Walk(*source, func(path string, info fs.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
if !info.IsDir() {
level.Debug(logger).Log("msg", "queuing", "path", path)
queue <- path
}
return nil
})
close(queue)
return err
})
select {
case <-interrupt:
cancel()
break
case <-ctx.Done():
break
}
level.Warn(logger).Log("msg", "received shutdown signal")
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()
if httpMetricsServer != nil {
_ = httpMetricsServer.Shutdown(shutdownCtx)
}
err := g.Wait()
level.Info(logger).Log(
"fileCounter", atomic.LoadUint64(&fileCounter),
"tileCounter", atomic.LoadUint64(&tileCounter),
"rejectedBlurryCounter", atomic.LoadUint64(&rejectedBlurryCounter),
)
if err != nil {
level.Error(logger).Log("msg", "stopping", "error", err)
os.Exit(2)
}
}