fix: parse charset as JSON array instead of line-by-line
This commit is contained in:
+13
-14
@@ -1,9 +1,9 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"math"
|
||||
@@ -254,26 +254,25 @@ func ctcDecode(output []float32, charset []string) string {
|
||||
|
||||
func (h *Handler) loadCharset() ([]string, error) {
|
||||
charsetPath := filepath.Join(h.modelPath, "CharSets.txt")
|
||||
file, err := os.Open(charsetPath)
|
||||
|
||||
// 读取整个文件
|
||||
data, err := os.ReadFile(charsetPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
charset := make([]string, 0, 6000)
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line != "" {
|
||||
charset = append(charset, line)
|
||||
}
|
||||
// 解析 JSON 数组格式
|
||||
var charset []string
|
||||
if err := json.Unmarshal(data, &charset); err != nil {
|
||||
return nil, fmt.Errorf("解析字符集失败: %v", err)
|
||||
}
|
||||
|
||||
result := make([]string, len(charset)+1)
|
||||
result[0] = ""
|
||||
copy(result[1:], charset)
|
||||
// 确保第一个元素是空字符串(CTC blank)
|
||||
if len(charset) == 0 || charset[0] != "" {
|
||||
charset = append([]string{""}, charset...)
|
||||
}
|
||||
|
||||
return result, scanner.Err()
|
||||
return charset, nil
|
||||
}
|
||||
|
||||
// ===================== Math 数学计算 =====================
|
||||
|
||||
Reference in New Issue
Block a user