breezedeus commited on
Commit
694d1a3
1 Parent(s): f3e027b

Delete app.py

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