-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
98 lines (86 loc) · 1.98 KB
/
api.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
package src
import (
"errors"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
type OcrDTO struct {
ImageUrl string `json:"image_url"`
ImageBase64 string `json:"image_base_64"`
NeedBlock bool `json:"need_block"`
}
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func OcrJson(c *gin.Context) {
var input OcrDTO
err := c.ShouldBindWith(&input, binding.JSON)
if nil != err {
SendError(c, err.Error())
return
}
var imagePath string
if input.ImageBase64 != "" {
imagePath, err = saveBase64Image(input.ImageBase64)
} else if input.ImageUrl != "" {
imagePath, err = downloadAndSaveImage(input.ImageUrl)
}
if err != nil {
SendError(c, err.Error())
return
}
if imagePath == "" {
SendError(c, "请输入图片信息")
return
}
//识别
err, response := ocr(imagePath, input)
if err != nil {
SendError(c, err.Error())
} else {
c.JSON(http.StatusOK, response)
}
}
func OcrFile(c *gin.Context) {
var input OcrDTO
// 单文件
file, _ := c.FormFile("file")
if c.DefaultPostForm("need_block", "") == "true" {
input.NeedBlock = true
}
log.Println(file.Filename)
imagePath := "./tmp/" + file.Filename
// 上传文件至指定的完整文件路径
err := c.SaveUploadedFile(file, imagePath)
if err != nil {
SendError(c, err.Error())
return
}
//识别
err, response := ocr(imagePath, input)
if err != nil {
SendError(c, err.Error())
} else {
c.JSON(http.StatusOK, response)
}
}
func ocr(imagePath string, input OcrDTO) (error, *Response) {
detect, ocrResult := Detect(imagePath)
_ = os.Remove(imagePath)
_ = os.Remove(imagePath + "-result.jpg")
if !detect {
return errors.New("识别失败"), nil
}
if !input.NeedBlock {
ocrResult.TextBlocks = nil
}
return nil, &Response{Code: 200, Msg: "ok", Data: ocrResult}
}
func SendError(c *gin.Context, message string) {
c.JSON(http.StatusOK, Response{Code: 500, Msg: message, Data: nil})
}