1 Star 0 Fork 0

JJusti / img2txt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
img2txt.go 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
LiuYajun 提交于 2017-05-23 16:06 . add img2txt
package main
import (
"bytes"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
"github.com/go-redis/redis"
)
func callShellScript(s string) string {
cmd := exec.Command("/bin/sh", "-c", s)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
return out.String()
}
func img2txt(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, err := template.ParseFiles("./img2txt.gptl")
if err != nil {
err.Error()
}
t.Execute(w, nil)
} else {
result, _:= ioutil.ReadAll(r.Body)
r.Body.Close()
filename := "./upload/" + r.Header.Get("filename")
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0666)
f.Write(result)
if err != nil {
err.Error()
}
f.Close()
txt := callShellScript("tesseract " + filename + " stdout")
response := strings.TrimSpace(txt)
w.Write([]byte(response))
log.Println("img2txt " + filename + " " + response)
}
}
func getcid(w http.ResponseWriter, r *http.Request) {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
if err := client.Incr("app_img2txt_cid").Err(); err != nil {
panic(err)
}
cid, _ := client.Get("app_img2txt_cid").Result()
w.Write([]byte(cid))
}
func main() {
http.HandleFunc("/getcid", getcid)
http.HandleFunc("/img2txt", img2txt)
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal("listenAndServe: ", err)
}
}
Go
1
https://gitee.com/eStatMiner/img2txt.git
git@gitee.com:eStatMiner/img2txt.git
eStatMiner
img2txt
img2txt
master

搜索帮助