add tesseract support

This commit is contained in:
Arya 2023-07-03 17:46:58 +05:30
parent feb3baef9e
commit f89cf56d86
Signed by: arya
GPG Key ID: 842D12BDA50DF120
5 changed files with 47 additions and 1 deletions

26
cmd/imgtxt.go Normal file
View File

@ -0,0 +1,26 @@
package cmd
import (
"codeberg.org/aryak/simplytranslate/utils"
"github.com/spf13/cobra"
"fmt"
)
var file string
var imgtxtCmd = &cobra.Command{
Use: "imgtxt",
Short: "Image -> Text.",
Long: `Convert given image (filename) to text using gosseract.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(utils.ImgTxt(file))
},
}
func init() {
rootCmd.AddCommand(imgtxtCmd)
imgtxtCmd.Flags().StringVarP(&file, "file", "f", "", "The query SimplyTranslate will listen to. Defaults to 3000, and overrides the SIMPLYTRANSLATE_query environment variable.")
langlist = imgtxtCmd.Flag("file").Value.String()
}

1
go.mod
View File

@ -29,6 +29,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/otiai10/gosseract/v2 v2.4.0 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect

7
go.sum
View File

@ -327,6 +327,13 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
github.com/otiai10/gosseract/v2 v2.4.0 h1:gYd3mx6FuMtIlxL4sYb9JLCFEDzg09VgNSZRNbqpiGM=
github.com/otiai10/gosseract/v2 v2.4.0/go.mod h1:fhbIDRh29bj13vni6RT3gtWKjKCAeqDYI4C1dxeJuek=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI=
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=

View File

@ -3,7 +3,6 @@ package utils
import (
"github.com/gocolly/colly"
"os"
"strings"
)
func TranslateGoogle(to string, from string, query string) string {

13
utils/imgtxt.go Normal file
View File

@ -0,0 +1,13 @@
package utils
import (
"github.com/otiai10/gosseract/v2"
)
func ImgTxt(file string) string {
client := gosseract.NewClient()
defer client.Close()
client.SetImage(file)
text, _ := client.Text()
return text
}