diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec36fd1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.* +!.gitignore +go.sum +env.yml +node_modules +package.json +*.bak.* +*.png +*.jpg \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e78a8c7 --- /dev/null +++ b/go.mod @@ -0,0 +1,29 @@ +module textin + +go 1.23.1 + +require ( + apigo.cc/apigo/gojs v0.1.0 + github.com/ssgo/config v1.7.7 + github.com/ssgo/httpclient v1.7.7 + github.com/ssgo/u v1.7.7 +) + +require ( + filippo.io/edwards25519 v1.1.0 // indirect + github.com/dlclark/regexp2 v1.11.4 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect + github.com/go-sql-driver/mysql v1.8.1 // indirect + github.com/google/pprof v0.0.0-20241001023024-f4c0cfd0cf1d // indirect + github.com/mitchellh/mapstructure v1.4.1 // indirect + github.com/ssgo/dao v0.1.4 // indirect + github.com/ssgo/db v1.7.8 // indirect + github.com/ssgo/log v1.7.7 // indirect + github.com/ssgo/standard v1.7.7 // indirect + github.com/ssgo/tool v0.4.27 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/sys v0.25.0 // indirect + golang.org/x/text v0.18.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/test.js b/test.js new file mode 100644 index 0000000..36599ab --- /dev/null +++ b/test.js @@ -0,0 +1,6 @@ +import textin from "textin" +import file from "file" + +function main(){ + return {a : textin.OCR(file.read("img.png")), b : textin.OCRFromFile("img.png")} +} diff --git a/textin.go b/textin.go new file mode 100644 index 0000000..fabcc4d --- /dev/null +++ b/textin.go @@ -0,0 +1,82 @@ +package textin + +import ( + "apigo.cc/apigo/gojs" + "apigo.cc/apigo/gojs/dop251/goja" + _ "embed" + "errors" + "github.com/ssgo/config" + "github.com/ssgo/httpclient" + "github.com/ssgo/u" + "time" +) + +type Conf struct { + AKey string + SKey string +} + +//go:embed textin.ts +var textinTS string + +var conf = Conf{} + +var confAes = u.NewAes([]byte("?GQ$0K0GgLdO=f+~L68PLm$uhKr4'=tV"), []byte("VFs7@sK61cj^f?HZ")) +var keysIsSet = false + +func SetSSKey(key, iv []byte) { + if !keysIsSet { + confAes = u.NewAes(key, iv) + keysIsSet = true + } +} + +func init() { + obj := map[string]interface{}{ + "OCR": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { + args := gojs.MakeArgs(&argsIn, vm).Check(1) + return vm.ToValue(OCR(args.Bytes(0), vm)) + }, + "OCRFromFile": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { + path := gojs.MakeArgs(&argsIn, vm).Check(1).Path(0) + if file, err := u.ReadFileBytes(path); err != nil { + panic(vm.NewGoError(err)) + } else { + return vm.ToValue(OCR(file, vm)) + } + }, + } + config.LoadConfig("textin", &conf) + conf.AKey = confAes.DecryptUrlBase64ToString(conf.AKey) + conf.SKey = confAes.DecryptUrlBase64ToString(conf.SKey) + //log.DefaultLogger.Info("conf", "", conf) + gojs.Register("textin", gojs.Module{ + Object: obj, + TsCode: textinTS, + Example: "", + }) +} + +func OCR(file []byte, vm *goja.Runtime) string { + resMap := httpclient.GetClient(time.Second*30).Post("https://api.textin.com/ai/service/v2/recognize/multipage", file, "x-ti-app-id", conf.AKey, "x-ti-secret-code", conf.SKey).Map() + res := struct { + Result struct { + Pages []struct { + Lines []struct { + Text string + } `json:"lines"` + } `json:"pages"` + } `json:"result"` + }{} + //log.DefaultLogger.Info("Result", "", resMap) + str := "" + u.Convert(resMap, &res) + if len(res.Result.Pages) < 1 { + panic(vm.NewGoError(errors.New(u.BRed("no characters recognized")))) + } + for _, k := range res.Result.Pages[len(res.Result.Pages)-1].Lines { + str += "" + k.Text + } + //log.DefaultLogger.Info("OCRResult", "Map", resMap, "Text", str) + return str +} diff --git a/textin.ts b/textin.ts new file mode 100644 index 0000000..46408bb --- /dev/null +++ b/textin.ts @@ -0,0 +1,7 @@ +export default { + OCR, + OCRFromFile +} + +function OCR(pictureData) {return ""} +function OCRFromFile(picturePath) {return ""} \ No newline at end of file diff --git a/textin_test.go b/textin_test.go new file mode 100644 index 0000000..03e41fc --- /dev/null +++ b/textin_test.go @@ -0,0 +1,22 @@ +package textin + +import ( + "apigo.cc/apigo/gojs" + _ "apigo.cc/apigo/gojs/modules" + "fmt" + "github.com/ssgo/u" + "testing" +) + +func TestExport(t *testing.T) { + gojs.ExportForDev() +} + +func TestLLM(t *testing.T) { + r, err := gojs.RunFile("test.js") + if err != nil { + t.Fatal(err) + } + fmt.Println() + fmt.Println(u.JsonP(r)) +}