-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
110 lines (96 loc) · 2.54 KB
/
server.go
File metadata and controls
110 lines (96 loc) · 2.54 KB
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
//go:build linux
// +build linux
package recaptcha
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/otiai10/gosseract/v2"
"io"
"log"
)
func Recognition(captcha []byte, opt *Options) string {
client := gosseract.NewClient()
defer client.Close()
captcha, err := ThresholdBinary(captcha) //图像二值化
if err != nil {
log.Println(err)
return ""
}
client.SetImageFromBytes(captcha)
client.SetWhitelist(opt.Whitelist)
client.Languages = opt.Languages
if len(opt.Languages) == 0 {
client.Languages = []string{"eng"}
}
client.SetPageSegMode(gosseract.PageSegMode(opt.PageSegMode))
text, _ := client.Text()
return text
}
// RecCaptchaImageHandle
// https://gin-gonic.com/docs/examples/upload-file/multiple-file/
func RecCaptchaImageHandle(c *gin.Context) {
// Multipart form
form, err := c.MultipartForm()
if err != nil {
c.JSON(500, Result{Status: Error, Message: "Invalid multipart form"})
return
}
opt := new(Options)
if _, ok := form.Value["options"]; !ok {
c.JSON(500, Result{Status: Error, Message: fmt.Sprintf("Invalid options: %s", form.Value["options"])})
return
}
if err := json.Unmarshal([]byte(form.Value["options"][0]), opt); err != nil {
c.JSON(500, Result{Status: Error, Message: "Invalid options: " + form.Value["options"][0]})
return
}
files := form.File["captcha[]"]
res := Result{
Status: Success,
Message: "ok",
Data: make([]CaptchaText, 0),
}
for _, file := range files {
op, err := file.Open()
if err != nil {
log.Println(err)
c.JSON(500, Result{Status: Error, Message: "Invalid open file: " + file.Filename})
return
}
content, err := io.ReadAll(op)
if err != nil {
c.JSON(500, Result{Status: Error, Message: "Invalid read file: " + file.Filename})
return
}
res.Data = append(res.Data.([]CaptchaText), CaptchaText{
ID: file.Filename,
Text: Recognition(content, opt),
})
}
c.JSON(200, res)
}
func RecCaptchaBase64Handle(c *gin.Context) {
rb := new(CaptchaBase64)
if err := c.Bind(rb); err != nil {
c.JSON(500, Result{Status: Error, Message: "Invalid json " + err.Error()})
return
}
rbs := make([]CaptchaText, 0)
for x := range rb.Captcha {
// Decoding the string
content, err := base64.StdEncoding.DecodeString(rb.Captcha[x].Text)
var text string
if err != nil {
log.Println("Error decoding base64", err)
} else {
text = Recognition(content, &rb.Options)
}
rbs = append(rbs, CaptchaText{
ID: rb.Captcha[x].ID,
Text: text,
})
}
c.JSON(200, Result{Status: Success, Message: "ok", Data: rbs})
}