使用Golang+腾讯云实现文字识别/健康码识别等,并使用Docker部署Goweb到阿里云服务器上

从B站上看到的一个不错的Golang项目,避免自己遗忘,记录一下
参考B站视频:https://www.bilibili.***/video/BV15L41187LY?p=13&vd_source=bdee36feb4edbfc7100eaadac4772a5e



部署部分(这部分自己探索的)

需要一台云服务器(我是阿里云),并安装docker(使用Dockerfile)


一、上传项目到阿里云服务器

依旧是xshell,注意把Go项目放进压缩包,不能直接上传项目文件夹。
具体上传步骤可以参考我之前的文章,部署springboot项目那篇。

二、部署

1.解压项目

我的Go项目名为ocr

unzip ocr.zip

2.使用Docker

mkdir ocr
cd ocr
vi Dockerfile

Dockerfile内容如下:


# 使用官方的 Go 镜像作为基础镜像
FROM golang:latest

# 设置工作目录
WORKDIR /app

# 复制 go.mod 和 go.sum 文件并下载依赖
COPY go.mod .
COPY go.sum .
RUN go env -w GOPROXY=https://proxy.golang.***.***,https://goproxy.***,direct
RUN go mod download

# 将本地的代码复制到容器中的工作目录
COPY . .

# 编译 Go 项目
RUN go build -o main .

# 暴露应用程序的端口
EXPOSE 80
EXPOSE 8080

# 运行应用程序
CMD ["./main"]

接着执行:

docker build -t  ocr .
docker run -p 8080:8080 -p 80:80 ocr

执行完毕查看docker 的images和containers:

三:测试

使用Apifox进行测试

测试对普通印刷物品

使用imageUrl
访问:http://47.94.246.106:80/ocr/general/basic,post请求,body体传输必要参数

服务器会返回一系列json格式数据。

DetectedText : 所识别的文字
Confidence : 置信度
Polygon : 检测到的四个角的坐标
ItemPolygon : 文本行在旋转纠正之后的图像中的像素坐标,表示为(左上角 x, 左上角 y,宽 width,高 height)。
{
    "Response": {
        "TextDetections": [
            {
                "DetectedText": "TetraP",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 183,
                        "Y": 43
                    },
                    {
                        "X": 206,
                        "Y": 10
                    },
                    {
                        "X": 214,
                        "Y": 16
                    },
                    {
                        "X": 191,
                        "Y": 49
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":1}}",
                "ItemPolygon": {
                    "X": 477,
                    "Y": 173,
                    "Width": 40,
                    "Height": 10
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "生牛乳",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 141,
                        "Y": 296
                    },
                    {
                        "X": 220,
                        "Y": 260
                    },
                    {
                        "X": 235,
                        "Y": 293
                    },
                    {
                        "X": 156,
                        "Y": 330
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":8}}",
                "ItemPolygon": {
                    "X": 249,
                    "Y": 290,
                    "Width": 86,
                    "Height": 36
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "特仑苏",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 227,
                        "Y": 133
                    },
                    {
                        "X": 307,
                        "Y": 34
                    },
                    {
                        "X": 342,
                        "Y": 63
                    },
                    {
                        "X": 262,
                        "Y": 161
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":2}}",
                "ItemPolygon": {
                    "X": 431,
                    "Y": 262,
                    "Width": 127,
                    "Height": 45
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "全脂灭菌乳",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 159,
                        "Y": 338
                    },
                    {
                        "X": 291,
                        "Y": 279
                    },
                    {
                        "X": 305,
                        "Y": 310
                    },
                    {
                        "X": 173,
                        "Y": 369
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":7}}",
                "ItemPolygon": {
                    "X": 226,
                    "Y": 329,
                    "Width": 144,
                    "Height": 34
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "贮存条件:常温密闭保存。",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 258,
                        "Y": 238
                    },
                    {
                        "X": 377,
                        "Y": 86
                    },
                    {
                        "X": 393,
                        "Y": 99
                    },
                    {
                        "X": 274,
                        "Y": 251
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":3}}",
                "ItemPolygon": {
                    "X": 365,
                    "Y": 349,
                    "Width": 193,
                    "Height": 20
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "n",
                "Confidence": 50,
                "Polygon": [
                    {
                        "X": 156,
                        "Y": 390
                    },
                    {
                        "X": 171,
                        "Y": 418
                    },
                    {
                        "X": 154,
                        "Y": 428
                    },
                    {
                        "X": 138,
                        "Y": 399
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":18}}",
                "ItemPolygon": {
                    "X": 182,
                    "Y": 358,
                    "Width": 31,
                    "Height": 19
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "GB 25190",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 178,
                        "Y": 377
                    },
                    {
                        "X": 300,
                        "Y": 324
                    },
                    {
                        "X": 314,
                        "Y": 356
                    },
                    {
                        "X": 192,
                        "Y": 410
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":18}}",
                "ItemPolygon": {
                    "X": 206,
                    "Y": 368,
                    "Width": 133,
                    "Height": 34
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "开启前,无需冷藏",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 274,
                        "Y": 250
                    },
                    {
                        "X": 359,
                        "Y": 142
                    },
                    {
                        "X": 375,
                        "Y": 155
                    },
                    {
                        "X": 290,
                        "Y": 263
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":4}}",
                "ItemPolygon": {
                    "X": 365,
                    "Y": 369,
                    "Width": 137,
                    "Height": 20
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "开启后,请立即饮用。",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 290,
                        "Y": 262
                    },
                    {
                        "X": 386,
                        "Y": 138
                    },
                    {
                        "X": 403,
                        "Y": 150
                    },
                    {
                        "X": 307,
                        "Y": 275
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":9}}",
                "ItemPolygon": {
                    "X": 365,
                    "Y": 389,
                    "Width": 156,
                    "Height": 20
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "6个月",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 196,
                        "Y": 421
                    },
                    {
                        "X": 263,
                        "Y": 390
                    },
                    {
                        "X": 278,
                        "Y": 422
                    },
                    {
                        "X": 210,
                        "Y": 453
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":24}}",
                "ItemPolygon": {
                    "X": 181,
                    "Y": 409,
                    "Width": 73,
                    "Height": 35
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "请勿连同包装在微波炉中加热。",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 307,
                        "Y": 273
                    },
                    {
                        "X": 438,
                        "Y": 97
                    },
                    {
                        "X": 456,
                        "Y": 110
                    },
                    {
                        "X": 325,
                        "Y": 286
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":7}}",
                "ItemPolygon": {
                    "X": 366,
                    "Y": 409,
                    "Width": 219,
                    "Height": 22
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "贝包装喷码",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 218,
                        "Y": 458
                    },
                    {
                        "X": 338,
                        "Y": 404
                    },
                    {
                        "X": 352,
                        "Y": 436
                    },
                    {
                        "X": 233,
                        "Y": 490
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":26}}",
                "ItemPolygon": {
                    "X": 164,
                    "Y": 448,
                    "Width": 131,
                    "Height": 34
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "料",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 376,
                        "Y": 245
                    },
                    {
                        "X": 388,
                        "Y": 227
                    },
                    {
                        "X": 403,
                        "Y": 237
                    },
                    {
                        "X": 391,
                        "Y": 255
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":10}}",
                "ItemPolygon": {
                    "X": 430,
                    "Y": 448,
                    "Width": 21,
                    "Height": 18
                },
                "Words": [],
                "WordCoordPoint": []
            },
            {
                "DetectedText": "生牛乳",
                "Confidence": 100,
                "Polygon": [
                    {
                        "X": 388,
                        "Y": 225
                    },
                    {
                        "X": 420,
                        "Y": 183
                    },
                    {
                        "X": 436,
                        "Y": 195
                    },
                    {
                        "X": 403,
                        "Y": 238
                    }
                ],
                "AdvancedInfo": "{\"Parag\":{\"ParagNo\":5}}",
                "ItemPolygon": {
                    "X": 453,
                    "Y": 446,
                    "Width": 52,
                    "Height": 20
                },
                "Words": [],
                "WordCoordPoint": []
            },
            ... ...
            ... ...
            ... ...
            ... ...

测试对普通印刷物品,使用image_file
访问:http://47.94.246.106:80/ocr/general/basic,post请求,body体传输必要参数

识别成功。

测试健康码

使用imageUrl
访问:http://47.94.246.106:80/ocr/general/healthcode,post请求,body体传输必要参数


服务器响应返回json数据:

{
    "Response": {
        "Name": "郭**",
        "IDNumber": "",
        "Time": "09-29 11:17:41",
        "Color": "绿色",
        "TestingInterval": "",
        "TestingResult": "阴性",
        "TestingTime": "2021-08-27 17:00",
        "Va***ination": "暂未查询到数据",
        "SpotName": "",
        "Va***inationTime": "",
        "RequestId": "45fe12a8-9018-46ae-874b-07ba53703475"
    }
}

使用image_file
访问:http://47.94.246.106:80/ocr/general/healthcode,post请求,body体传输必要参数

识别成功

四、还有几件事

1、整个识别逻辑在腾讯云那里,还有很多可以识别的东西,比如车牌识别等等, 识别质量还有高精度识别等。
2、写完一个识别,基于固定套路很容易扩展,基本当时写完文字识别,自己调了差不多5分钟,就把健康码识别搞定了。
3、现在只是返回了Json数据,如何实现友好的前端交互渲染,额目前先不考虑,相比之下自己更喜欢后端,而且想把Go学好。
4、记录一下,免得哪天自己忘了,过来能回忆回忆。
5、还有就是,腾讯云的这个识别,刚免费开通时提供1000次免费次数,之后需要购买。

3月12日上午,使用Gin框架完成了项目结构的重写。

转载请说明出处内容投诉
CSS教程_站长资源网 » 使用Golang+腾讯云实现文字识别/健康码识别等,并使用Docker部署Goweb到阿里云服务器上

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买