From 367e95adee6a7efbbe85468ac180f06402873a2f Mon Sep 17 00:00:00 2001 From: Nick White Date: Wed, 23 Jan 2019 23:17:13 +0000 Subject: Create general BucketUp function, use it for line-conf-buckets --- line-conf-buckets/line-conf-buckets.go | 97 ++++------------------------------ parse/bucket.go | 82 ++++++++++++++++++++++++++++ parse/prob/prob.go | 2 +- 3 files changed, 92 insertions(+), 89 deletions(-) create mode 100644 parse/bucket.go diff --git a/line-conf-buckets/line-conf-buckets.go b/line-conf-buckets/line-conf-buckets.go index 1c33ba4..65c85b8 100644 --- a/line-conf-buckets/line-conf-buckets.go +++ b/line-conf-buckets/line-conf-buckets.go @@ -4,62 +4,14 @@ import ( "bufio" "flag" "fmt" - "io" "log" "os" - "path/filepath" "sort" - "strconv" - "strings" "git.rescribe.xyz/testingtools/parse" "git.rescribe.xyz/testingtools/parse/prob" ) -// TODO: this is just a placeholder, do this more sensibly, as -tess does (hint: full txt should already be in the LineDetail) -func copyline(filebase string, dirname string, basename string, avgconf string, outdir string, todir string, l parse.LineDetail) (err error) { - outname := filepath.Join(outdir, todir, filepath.Base(dirname) + "_" + basename + "_" + avgconf) - //log.Fatalf("I'd use '%s' as outname, and '%s' as filebase\n", outname, filebase) - - for _, extn := range []string{".txt"} { - infile, err := os.Open(filebase + extn) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to open %s\n", filebase + extn) - return err - } - defer infile.Close() - - err = os.MkdirAll(filepath.Join(outdir, todir), 0700) - if err != nil { - return err - } - - outfile, err := os.Create(outname + extn) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to create %s\n", outname + extn) - return err - } - defer outfile.Close() - - _, err = io.Copy(outfile, infile) - if err != nil { - return err - } - } - - f, err := os.Create(outname + ".bin.png") - if err != nil { - return err - } - defer f.Close() - err = l.Img.CopyLineTo(f) - if err != nil { - return err - } - - return err -} - func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: line-conf-buckets prob1 [prob2] [...]\n") @@ -99,47 +51,16 @@ func main() { sort.Sort(lines) - worstnum := 0 - mediumnum := 0 - bestnum := 0 - - outdir := "buckets" // TODO: set this from cmdline - todir := "" - - for _, l := range lines { - switch { - case l.Avgconf < 0.95: - todir = "bad" - worstnum++ - case l.Avgconf < 0.98: - todir = "95to98" - mediumnum++ - default: - todir = "98plus" - bestnum++ - } - - avgstr := strconv.FormatFloat(l.Avgconf, 'G', -1, 64) - if len(avgstr) > 2 { - avgstr = avgstr[2:] - } - filebase := strings.Replace(l.Name, ".prob", "", 1) - basename := filepath.Base(filebase) - err := copyline(filebase, l.OcrName, basename, avgstr, outdir, todir, l) - if err != nil { - log.Fatal(err) - } + //var b parse.BucketSpecs + b := parse.BucketSpecs{ + { 0, "bad" }, + { 0.95, "95to98" }, + { 0.98, "98plus" }, } - total := worstnum + mediumnum + bestnum - - if total == 0 { - log.Fatal("No lines copied") + // TODO: set bucket dirname from cmdline + err := parse.BucketUp(lines, b, "newbuckets") + if err != nil { + log.Fatal(err) } - - fmt.Printf("Copied lines to %s\n", outdir) - fmt.Printf("---------------------------------\n") - fmt.Printf("Lines 98%%+ quality: %d%%\n", 100 * bestnum / total) - fmt.Printf("Lines 95-98%% quality: %d%%\n", 100 * mediumnum / total) - fmt.Printf("Lines <95%% quality: %d%%\n", 100 * worstnum / total) } diff --git a/parse/bucket.go b/parse/bucket.go new file mode 100644 index 0000000..3a4cd6a --- /dev/null +++ b/parse/bucket.go @@ -0,0 +1,82 @@ +package parse + +import ( + "io" + "path/filepath" + "os" + "sort" + "strconv" +) + +type BucketSpec struct { + Min float64 + Name string +} +type BucketSpecs []BucketSpec +func (b BucketSpecs) Len() int { return len(b) } +func (b BucketSpecs) Swap(i, j int) { b[i], b[j] = b[j], b[i] } +func (b BucketSpecs) Less(i, j int) bool { return b[i].Min < b[j].Min } + +func bucketLine(l LineDetail, buckets BucketSpecs, dirname string) error { + todir := "" + for _, b := range buckets { + if l.Avgconf >= b.Min { + todir = b.Name + } + } + + if todir == "" { + return nil + } + + avgstr := strconv.FormatFloat(l.Avgconf, 'G', -1, 64) + if len(avgstr) > 2 { + avgstr = avgstr[2:] + } + + base := filepath.Join(dirname, todir, filepath.Base(l.OcrName) + "_" + l.Name + "_" + avgstr) + + err := os.MkdirAll(filepath.Join(dirname, todir), 0700) + if err != nil { + return err + } + + f, err := os.Create(base + ".png") + if err != nil { + return err + } + defer f.Close() + + err = l.Img.CopyLineTo(f) + if err != nil { + return err + } + + f, err = os.Create(base + ".txt") + if err != nil { + return err + } + defer f.Close() + + _, err = io.WriteString(f, l.Text) + if err != nil { + return err + } + + return nil +} + +// Copies line images and text into directories based on their +// confidence, as defined by the buckets struct +func BucketUp(lines LineDetails, buckets BucketSpecs, dirname string) error { + sort.Sort(buckets) + // TODO: record and print out summary of % in each bucket category (see how tools did it) + for _, l := range lines { + err := bucketLine(l, buckets, dirname) + if err != nil { + return err + } + } + + return nil +} diff --git a/parse/prob/prob.go b/parse/prob/prob.go index 5a84567..6781a1a 100644 --- a/parse/prob/prob.go +++ b/parse/prob/prob.go @@ -57,7 +57,7 @@ func GetLineDetails(name string, r *bufio.Reader) (parse.LineDetails, error) { return lines, err } - line.Name = name + line.Name = filepath.Base(filebase) line.Avgconf = avg line.Text = string(txt) line.OcrName = filepath.Dir(filebase) -- cgit v1.2.1-24-ge1ad