breezedeus commited on
Commit
877c27b
1 Parent(s): c305175

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding: utf-8
2
+ # Copyright (C) 2022, [Breezedeus](https://github.com/breezedeus).
3
+
4
+ from PIL import Image, ImageFilter
5
+ import streamlit as st
6
+
7
+ from cnocr import CnOcr
8
+ from cnocr.utils import set_logger
9
+
10
+ from antiocr.anti_ocr import AntiOcr
11
+ from antiocr.consts import RESOURCE_PATH
12
+
13
+ logger = set_logger()
14
+ st.set_page_config(layout="wide")
15
+
16
+ FONT_FP = '/System/Library/Fonts/PingFang.ttc'
17
+
18
+
19
+ @st.cache(allow_output_mutation=True)
20
+ def get_ocr_model():
21
+ return CnOcr()
22
+
23
+
24
+ def main():
25
+ st.sidebar.header('输出设置')
26
+ char_reverse_ratio = st.sidebar.slider(
27
+ '文字倒转概率', min_value=0.0, max_value=1.0, value=0.1
28
+ )
29
+ char_to_pinyin_ratio = st.sidebar.slider(
30
+ '文字转拼音概率', min_value=0.0, max_value=1.0, value=0.1
31
+ )
32
+ cols = st.sidebar.columns(2)
33
+ min_font_size = int(cols[0].number_input('最小文字大小', 2, 80, value=15))
34
+ max_font_size = int(
35
+ cols[1].number_input(
36
+ '最大文字大小', min_font_size + 1, 120, value=max(40, min_font_size + 1)
37
+ )
38
+ )
39
+ text_color = st.sidebar.color_picker('文字颜色', value='#5087DC')
40
+
41
+ st.sidebar.markdown('----')
42
+ use_random_bg = st.sidebar.checkbox('随机生成背景图片')
43
+ if use_random_bg:
44
+ bg_text_density = st.sidebar.slider(
45
+ '背景图片文字密度', min_value=0.0, max_value=3.0, value=1.0
46
+ )
47
+ cols = st.sidebar.columns(2)
48
+ bg_min_size = int(
49
+ cols[0].number_input('背景图片最小文字', 2, 80, key='bg_min', value=15)
50
+ )
51
+ bg_max_size = int(
52
+ cols[1].number_input(
53
+ '背景图片最大文字',
54
+ bg_min_size + 1,
55
+ 120,
56
+ key='bg_max',
57
+ value=max(70, bg_min_size + 1),
58
+ )
59
+ )
60
+ bg_text_color = st.sidebar.color_picker('背景图片文字颜色', value='#07BCE0')
61
+ bg_gen_config = dict(
62
+ text_density=bg_text_density,
63
+ text_color=bg_text_color,
64
+ min_font_size=bg_min_size,
65
+ max_font_size=bg_max_size,
66
+ )
67
+ bg_image = None
68
+ else:
69
+ bg_gen_config = None
70
+ bg_image = Image.open(RESOURCE_PATH / 'bg.jpeg')
71
+ bg_image = bg_image.filter(ImageFilter.MaxFilter(3))
72
+
73
+ title = '让文字自由传播:<a href="https://github.com/breezedeus/antiOCR">antiOCR</a>'
74
+ st.markdown(f"<h1 style='text-align: center;'>{title}</h1>", unsafe_allow_html=True)
75
+ subtitle = '作者:<a href="https://github.com/breezedeus">breezedeus</a>; ' \
76
+ '欢迎加入 <a href="https://cnocr.readthedocs.io/zh/latest/contact/">交流群</a>'
77
+ st.markdown(f"<div style='text-align: center;'>{subtitle}</div>", unsafe_allow_html=True)
78
+ st.markdown('')
79
+ st.markdown('')
80
+ desc = '<strong>antiOCR</strong> 对指定的文字(来自输入或者图片)进行处理,输出图片,此图片无法通过OCR技术识别出有意义的文字。'
81
+ st.markdown(f"<div style='text-align: left;'>{desc}</div>", unsafe_allow_html=True)
82
+ st.markdown('')
83
+ st.subheader('选择待转换文字图片,或者直接输入待转换文字')
84
+ default_texts = '真的猛士,敢于直面惨淡的人生,敢于正视淋漓的鲜血。这是怎样的哀痛者和幸福者?然而造化又常常为庸人设计,以时间的流逝,来洗涤旧迹,仅是留下淡红的血色和微漠的悲哀。在这淡红的血色和微漠的悲哀中,又给人暂得偷生,维持着这似人非人的世界。 ——鲁迅'
85
+ content_file = st.file_uploader('待转换文字图片', type=["png", "jpg", "jpeg", "webp"])
86
+ ocr = get_ocr_model()
87
+ anti = AntiOcr()
88
+ texts = None
89
+ if content_file is not None:
90
+ try:
91
+ img = Image.open(content_file).convert('RGB')
92
+ ocr_out = ocr.ocr(img)
93
+ texts = '\n'.join([out['text'] for out in ocr_out])
94
+ except Exception as e:
95
+ st.error(e)
96
+
97
+ texts = st.text_area('待转换文字图片', value=texts or default_texts, height=120)
98
+
99
+ if st.button("生成图片"):
100
+ if texts:
101
+ with st.spinner('图片生成中…'):
102
+ out_img = anti(
103
+ texts,
104
+ char_reverse_ratio=char_reverse_ratio,
105
+ char_to_pinyin_ratio=char_to_pinyin_ratio,
106
+ text_color=text_color,
107
+ min_font_size=min_font_size,
108
+ max_font_size=max_font_size,
109
+ bg_image=bg_image,
110
+ bg_gen_config=bg_gen_config,
111
+ )
112
+ st.subheader('输出图片')
113
+ st.image(out_img)
114
+ st.markdown('**对输出图片进行OCR,结果如下(如果依旧出现敏感词,尝试重新生成图片):**')
115
+ ocr_out = ocr.ocr(out_img)
116
+ new_texts = [out['text'] for out in ocr_out]
117
+ st.text('\n'.join(new_texts))
118
+
119
+
120
+ if __name__ == '__main__':
121
+ main()