From 729822830b60a34997d4ab6f9ad401dce454de50 Mon Sep 17 00:00:00 2001 From: Nick White Date: Thu, 24 Jan 2019 18:33:24 +0000 Subject: Allow the specs for buckets to be set using a json file in an argument --- bucket-lines/bucket-lines.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'bucket-lines') diff --git a/bucket-lines/bucket-lines.go b/bucket-lines/bucket-lines.go index 186f568..e4e5a4d 100644 --- a/bucket-lines/bucket-lines.go +++ b/bucket-lines/bucket-lines.go @@ -1,8 +1,10 @@ package main import ( + "encoding/json" "flag" "fmt" + "io/ioutil" "log" "os" "path/filepath" @@ -13,8 +15,6 @@ import ( ) func main() { - // TODO: Allow bucket specs to be determined by a json file passed - // as an argument. b := parse.BucketSpecs{ // minimum confidence, name { 0, "bad" }, @@ -23,7 +23,7 @@ func main() { } flag.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage: bucket-lines [-d dir] [hocr1] [prob1] [hocr2] [...]\n") + fmt.Fprintf(os.Stderr, "Usage: bucket-lines [-d dir] [-s specs.json] [hocr1] [prob1] [hocr2] [...]\n") fmt.Fprintf(os.Stderr, "Copies image-text line pairs into different directories according\n") fmt.Fprintf(os.Stderr, "to the average character probability for the line.\n\n") fmt.Fprintf(os.Stderr, "Both .hocr and .prob files can be processed.\n\n") @@ -33,14 +33,28 @@ func main() { fmt.Fprintf(os.Stderr, "The .prob and .hocr files are assumed to be in the same directory\n") fmt.Fprintf(os.Stderr, "as the line's image and text files.\n\n") flag.PrintDefaults() + fmt.Fprintf(os.Stderr, "\nAn example specs.json file would be the following:\n") + fmt.Fprintf(os.Stderr, "[{\"min\": 0, \"name\": \"terrible\"}, {\"min\": 0.80, \"name\": \"ok\"}, {\"min\": 0.98, \"name\": \"great\"}]\n") } dir := flag.String("d", "buckets", "Directory to store the buckets") + specs := flag.String("s", "", "JSON file describing specs to bucket into") flag.Parse() if flag.NArg() < 1 { flag.Usage() os.Exit(1) } + if *specs != "" { + js, err := ioutil.ReadFile(*specs) + if err != nil { + log.Fatal(err) + } + err = json.Unmarshal(js, &b) + if err != nil { + log.Fatal(err) + } + } + var err error lines := make(parse.LineDetails, 0) -- cgit v1.2.1-24-ge1ad