From f89cf56d86fb5685fd3146c79cda6141a4148563 Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Mon, 3 Jul 2023 17:46:58 +0530 Subject: [PATCH] add tesseract support --- cmd/imgtxt.go | 26 ++++++++++++++++++++++++++ go.mod | 1 + go.sum | 7 +++++++ utils/engines.go | 1 - utils/imgtxt.go | 13 +++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 cmd/imgtxt.go create mode 100644 utils/imgtxt.go diff --git a/cmd/imgtxt.go b/cmd/imgtxt.go new file mode 100644 index 0000000..1f44b83 --- /dev/null +++ b/cmd/imgtxt.go @@ -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() +} diff --git a/go.mod b/go.mod index 4889596..137d838 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e54fd0d..e41e929 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/utils/engines.go b/utils/engines.go index c4b77e6..dfc3cbc 100644 --- a/utils/engines.go +++ b/utils/engines.go @@ -3,7 +3,6 @@ package utils import ( "github.com/gocolly/colly" "os" - "strings" ) func TranslateGoogle(to string, from string, query string) string { diff --git a/utils/imgtxt.go b/utils/imgtxt.go new file mode 100644 index 0000000..e1cc281 --- /dev/null +++ b/utils/imgtxt.go @@ -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 +}