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
|
package main
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")
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")
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
lines := make(parse.LineDetails, 0)
for _, f := range flag.Args() {
file, err := os.Open(f)
if err != nil {
log.Fatal(err)
}
defer file.Close()
reader := bufio.NewReader(file)
newlines, err := prob.GetLineDetails(f, reader)
if err != nil {
log.Fatal(err)
}
for _, l := range newlines {
lines = append(lines, l)
}
// explicitly close the file, so we can be sure we won't run out of
// handles before defer runs
file.Close()
}
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)
}
}
total := worstnum + mediumnum + bestnum
if total == 0 {
log.Fatal("No lines copied")
}
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)
}
|