PaddleHub+PaddleOCR+FALSK 文字识别

PaddleHub+PaddleOCR+FALSK 文字识别

背景

​ 识别验证码

环境

  1. paddlepaddle2.0
  2. paddlehub2.0
  3. python3.7
  4. miniconda4.8.3
  5. shapely1.7.1
  6. pyclipper1.2.1

安装与配置

首先安装miniconda

1.下载,使用清华下载源,进入miniconda下载页面

​ https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/

​ https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py37_4.8.3-Linux-x86_64.sh

2.安装

bash Miniconda3-py37_4.8.3-Linux-x86_64.sh

3.In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue

​ 回车–》q #退出阅读

4.Do you accept the license terms? [yes|no]

​ yes

5.Miniconda3 will now be installed into this location:
/home/aiuser/miniconda3

  • Press ENTER to confirm the location

  • Press CTRL-C to abort the installation

  • Or specify a different location below

    回车

6.Do you wish the installer to initialize Miniconda3
by running conda init? [yes|no]

​ no

​ 安装完成

7.配置.condarc

vim ~/.condarc
# 复制https://mirror.tuna.tsinghua.edu.cn/help/anaconda/里的配置文件

channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

安装PaddlePaddle

#激活虚拟环境
source ~/miniconda3/bin/activate

#创建虚拟环境
conda create -n ocr python=3.7

#进入虚拟环境
conda activate ocr

#退出虚拟环境
#conda deactivate

# 安装gpu版本的PaddlePaddle
pip install paddlepaddle-gpu==2.0.0rc

# 或者安装cpu版本的paddlepaddle
# pip install paddlepaddle==2.0.0rc

#安装paddlehub
pip install paddlehub==2.0.0rc0

#安装依赖
pip install shapely
pip install pyclipper

#检查paddlehub安装是否成功
#import paddlehub
#paddlehub.server_check()
# 如果可以连接远端PaddleHub-Server,则显示Request Hub-Server successfully。
# 如果无法连接远端PaddleHub-Server,则显示Request Hub-Server unsuccessfully。



Flask接口调用

server

# -- coding: utf-8 --
import base64

from flask import Flask, jsonify, request, send_from_directory
import requests, os, time
import paddlehub as hub
import cv2,json
import numpy as np

app = Flask(__name__)

path = os.getcwd()
ocr = hub.Module(name="chinese_ocr_db_crnn_server")

def download_img(img_url, api_token=None):
    print (img_url)
    # header = {"Authorization": "Bearer " + api_token} # 设置http header,视情况加需要的条目,这里的token是用来鉴权的一种方式
    r = requests.get(img_url, stream=True)
    print(r.status_code) # 返回状态码
    if r.status_code == 200:
        p, n = getNowTime().split(" ")
        n = n.replace(":","")
        folder_path = f"{path}/{p}"
        if not os.path.exists(folder_path):  # 判断是否存在文件夹如果不存在则创建为文件夹
            os.makedirs(folder_path)
        open(f"{folder_path}/{n}.jpg", 'wb').write(r.content) # 将内容写入图片
    del r
    return f"{folder_path}/{n}.jpg"

def getNowTime(format="%Y-%m-%d %H:%M:%S"):
    return time.strftime(format, time.localtime())

@app.route("/imageToStr", methods=["GET","POST"])
def imageToStr():
    # json就是字符串
    url = request.args.get("url")
    path = download_img(url)
    print(path)
    # result = ocr.recognize_text(images=[cv2.imread(path)])
    result = {}
    return jsonify(data=result)

@app.route("/base64ToStr", methods=["GET","POST"])
def base64ToStr():
    data = str(request.data,'utf-8')
    data = json.loads(data)
    img_str = data["image"]
    img_decode_ = img_str.encode('ascii')  # ascii编码
    img_decode = base64.b64decode(img_decode_)  # base64解码
    img_np = np.frombuffer(img_decode, np.uint8)  # 从byte数据读取为np.array形式
    img = cv2.imdecode(img_np, cv2.COLOR_RGB2BGR)  # 转为OpenCV形式

    p, n = getNowTime().split(" ")
    n = n.replace(":", "")
    folder_path = f"{path}/{p}"
    if not os.path.exists(folder_path):  # 判断是否存在文件夹如果不存在则创建为文件夹
        os.makedirs(folder_path)
    cv2.imwrite(f"{folder_path}/{n}.jpg", img)
    result = ocr.recognize_text(images=[img])
    return jsonify(data=result)

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=8878)

client

# -- coding: utf-8 --
import requests
import json
import base64
import socket
import numpy as np
import cv2

def getByte(path):
    with open(path, 'rb') as f:
        img_byte = base64.b64encode(f.read())
    img_str = img_byte.decode('ascii')
    return img_str


img_str = getByte('1.jpg')

url = 'http://ip:8878/base64ToStr'
print(img_str)
data = {'image': img_str}
json_mod = json.dumps(data)
res = requests.post(url=url, data=json_mod)
print(res.text)
print(eval(res.text))
print(type(img_str))
img_decode_ = img_str.encode('ascii')  # ascii编码
img_decode = base64.b64decode(img_decode_)  # base64解码
img_np = np.frombuffer(img_decode, np.uint8)  # 从byte数据读取为np.array形式
img = cv2.imdecode(img_np, cv2.COLOR_RGB2BGR)  # 转为OpenCV形式

# 显示图像
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()

遇到的问题

1.ImportError: libGL.so.1: cannot open shared object file: No such file or directory

sudo apt-get update
sudo apt-get install -y libgl1-mesa-dev
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页