-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
289 lines (243 loc) · 6.88 KB
/
app.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"context"
"encoding/base64"
"fmt"
"os"
"sort"
"strings"
"github.com/otiai10/gosseract/v2"
)
// App struct
type App struct {
ctx context.Context
InitialImage string
}
type OCRResult struct {
Text string `json:"text"`
Boxes []Box `json:"boxes"`
}
type Box struct {
Text string `json:"text"`
X int `json:"x"`
Y int `json:"y"`
Width int `json:"width"`
Height int `json:"height"`
}
// NewApp creates a new App application
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (a *App) ProcessImage(input string) (OCRResult, error) {
// If input starts with "data:", it's a base64 image
if strings.HasPrefix(input, "data:") {
// Extract the base64 data after the comma
base64Data := strings.Split(input, ",")[1]
imageBytes, err := base64.StdEncoding.DecodeString(base64Data)
if err != nil {
return OCRResult{}, fmt.Errorf("error decoding base64 image: %v", err)
}
// Initialize Tesseract client
client := gosseract.NewClient()
defer client.Close()
// Set OCR settings (same as ProcessImageFile)
client.SetVariable("tessedit_pageseg_mode", "1")
client.SetVariable("tessedit_ocr_engine_mode", "2")
client.SetVariable("preserve_interword_spaces", "1")
client.SetVariable("textord_heavy_nr", "1")
client.SetVariable("textord_min_linesize", "2.5")
client.SetVariable("tessedit_char_blacklist", "§¶©®™")
if err := client.SetImageFromBytes(imageBytes); err != nil {
return OCRResult{}, err
}
// Get bounding boxes and process OCR
boxes, err := client.GetBoundingBoxes(gosseract.RIL_WORD)
if err != nil {
return OCRResult{}, err
}
var result OCRResult
for _, box := range boxes {
result.Boxes = append(result.Boxes, Box{
Text: box.Word,
X: box.Box.Min.X,
Y: box.Box.Min.Y,
Width: box.Box.Max.X - box.Box.Min.X,
Height: box.Box.Max.Y - box.Box.Min.Y,
})
}
// Merge nearby boxes
result.Boxes = mergeBoxes(result.Boxes)
// Get full text
text, err := client.Text()
if err != nil {
return OCRResult{}, err
}
result.Text = text
return result, nil
}
return OCRResult{}, fmt.Errorf("invalid input format")
}
func (a *App) ProcessImageFile(filepath string) (OCRResult, error) {
// Read the image file
imageBytes, err := os.ReadFile(filepath)
if err != nil {
return OCRResult{}, fmt.Errorf("error reading image file: %v", err)
}
// Initialize Tesseract client
client := gosseract.NewClient()
defer client.Close()
// Basic configuration
if err := client.SetLanguage("eng"); err != nil {
return OCRResult{}, err
}
// Optimize OCR settings
client.SetVariable("tessedit_pageseg_mode", "1") // Automatic page segmentation with OSD
client.SetVariable("tessedit_ocr_engine_mode", "2") // Legacy + LSTM mode
client.SetVariable("preserve_interword_spaces", "1") // Preserve spacing between words
client.SetVariable("textord_heavy_nr", "1") // Heavy noise removal
client.SetVariable("textord_min_linesize", "2.5") // Minimum text size to detect
client.SetVariable("tessedit_char_blacklist", "§¶©®™") // Exclude problematic characters
// Set image from bytes
if err := client.SetImageFromBytes(imageBytes); err != nil {
return OCRResult{}, err
}
// Get bounding boxes
boxes, err := client.GetBoundingBoxes(gosseract.RIL_WORD)
if err != nil {
return OCRResult{}, err
}
var result OCRResult
for _, box := range boxes {
result.Boxes = append(result.Boxes, Box{
Text: box.Word,
X: box.Box.Min.X,
Y: box.Box.Min.Y,
Width: box.Box.Max.X - box.Box.Min.X,
Height: box.Box.Max.Y - box.Box.Min.Y,
})
}
// Merge nearby boxes
result.Boxes = mergeBoxes(result.Boxes)
// Get full text
text, err := client.Text()
if err != nil {
return OCRResult{}, err
}
result.Text = text
return result, nil
}
func (b Box) isNearby(other Box) bool {
// Consider boxes nearby if they are within 20 pixels horizontally
// and 10 pixels vertically of each other
horizontalGap := 20
verticalGap := 10
// Check if boxes are horizontally nearby
horizontallyNear := (b.X <= other.X+other.Width+horizontalGap) &&
(other.X <= b.X+b.Width+horizontalGap)
// Check if boxes are vertically nearby
verticallyNear := (b.Y <= other.Y+other.Height+verticalGap) &&
(other.Y <= b.Y+b.Height+verticalGap)
return horizontallyNear && verticallyNear
}
func mergeBoxes(boxes []Box) []Box {
if len(boxes) == 0 {
return boxes
}
// Sort boxes by Y coordinate first, then X coordinate
sort.Slice(boxes, func(i, j int) bool {
if abs(boxes[i].Y-boxes[j].Y) < 10 { // If Y coordinates are similar, sort by X
return boxes[i].X < boxes[j].X
}
return boxes[i].Y < boxes[j].Y
})
var mergedBoxes []Box
currentGroup := []Box{boxes[0]}
for i := 1; i < len(boxes); i++ {
// Check if current box is nearby any box in the current group
isNearby := false
for _, groupBox := range currentGroup {
if groupBox.isNearby(boxes[i]) {
isNearby = true
break
}
}
if isNearby {
currentGroup = append(currentGroup, boxes[i])
} else {
// Merge current group and start a new one
mergedBox := mergeGroup(currentGroup)
mergedBoxes = append(mergedBoxes, mergedBox)
currentGroup = []Box{boxes[i]}
}
}
// Don't forget to merge the last group
if len(currentGroup) > 0 {
mergedBox := mergeGroup(currentGroup)
mergedBoxes = append(mergedBoxes, mergedBox)
}
return mergedBoxes
}
func mergeGroup(group []Box) Box {
if len(group) == 0 {
return Box{}
}
if len(group) == 1 {
return group[0]
}
// Find the bounding box that contains all boxes in the group
minX := group[0].X
minY := group[0].Y
maxX := group[0].X + group[0].Width
maxY := group[0].Y + group[0].Height
words := []string{group[0].Text}
for i := 1; i < len(group); i++ {
box := group[i]
words = append(words, box.Text)
if box.X < minX {
minX = box.X
}
if box.Y < minY {
minY = box.Y
}
if box.X+box.Width > maxX {
maxX = box.X + box.Width
}
if box.Y+box.Height > maxY {
maxY = box.Y + box.Height
}
}
return Box{
Text: strings.Join(words, " "),
X: minX,
Y: minY,
Width: maxX - minX,
Height: maxY - minY,
}
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
// Add a new method to get the initial image
func (a *App) GetInitialImage() string {
return a.InitialImage
}
func (a *App) GetImageData(input string) (string, error) {
// If input is a file path, read the file and convert to base64
imageBytes, err := os.ReadFile(input)
if err != nil {
return "", fmt.Errorf("error reading image file: %v", err)
}
return "data:image/png;base64," + base64.StdEncoding.EncodeToString(imageBytes), nil
}