70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
from paddleocr import PaddleOCR, draw_ocr
|
|
import numpy as np
|
|
from flask import Flask, request, jsonify, render_template
|
|
from PIL import Image
|
|
import requests
|
|
import base64
|
|
import io
|
|
import cv2
|
|
import time
|
|
import json
|
|
|
|
# # Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
|
|
# # 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
|
|
# ocr = PaddleOCR(use_angle_cls=True, lang="ch", ocr_version='PP-OCRv4') # need to run only once to download and load model into memory
|
|
# # img_path = './imgs/11.jpg'
|
|
# img_path = './id_cards/card0.png'
|
|
# result = ocr.ocr(img_path, cls=True)
|
|
# print(result)
|
|
# # for idx in range(len(result)):
|
|
# # res = result[idx]
|
|
# # for line in res:
|
|
# # print(line)
|
|
#
|
|
# # 显示结果
|
|
# result = result[0]
|
|
# print(len(result))
|
|
# for line in result:
|
|
# print(line[1][0])
|
|
# image = Image.open(img_path).convert('RGB')
|
|
# boxes = [line[0] for line in result]
|
|
# txts = [line[1][0] for line in result]
|
|
# scores = [line[1][1] for line in result]
|
|
# im_show = draw_ocr(image, boxes, txts, scores, font_path='./fonts/simfang.ttf')
|
|
# im_show = Image.fromarray(im_show)
|
|
# im_show.save('result.jpg')
|
|
|
|
app = Flask(__name__)
|
|
ocr = PaddleOCR(use_angle_cls=True, lang="ch", ocr_version='PP-OCRv4')
|
|
|
|
@app.route('/')
|
|
def hello_world(): # put application's code here
|
|
# return 'Hello World!'
|
|
return render_template('index.html')
|
|
|
|
|
|
@app.route('/processing', methods=['POST'])
|
|
def process():
|
|
file = request.files['image'] # 获取图像
|
|
|
|
img_pil = Image.open(file.stream) # PIL读取图流
|
|
img_cv = cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR) # PIL->cv2
|
|
result = ocr.ocr(img=img_cv, cls=True)
|
|
txts = [line[1][0] for line in result[0]]
|
|
json_demo = {'result': txts, 'len': len(result[0])}
|
|
|
|
# 返回,需要看实际情况返回字段
|
|
return json_demo # 这个是把图片展示在前端
|
|
|
|
# @app.route('/show')
|
|
# def show_image():
|
|
# r = requests.get('https://s.cn.bing.net/th?id=OHR.CliffsEtretat_ZH-CN9911283373_UHD.jpg')
|
|
# # image = base64.b64encode(r.content).decode('ascii')
|
|
# image = base64.b64encode(r.content).decode()
|
|
# return render_template('show_image.html', img_stream=image)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='127.0.1.3', port=5555)
|
|
|