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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding: utf-8
|
| 2 |
+
# Copyright (C) 2022, [Breezedeus](https://github.com/breezedeus).
|
| 3 |
+
# Licensed to the Apache Software Foundation (ASF) under one
|
| 4 |
+
# or more contributor license agreements. See the NOTICE file
|
| 5 |
+
# distributed with this work for additional information
|
| 6 |
+
# regarding copyright ownership. The ASF licenses this file
|
| 7 |
+
# to you under the Apache License, Version 2.0 (the
|
| 8 |
+
# "License"); you may not use this file except in compliance
|
| 9 |
+
# with the License. You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing,
|
| 14 |
+
# software distributed under the License is distributed on an
|
| 15 |
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
| 16 |
+
# KIND, either express or implied. See the License for the
|
| 17 |
+
# specific language governing permissions and limitations
|
| 18 |
+
# under the License.
|
| 19 |
+
|
| 20 |
+
import os
|
| 21 |
+
|
| 22 |
+
from PIL import Image, ImageFilter
|
| 23 |
+
import streamlit as st
|
| 24 |
+
|
| 25 |
+
from cnocr import CnOcr
|
| 26 |
+
|
| 27 |
+
from antiocr import AntiOcr, BG_IMAGE_FP, FONT_NAMES, set_logger, download_font
|
| 28 |
+
|
| 29 |
+
logger = set_logger()
|
| 30 |
+
st.set_page_config(layout="wide")
|
| 31 |
+
FONT_LOCAL_DIR = 'fonts'
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@st.cache(allow_output_mutation=True)
|
| 35 |
+
def get_ocr_model():
|
| 36 |
+
return CnOcr()
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def save_image(img):
|
| 40 |
+
from io import BytesIO
|
| 41 |
+
|
| 42 |
+
buf = BytesIO()
|
| 43 |
+
img.save(buf, format="JPEG")
|
| 44 |
+
byte_im = buf.getvalue()
|
| 45 |
+
st.download_button(
|
| 46 |
+
label="下载图片",
|
| 47 |
+
data=byte_im,
|
| 48 |
+
file_name="antiOCR.jpeg",
|
| 49 |
+
mime="image/jpeg",
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def main():
|
| 54 |
+
st.sidebar.header('输出设置')
|
| 55 |
+
|
| 56 |
+
with st.spinner('Downloading fonts ...'):
|
| 57 |
+
for fnt_fp in FONT_NAMES:
|
| 58 |
+
download_font(os.path.join(FONT_LOCAL_DIR, fnt_fp))
|
| 59 |
+
|
| 60 |
+
font_fn = st.sidebar.selectbox('选择字体', FONT_NAMES, index=0)
|
| 61 |
+
font_fp = os.path.join(FONT_LOCAL_DIR, font_fn)
|
| 62 |
+
char_reverse_ratio = st.sidebar.slider(
|
| 63 |
+
'文字倒转概率', min_value=0.0, max_value=1.0, value=0.1
|
| 64 |
+
)
|
| 65 |
+
char_to_pinyin_ratio = st.sidebar.slider(
|
| 66 |
+
'文字转拼音概率', min_value=0.0, max_value=1.0, value=0.1
|
| 67 |
+
)
|
| 68 |
+
cols = st.sidebar.columns(2)
|
| 69 |
+
min_font_size = int(cols[0].number_input('最小文字大小', 2, 80, value=15))
|
| 70 |
+
max_font_size = int(
|
| 71 |
+
cols[1].number_input(
|
| 72 |
+
'最大文字大小', min_font_size + 1, 120, value=max(40, min_font_size + 1)
|
| 73 |
+
)
|
| 74 |
+
)
|
| 75 |
+
text_color = st.sidebar.color_picker('文字颜色', value='#5087DC')
|
| 76 |
+
|
| 77 |
+
st.sidebar.markdown('----')
|
| 78 |
+
use_random_bg = st.sidebar.checkbox('随机生成背景图片')
|
| 79 |
+
if use_random_bg:
|
| 80 |
+
bg_text_density = st.sidebar.slider(
|
| 81 |
+
'背景图片文字密度', min_value=0.0, max_value=3.0, value=1.0
|
| 82 |
+
)
|
| 83 |
+
cols = st.sidebar.columns(2)
|
| 84 |
+
bg_min_size = int(
|
| 85 |
+
cols[0].number_input('背景图片最小文字', 2, 80, key='bg_min', value=15)
|
| 86 |
+
)
|
| 87 |
+
bg_max_size = int(
|
| 88 |
+
cols[1].number_input(
|
| 89 |
+
'背景图片最大文字',
|
| 90 |
+
bg_min_size + 1,
|
| 91 |
+
120,
|
| 92 |
+
key='bg_max',
|
| 93 |
+
value=max(70, bg_min_size + 1),
|
| 94 |
+
)
|
| 95 |
+
)
|
| 96 |
+
bg_text_color = st.sidebar.color_picker('背景图片文字颜色', value='#07BCE0')
|
| 97 |
+
bg_gen_config = dict(
|
| 98 |
+
text_density=bg_text_density,
|
| 99 |
+
text_color=bg_text_color,
|
| 100 |
+
min_font_size=bg_min_size,
|
| 101 |
+
max_font_size=bg_max_size,
|
| 102 |
+
)
|
| 103 |
+
bg_image = None
|
| 104 |
+
else:
|
| 105 |
+
bg_gen_config = None
|
| 106 |
+
bg_image = Image.open(BG_IMAGE_FP)
|
| 107 |
+
bg_image = bg_image.filter(ImageFilter.MaxFilter(3))
|
| 108 |
+
|
| 109 |
+
title = '让文字自由传播:<a href="https://github.com/breezedeus/antiOCR">antiOCR</a>'
|
| 110 |
+
st.markdown(f"<h1 style='text-align: center;'>{title}</h1>", unsafe_allow_html=True)
|
| 111 |
+
subtitle = '作者:<a href="https://github.com/breezedeus">breezedeus</a>; ' \
|
| 112 |
+
'欢迎加入 <a href="https://cnocr.readthedocs.io/zh/latest/contact/">交流群</a>'
|
| 113 |
+
st.markdown(f"<div style='text-align: center;'>{subtitle}</div>", unsafe_allow_html=True)
|
| 114 |
+
st.markdown('')
|
| 115 |
+
st.markdown('')
|
| 116 |
+
desc = '<strong>antiOCR</strong> 对指定的文字(来自输入或者图片)进行处理,输出图片,此图片无法通过OCR技术识别出有意义的文字。'
|
| 117 |
+
st.markdown(f"<div style='text-align: left;'>{desc}</div>", unsafe_allow_html=True)
|
| 118 |
+
st.markdown('')
|
| 119 |
+
st.subheader('选择待转换文字图片,或者直接输入待转换文字')
|
| 120 |
+
default_texts = '真的猛士,敢于直面惨淡的人生,敢于正视淋漓的鲜血。这是怎样的哀痛者和幸福者?然而造化又常常为庸人设计,以时间的流逝,来洗涤旧迹,仅是留下淡红的血色和微漠的悲哀。在这淡红的血色和微漠的悲哀中,又给人暂得偷生,维持着这似人非人的世界。 ——鲁迅'
|
| 121 |
+
content_file = st.file_uploader('输入待转换的文字图片:', type=["png", "jpg", "jpeg", "webp"])
|
| 122 |
+
ocr = get_ocr_model()
|
| 123 |
+
anti = AntiOcr()
|
| 124 |
+
texts = None
|
| 125 |
+
if content_file is not None:
|
| 126 |
+
try:
|
| 127 |
+
img = Image.open(content_file).convert('RGB')
|
| 128 |
+
ocr_out = ocr.ocr(img)
|
| 129 |
+
texts = '\n'.join([out['text'] for out in ocr_out])
|
| 130 |
+
except Exception as e:
|
| 131 |
+
st.error(e)
|
| 132 |
+
|
| 133 |
+
texts = st.text_area('或者,直接输入待转换的文字:', value=texts or default_texts, height=120)
|
| 134 |
+
|
| 135 |
+
if st.button("生成图片"):
|
| 136 |
+
if texts:
|
| 137 |
+
with st.spinner('图片生成中…'):
|
| 138 |
+
out_img = anti(
|
| 139 |
+
texts,
|
| 140 |
+
char_reverse_ratio=char_reverse_ratio,
|
| 141 |
+
char_to_pinyin_ratio=char_to_pinyin_ratio,
|
| 142 |
+
text_color=text_color,
|
| 143 |
+
min_font_size=min_font_size,
|
| 144 |
+
max_font_size=max_font_size,
|
| 145 |
+
bg_image=bg_image,
|
| 146 |
+
bg_gen_config=bg_gen_config,
|
| 147 |
+
font_fp=font_fp,
|
| 148 |
+
)
|
| 149 |
+
st.subheader('输出图片')
|
| 150 |
+
st.image(out_img)
|
| 151 |
+
save_image(out_img)
|
| 152 |
+
|
| 153 |
+
st.markdown('**对输出图片进行OCR,结果如下(如果依旧出现敏感词,尝试重新生成图片):**')
|
| 154 |
+
ocr_out = ocr.ocr(out_img)
|
| 155 |
+
new_texts = [out['text'] for out in ocr_out]
|
| 156 |
+
st.text('\n'.join(new_texts))
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
if __name__ == '__main__':
|
| 160 |
+
main()
|