Spaces:
Running
Running
File size: 6,870 Bytes
66658a5 |
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# coding: utf-8
# Copyright (C) 2021, [Breezedeus](https://github.com/breezedeus).
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import os
from collections import OrderedDict
import cv2
import numpy as np
from PIL import Image
import streamlit as st
from cnstd.utils import pil_to_numpy, imsave
from cnocr import CnOcr, DET_AVAILABLE_MODELS, REC_AVAILABLE_MODELS
from cnocr.utils import set_logger, draw_ocr_results, download
logger = set_logger()
st.set_page_config(layout="wide")
def plot_for_debugging(rotated_img, one_out, box_score_thresh, crop_ncols, prefix_fp):
import matplotlib.pyplot as plt
import math
rotated_img = rotated_img.copy()
crops = [info['cropped_img'] for info in one_out]
print('%d boxes are found' % len(crops))
ncols = crop_ncols
nrows = math.ceil(len(crops) / ncols)
fig, ax = plt.subplots(nrows=nrows, ncols=ncols)
for i, axi in enumerate(ax.flat):
if i >= len(crops):
break
axi.imshow(crops[i])
crop_fp = '%s-crops.png' % prefix_fp
plt.savefig(crop_fp)
print('cropped results are save to file %s' % crop_fp)
for info in one_out:
box, score = info.get('position'), info['score']
if score < box_score_thresh: # score < 0.5
continue
if box is not None:
box = box.astype(int).reshape(-1, 2)
cv2.polylines(rotated_img, [box], True, color=(255, 0, 0), thickness=2)
result_fp = '%s-result.png' % prefix_fp
imsave(rotated_img, result_fp, normalized=False)
print('boxes results are save to file %s' % result_fp)
@st.cache_resource
def get_ocr_model(det_model_name, rec_model_name, det_more_configs):
det_model_name, det_model_backend = det_model_name
rec_model_name, rec_model_backend = rec_model_name
return CnOcr(
det_model_name=det_model_name,
det_model_backend=det_model_backend,
rec_model_name=rec_model_name,
rec_model_backend=rec_model_backend,
det_more_configs=det_more_configs,
)
def visualize_naive_result(img, det_model_name, std_out, box_score_thresh):
img = pil_to_numpy(img).transpose((1, 2, 0)).astype(np.uint8)
plot_for_debugging(img, std_out, box_score_thresh, 2, './streamlit-app')
st.subheader('Detection Result')
if det_model_name == 'default_det':
st.warning('⚠️ Warning: "default_det" 检测模型不返回文本框位置!')
cols = st.columns([1, 7, 1])
cols[1].image('./streamlit-app-result.png')
st.subheader('Recognition Result')
cols = st.columns([1, 7, 1])
cols[1].image('./streamlit-app-crops.png')
_visualize_ocr(std_out)
def _visualize_ocr(ocr_outs):
st.empty()
ocr_res = OrderedDict({'文本': []})
ocr_res['得分'] = []
for out in ocr_outs:
# cropped_img = out['cropped_img'] # 检测出的文本框
ocr_res['得分'].append(out['score'])
ocr_res['文本'].append(out['text'])
st.table(ocr_res)
def visualize_result(img, ocr_outs):
out_draw_fp = './streamlit-app-det-result.png'
font_path = 'docs/fonts/simfang.ttf'
if not os.path.exists(font_path):
url = 'https://huggingface.co/datasets/breezedeus/cnocr-wx-qr-code/resolve/main/fonts/simfang.ttf'
os.makedirs(os.path.dirname(font_path), exist_ok=True)
download(url, path=font_path, overwrite=True)
draw_ocr_results(img, ocr_outs, out_draw_fp, font_path)
st.image(out_draw_fp)
def main():
st.sidebar.header('模型设置')
det_models = list(DET_AVAILABLE_MODELS.all_models())
det_models.append(('naive_det', 'onnx'))
det_models.sort()
det_model_name = st.sidebar.selectbox(
'选择检测模型', det_models, index=det_models.index(('ch_PP-OCRv3_det', 'onnx'))
)
all_models = list(REC_AVAILABLE_MODELS.all_models())
all_models.sort()
idx = all_models.index(('densenet_lite_136-fc', 'onnx'))
rec_model_name = st.sidebar.selectbox('选择识别模型', all_models, index=idx)
st.sidebar.subheader('检测参数')
rotated_bbox = st.sidebar.checkbox('是否检测带角度文本框', value=True)
use_angle_clf = st.sidebar.checkbox('是否使用角度预测模型校正文本框', value=False)
new_size = st.sidebar.slider(
'resize 后图片(长边)大小', min_value=124, max_value=4096, value=768
)
box_score_thresh = st.sidebar.slider(
'得分阈值(低于阈值的结果会被过滤掉)', min_value=0.05, max_value=0.95, value=0.3
)
min_box_size = st.sidebar.slider(
'框大小阈值(更小的文本框会被过滤掉)', min_value=4, max_value=50, value=10
)
# std = get_std_model(det_model_name, rotated_bbox, use_angle_clf)
# st.sidebar.markdown("""---""")
# st.sidebar.header('CnOcr 设置')
det_more_configs = dict(rotated_bbox=rotated_bbox, use_angle_clf=use_angle_clf)
ocr = get_ocr_model(det_model_name, rec_model_name, det_more_configs)
st.markdown('# 开源Python OCR工具 ' '[CnOCR](https://github.com/breezedeus/cnocr)')
st.markdown('> 详细说明参见:[CnOCR 文档](https://cnocr.readthedocs.io/) ;'
'欢迎加入 [交流群](https://www.breezedeus.com/join-group) ;'
'作者:[breezedeus](https://www.breezedeus.com), [Github](https://github.com/breezedeus) 。')
st.markdown('')
st.subheader('选择待检测图片')
content_file = st.file_uploader('', type=["png", "jpg", "jpeg", "webp"])
if content_file is None:
st.stop()
try:
img = Image.open(content_file).convert('RGB')
ocr_out = ocr.ocr(
img,
return_cropped_image=True,
resized_shape=new_size,
preserve_aspect_ratio=True,
box_score_thresh=box_score_thresh,
min_box_size=min_box_size,
)
if det_model_name[0] == 'naive_det':
visualize_naive_result(img, det_model_name[0], ocr_out, box_score_thresh)
else:
visualize_result(img, ocr_out)
except Exception as e:
st.error(e)
if __name__ == '__main__':
main()
|