-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
124 lines (101 loc) · 2.4 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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/dhogborg/gosser/internal/ssocr"
)
var DEBUG = false
const (
OutputModeString string = "string"
OutputModeNumber string = "number"
)
func main() {
app := cli.NewApp()
app.Name = "Gosser"
app.Usage = "Read seven segment display image, output result to sdtout"
app.Version = "0.0.2"
app.Author = "github.com/dhogborg"
app.Email = "d@hogborg.se"
app.Action = func(c *cli.Context) {
DEBUG := c.GlobalBool("debug")
ssocr.DEBUG = DEBUG
// use a manifest file for segment reading
var manifest []byte
if manifestfile := c.GlobalString("manifest"); manifestfile != "" {
if DEBUG {
log.WithFields(log.Fields{
"file": manifestfile,
}).Info("using manifest file")
}
buffer, err := ioutil.ReadFile(manifestfile)
if err != nil {
panic(err)
}
manifest = buffer
}
pos := c.GlobalInt("positions")
ssocr := ssocr.NewSSOCR(pos, manifest)
result := ssocr.Scan(c.GlobalString("input"))
// integer output forces pedantic mode
if c.GlobalBool("pedantic") || c.GlobalString("output") == OutputModeNumber {
if strings.Index(result, "-") > -1 {
log.WithFields(log.Fields{
"result": result,
}).Error("result is not well formed (pedantic mode)")
os.Exit(-1)
}
}
if c.GlobalString("output") == OutputModeNumber {
i, err := strconv.ParseFloat(result, 64)
if err != nil {
log.Panic(err)
}
if c.GlobalInt("div") > 1 {
i = i / float64(c.GlobalInt("div"))
}
result := fmt.Sprintf("%f", i)
println(result)
os.Exit(0)
}
// default printout
println(result)
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "input,i",
Usage: "input file",
},
cli.StringFlag{
Name: "manifest,m",
Usage: "Manifest file with coordinates for segments",
},
cli.IntFlag{
Name: "positions,p",
Usage: "Number of digits in the image",
},
cli.StringFlag{
Name: "output,o",
Value: OutputModeString,
Usage: "Output type, number or string",
},
cli.BoolFlag{
Name: "pedantic",
Usage: "Pedantic mode will output an error rather than let you see a invalid result",
},
cli.IntFlag{
Name: "div",
Value: 1,
Usage: "Divide the result by a factor (only int output)",
},
cli.BoolFlag{
Name: "debug",
Usage: "Enable debug output",
},
}
app.Run(os.Args)
}