-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (78 loc) · 3.03 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
package main
import (
"context"
"io/ioutil"
"log"
"path/filepath"
scryfall "github.com/BlueMonday/go-scryfall"
flag "github.com/ogier/pflag"
"github.com/otiai10/gosseract"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"gocv.io/x/gocv"
"gocv.io/x/gocv/contrib"
)
type configuration struct {
InferenceGraphPath string
ImageInputPath string
SetSymbolDirectory string
SetSymbolBackgroundPath string
GetNewSetSymbols bool
}
func main() {
log.Println(`Starting mox...
Tesseract is version`, gosseract.Version(), `(this should be 4.x).
TensorFlow is version`, tf.Version(), ` (this should be 1.x).
GoCV is version`, gocv.Version(), ` (who knows with this one).`)
var config configuration
flag.StringVarP(&config.InferenceGraphPath, "inference-graph", "g", "./inference_graph/", "Path to the directory containing the inference graph.")
flag.StringVarP(&config.ImageInputPath, "image-input", "i", "./image.jpg", "Path to an image to infer partial card information from.")
flag.StringVarP(&config.SetSymbolDirectory, "setsymbol-dir", "s", "./set_symbols/", "A directory containing set symbols as PNGs.")
flag.StringVarP(&config.SetSymbolBackgroundPath, "set-symbol-background", "b", "./set_symbols/background.jpg",
"Path to the background to composite card images over when finding set symbols. THE BACKGROUND MUST BE LARGER THAN IMAGES LIKE THIS: https://img.scryfall.com/cards/png/en/ddt/60.png")
flag.BoolVar(&config.GetNewSetSymbols, "get-new-set-symbols", false, "If set to true, mox will get new set symbols from the internet and load them.")
flag.Parse()
// Construct the graph
graph, err := newDetectionGraph(filepath.Join(config.InferenceGraphPath, "frozen_inference_graph.pb"))
if err != nil {
log.Panic(err)
}
// Create a session to run inference with
session, err := tf.NewSession(graph, nil)
if err != nil {
log.Panic(err)
}
defer session.Close()
// Set up Scryfall
context := context.Background()
scryfallClient, err := scryfall.NewClient()
if err != nil {
log.Panic(err)
}
// Generate/get an array of set symbols
setSymbols, err := setupSetSymbols(context, scryfallClient, config.SetSymbolDirectory, config.SetSymbolBackgroundPath, session, graph, config.GetNewSetSymbols)
if err != nil {
log.Panic(err)
}
// Create a Tesseract client to OCR the text
tessClient := gosseract.NewClient()
defer tessClient.Close()
tessClient.SetLanguage("eng") // Set the language to English (you may need to install this language for Tesseract)
tessClient.SetPageSegMode(gosseract.PSM_AUTO) // Set the word segmentation mode
// Setup a SURF context
surf := contrib.NewSURF()
defer surf.Close()
imageBytes, err := ioutil.ReadFile(config.ImageInputPath)
if err != nil {
log.Panic(err)
}
pcd, err := inferPartialCardData(imageBytes, session, graph, tessClient)
if err != nil {
log.Panic(err)
}
log.Println(pcd.String())
card, err := pcd.findClosestCard(context, scryfallClient, surf, setSymbols, config.SetSymbolDirectory)
if err != nil {
log.Panic(err)
}
log.Println(card)
}