summaryrefslogtreecommitdiff
path: root/cmd/pagegraph/main.go
blob: 57508922b0ff742a12618761456e9174e0599ed3 (plain)
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
// Copyright 2019 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"rescribe.xyz/bookpipeline"
	"rescribe.xyz/utils/pkg/hocr"
)

const usage = `Usage: pagegraph [-l] file.hocr graph.png

pagegraph creates a graph showing average confidence of each
word in a page of hOCR.
`

func main() {
	lines := flag.Bool("l", false, "use line confidence instead of word confidence")
	flag.Usage = func() {
		fmt.Fprintln(flag.CommandLine.Output(), usage)
		flag.PrintDefaults()
	}
	flag.Parse()

	if flag.NArg() != 2 {
		flag.Usage()
		return
	}

	var confs []*bookpipeline.Conf
	var xlabel string
	if *lines {
		linedetails, err := hocr.GetLineDetails(flag.Arg(0))
		if err != nil {
			log.Fatal(err)
		}
		for n, l := range linedetails {
			c := bookpipeline.Conf{
				Conf: l.Avgconf * 100,
				Path: fmt.Sprintf("%d_line", n),
			}
			confs = append(confs, &c)
		}
		xlabel = "Line number"
	} else {
		wordconfs, err := hocr.GetWordConfs(flag.Arg(0))
		if err != nil {
			log.Fatal(err)
		}
		for n, wc := range wordconfs {
			c := bookpipeline.Conf{
				Conf: wc,
				Path: fmt.Sprintf("%d_word", n),
			}
			confs = append(confs, &c)
		}
		xlabel = "Word number"
	}

	// Structure to fit what bookpipeline.Graph needs
	// TODO: probably reorganise bookpipeline to just need []*Conf
	cconfs := make(map[string]*bookpipeline.Conf)
	for _, c := range confs {
		cconfs[c.Path] = c
	}

	fn := flag.Arg(1)
	f, err := os.Create(fn)
	if err != nil {
		log.Fatalln("Error creating file", fn, err)
	}
	defer f.Close()
	err = bookpipeline.GraphOpts(cconfs, filepath.Base(flag.Arg(0)), xlabel, false, f)
	if err != nil {
		log.Fatalln("Error creating graph", err)
	}
}