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
|
package main
// TODO: see TODO in hocr package
//
// TODO: Simplify things into functions more; this works well, but is a bit of a rush job
import (
"flag"
"fmt"
"image/png"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"git.rescribe.xyz/testingtools/hocr"
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: line-conf-buckets hocr1 [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")
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
lines := make(hocr.LineDetails, 0)
for _, f := range flag.Args() {
file, err := ioutil.ReadFile(f)
if err != nil {
log.Fatal(err)
}
h, err := hocr.Parse(file)
if err != nil {
log.Fatal(err)
}
pngfn := strings.Replace(f, ".hocr", ".png", 1)
pngf, err := os.Open(pngfn)
if err != nil {
log.Fatal(err)
}
defer pngf.Close()
img, err := png.Decode(pngf)
if err != nil {
log.Fatal(err)
}
n := strings.Replace(filepath.Base(f), ".hocr", "", 1)
newlines, err := hocr.GetLineDetails(h, img, n)
if err != nil {
log.Fatal(err)
}
for _, l := range newlines {
lines = append(lines, l)
}
}
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, 'f', 5, 64)
avgstr = avgstr[2:]
outname := filepath.Join(outdir, todir, l.Hocrname + "_" + l.Name + "_" + avgstr + ".png")
err := os.MkdirAll(filepath.Join(outdir, todir), 0700)
if err != nil {
log.Fatal(err)
}
outfile, err := os.Create(outname)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create %s\n", outname)
log.Fatal(err)
}
defer outfile.Close()
err = png.Encode(outfile, l.Img)
if err != nil {
log.Fatal(err)
}
outname = filepath.Join(outdir, todir, l.Hocrname + "_" + l.Name + "_" + avgstr + ".txt")
outfile, err = os.Create(outname)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create %s\n", outname)
log.Fatal(err)
}
defer outfile.Close()
_, err = io.WriteString(outfile, l.Text)
if err != nil {
log.Fatal(err)
}
// TODO: test whether the line.img works properly with multiple hocrs, as it could be that as it's a pointer, it always points to the latest image (don't think so, but not sure)
}
total := worstnum + mediumnum + bestnum
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)
}
|