init
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
conda activate D:\Program Files\anaconda3\envs\paddle_env D:\Program Files\anaconda3\Scripts\conda.exe install -p D:/Program Files/anaconda3/envs/paddle_env
|
||||
|
||||
# 文字识别
|
||||
paddleocr --image_dir ./xjr_application/id_cards/card0.png --use_angle_cls true --use_gpu false
|
||||
|
||||
# 预测分类结果
|
||||
python ./tools/infer_cls.py -c configs/cls/cls_mv3.yml -o Global.infer_img=./imgs/color.png
|
||||
|
||||
python ./tools/infer_cls.py -c configs/cls/cls_mv3.yml -o Global.infer_img=./ppocr_img/id_cards/card1.png
|
||||
|
||||
|
||||
python ./tools/infer/predict_det.py --image_dir="./xjr_application/id_cards/card0.png" --det_model_dir="./xjr_application/model/ch_PP-OCRv3_det_infer/"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
import cv2
|
||||
import base64
|
||||
import os
|
||||
import sys
|
||||
import copy
|
||||
|
||||
sys.path.insert(0, ".")
|
||||
|
||||
from tools.infer.utility import parse_args
|
||||
from tools.infer.predict_det import TextDetector
|
||||
|
||||
|
||||
def cv2_to_base64(image):
|
||||
return base64.b64encode(image).decode('utf8')
|
||||
|
||||
def merge_configs():
|
||||
# deafult cfg
|
||||
backup_argv = copy.deepcopy(sys.argv)
|
||||
sys.argv = sys.argv[:1]
|
||||
cfg = parse_args()
|
||||
|
||||
update_cfg_map = vars(read_params())
|
||||
|
||||
for key in update_cfg_map:
|
||||
cfg.__setattr__(key, update_cfg_map[key])
|
||||
|
||||
sys.argv = copy.deepcopy(backup_argv)
|
||||
return cfg
|
||||
|
||||
class Config(object):
|
||||
pass
|
||||
|
||||
|
||||
def read_params():
|
||||
cfg = Config()
|
||||
|
||||
# params for text detector
|
||||
cfg.det_algorithm = "DB"
|
||||
# cfg.det_model_dir = "./inference/idCard2/ch_db_mv3_sfz/"
|
||||
cfg.det_model_dir = "./output_inference/ch_db_mv3_sfz/"
|
||||
# cfg.det_model_dir = "./inference/idCard2/det_r50_vd_db_sfz/"
|
||||
cfg.det_resize_long = 960
|
||||
|
||||
# DB parmas
|
||||
cfg.det_db_thresh = 0.3
|
||||
cfg.det_db_box_thresh = 0.5
|
||||
cfg.det_db_unclip_ratio = 2.0
|
||||
cfg.use_dilation = False
|
||||
cfg.det_db_score_mode = "fast"
|
||||
|
||||
# #EAST parmas
|
||||
# cfg.det_east_score_thresh = 0.8
|
||||
# cfg.det_east_cover_thresh = 0.1
|
||||
# cfg.det_east_nms_thresh = 0.2
|
||||
|
||||
cfg.use_pdserving = False
|
||||
cfg.use_tensorrt = False
|
||||
|
||||
return cfg
|
||||
|
||||
|
||||
# 身份证识别
|
||||
class IdCard(object):
|
||||
# 初始化
|
||||
def __init__(self, text_system, args):
|
||||
self.text_system = text_system
|
||||
self.score = 0.5
|
||||
# 实例分割
|
||||
# if args["idCard"]["accuracy"] == "accurate":
|
||||
# self.directory = "./inference/idCard/cascade_mask_rcnn_mobilenetv3_fpn_1x/"
|
||||
# else:
|
||||
# self.directory = "./inference/idCard/mask_rcnn_mobilenetv3_fpn_1x/"
|
||||
|
||||
cfg = merge_configs()
|
||||
|
||||
cfg.use_gpu = args["use_gpu"]
|
||||
if args["use_gpu"]:
|
||||
try:
|
||||
_places = os.environ["CUDA_VISIBLE_DEVICES"]
|
||||
int(_places[0])
|
||||
print("use gpu: ", args["use_gpu"])
|
||||
print("CUDA_VISIBLE_DEVICES: ", _places)
|
||||
cfg.gpu_mem = 8000
|
||||
except:
|
||||
raise RuntimeError(
|
||||
"Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id."
|
||||
)
|
||||
cfg.ir_optim = True
|
||||
cfg.enable_mkldnn = args["enable_mkldnn"]
|
||||
|
||||
self.model = TextDetector(cfg)
|
||||
|
||||
def idCard(self, img):
|
||||
dt_boxes, dt_labels, elapse = self.model(img, cls=True)
|
||||
|
||||
print(dt_boxes)
|
||||
print(dt_labels)
|
||||
new_results = {}
|
||||
|
||||
categorys = ["name", "sex", "nation", "birthday", "address", "number"]
|
||||
|
||||
if str(new_results) == '{}':
|
||||
return ""
|
||||
|
||||
return new_results
|
||||
|
||||
def __call__(self, img=None):
|
||||
return self.idCard(img)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# from deploy.hubserving.ocr_system.module import OCRSystem
|
||||
# text_system = OCRSystem({
|
||||
# "use_gpu": True,
|
||||
# "enable_mkldnn": False
|
||||
# }).text_sys
|
||||
|
||||
module = IdCard(None, {
|
||||
# "use_gpu": True,
|
||||
"use_gpu": False,
|
||||
"enable_mkldnn": False
|
||||
})
|
||||
# image = cv2.imread('E:\\dataset\\shenfenzheng\\images\\JPEGImages\\001.png')
|
||||
img_path = './id_cards/card1.png'
|
||||
image = cv2.imread(img_path)
|
||||
res = module(img=image)
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 71 KiB |
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>index</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="/processing" method="POST" enctype="multipart/form-data">
|
||||
<input type="file" name="image">
|
||||
<button>OK</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Show image</title>
|
||||
</head>
|
||||
<body>
|
||||
<img src="data:;base64,{{ img_stream }}">
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,69 @@
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user