-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhocr.go
150 lines (131 loc) · 2.9 KB
/
hocr.go
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
146
147
148
149
150
package gocropy
import (
"bytes"
"encoding/xml"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"os"
"path"
"regexp"
// "strconv"
)
type HOCRTitle struct {
Title string `xml:"title,attr"`
}
type HOCRClass struct {
Class string `xml:"class,attr"`
}
type HOCRSpan struct {
HOCRClass
HOCRTitle
Data string `xml:",chardata"`
Token []HOCRSpan `xml:"span"`
}
type HOCRDiv struct {
HOCRClass
HOCRTitle
Spans []HOCRSpan `xml:"span"`
}
type HOCRBody struct {
Div HOCRDiv `xml:"div"`
}
type HOCRMeta struct {
HttpEquiv string `xml:"http-equiv,attr,omitempty"`
Name string `xml:"name,attr,omitempty"`
Content string `xml:"content,attr"`
}
type HOCRHead struct {
Title string `xml:"title"`
Metas []HOCRMeta `xml:"meta"`
}
type HOCR struct {
XMLName xml.Name
Head HOCRHead `xml:"head"`
Body HOCRBody `xml:"body"`
File string `xml:"-"`
}
func (hocr *HOCR) ReadImageFileBbox() (*Bbox, error) {
basedir, _ := path.Split(hocr.File)
file := hocr.Body.Div.GetFile()
in, err := os.Open(path.Join(basedir, file))
if err != nil {
return nil, err
}
defer in.Close()
img, _, err := image.DecodeConfig(in)
if err != nil {
return nil, err
}
if img.ColorModel == nil {
return nil, fmt.Errorf("Could not read image (missing decoder?)")
}
bbox := Bbox{0, 0, img.Width, img.Height}
hocr.Body.Div.SetBbox(bbox)
return &bbox, nil
}
var fileRegex = regexp.MustCompile("file\\s+(.*)")
func (title HOCRTitle) GetFile() string {
m := fileRegex.FindStringSubmatch(title.Title)
if m != nil {
return m[1]
} else {
return ""
}
}
func (title *HOCRTitle) SetFile(file string) {
if len(title.Title) > 0 {
title.Title = fmt.Sprintf("%s; file %s", title.Title, file)
} else {
title.Title = fmt.Sprintf("file %s", file)
}
}
func (title *HOCRTitle) SetCuts(chars []Char) {
var buffer bytes.Buffer
for i := 1; i < len(chars); i++ {
delta := chars[i].Box.Left - chars[i-1].Box.Left
buffer.WriteString(fmt.Sprintf(" %d", delta))
}
if len(title.Title) > 0 {
title.Title = fmt.Sprintf("%s; cuts%s", title.Title, buffer.String())
} else {
title.Title = fmt.Sprintf("cuts%s", buffer.String())
}
}
var bboxRegex = regexp.MustCompile("bbox\\s+(\\d+\\s+\\d+\\s+\\d+\\s+\\d+)")
func (title HOCRTitle) GetBbox() Bbox {
var bbox Bbox
m := bboxRegex.FindStringSubmatch(title.Title)
if m != nil {
fmt.Sscanf(m[1], "%v", &bbox)
}
return bbox
}
func (title *HOCRTitle) SetBbox(bbox Bbox) {
if len(title.Title) > 0 {
title.Title = fmt.Sprintf("%s; bbox %v", title.Title, bbox)
} else {
title.Title = fmt.Sprintf("bbox %v", bbox)
}
}
func ReadHOCR(file string) (*HOCR, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
hocr := HOCR{File: file}
err = xml.Unmarshal(data, &hocr)
if err != nil {
return nil, err
}
return &hocr, nil
}
func MustReadHOCR(file string) *HOCR {
hocr, err := ReadHOCR(file)
if err != nil {
panic(err)
}
return hocr
}