forked from ilyapashuk/cloudvis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudvis.go
145 lines (126 loc) · 3.22 KB
/
cloudvis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bytes"
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"mime/multipart"
"os"
"time"
requests "github.com/carlmjohnson/requests"
)
type UploadResponse struct {
Status string `json:"status"`
Id string `json:"id"`
}
type RecognitionResult struct {
UploadResponse
Text string `json:"text"`
QR string `json:"qr"`
}
func uploadImage(url string, params map[string]string, fileData []byte) (*UploadResponse, error) {
var upResponse UploadResponse
rbody := new(bytes.Buffer)
writer := multipart.NewWriter(rbody)
defer writer.Close()
filepart, _ := writer.CreateFormFile("file", "img")
filepart.Write(fileData)
for pname, pval := range params {
writer.WriteField(pname, pval)
}
err := requests.
URL(url).
BodyBytes(rbody.Bytes()).
ContentType(writer.FormDataContentType()).
ToJSON(&upResponse).
Fetch(context.Background())
return &upResponse, err
}
func getResult(url string, id string) (*RecognitionResult, error) {
var result RecognitionResult
err := requests.
URL(url).
Param("id", id).
ToJSON(&result).
Fetch(context.Background())
return &result, err
}
func main() {
flag.Usage = func() {
fmt.Printf("Usage: %s [opts] [file] \n file is a image file name. png and jpg are officially supported. \n Opts can be one of:\n", os.Args[0])
flag.PrintDefaults()
}
useStdin := flag.Bool("stdin", false, "Get image data from stdin, filename will be ignored")
lang := flag.String("l", "en", "Set `language` name to use. Language is formed as two letter code, like ru or en")
target := flag.String("t", "all", "Data `type` to recognize. Can be 'text', 'image' or 'all'")
translate := flag.Bool("tr", false, "Translate recognition result")
qr := flag.Bool("qr", false, "Recognize QR/BAR codes")
flag.Parse()
var file *os.File
if *useStdin {
file = os.Stdin
} else {
var filename = flag.Arg(0)
if filename == "" {
log.Fatal("File name not specified")
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
log.Fatalf("File %v does not exist", filename)
}
var err error
file, err = os.Open(filename)
if err != nil {
log.Fatal(err)
}
}
filedata, err := ioutil.ReadAll(file)
if err != nil {
log.Fatal(err)
}
defer file.Close()
const apiUrl = "https://visionbot.ru/apiv2"
params := map[string]string{
"lang": *lang,
"target": *target,
"qr": "0",
"translate": "0",
}
if *qr {
params["qr"] = "1"
}
if *translate {
params["translate"] = "1"
}
fmt.Println("Uploading image...")
resp, err := uploadImage(apiUrl+"/in.php", params, filedata)
if err != nil {
log.Fatal(err)
}
if resp.Status != "ok" {
log.Fatal("Server error. Try again later")
}
id := resp.Id
fmt.Println("Waiting for result...")
for {
res, err := getResult(apiUrl+"/res.php", id)
if err != nil {
log.Fatal(err)
}
if res.Status == "error" {
log.Fatal("Server error. Try again later")
} else if res.Status == "ok" {
if res.Text == "" {
fmt.Println("No text")
} else {
fmt.Println(res.Text)
}
if res.QR != "" {
fmt.Printf("\nQR:\n%v", res.QR)
}
break
}
time.Sleep(1 * time.Second)
}
}