-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlabels.go
46 lines (35 loc) · 887 Bytes
/
labels.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
package rknnlite
import (
"bufio"
"fmt"
"os"
"strings"
)
// LoadLabels reads the labels used to train the Model from the given text file.
// It should contain one label per line.
func LoadLabels(file string) ([]string, error) {
// open the file
f, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("error opening file: %w", err)
}
defer f.Close()
// create a scanner to read the file.
scanner := bufio.NewScanner(f)
var labels []string
// read and trim each line
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// handle special keyword to convert to " " this is needed for
// PPOCR key list
if line == "__space__" {
line = " "
}
labels = append(labels, line)
}
// check for errors during scanning
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading file: %w", err)
}
return labels, nil
}