breezedeus commited on
Commit
1710958
1 Parent(s): cbbc0b4
Files changed (1) hide show
  1. app.py +41 -35
app.py CHANGED
@@ -36,17 +36,14 @@ 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
 
@@ -108,9 +105,13 @@ def main():
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技术识别出有意义的文字。'
@@ -121,40 +122,45 @@ def main():
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
- logger.info('\ngenerating an image for texts:\n %s', texts)
139
- out_img = anti(
140
- texts,
141
- char_reverse_ratio=char_reverse_ratio,
142
- char_to_pinyin_ratio=char_to_pinyin_ratio,
143
- text_color=text_color,
144
- min_font_size=min_font_size,
145
- max_font_size=max_font_size,
146
- bg_image=bg_image,
147
- bg_gen_config=bg_gen_config,
148
- font_fp=font_fp,
149
- )
150
- st.subheader('输出图片')
151
- st.image(out_img)
152
- save_image(out_img)
153
-
154
- st.markdown('**对输出图片进行OCR,结果如下(如果依旧出现敏感词,尝试重新生成图片):**')
155
- ocr_out = ocr.ocr(out_img)
156
- new_texts = [out['text'] for out in ocr_out]
157
- st.text('\n'.join(new_texts))
 
 
 
158
 
159
 
160
  if __name__ == '__main__':
 
36
  return CnOcr()
37
 
38
 
39
+ def download_image_button(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="下载图片", data=byte_im, file_name="antiOCR.jpeg", mime="image/jpeg",
 
 
 
47
  )
48
 
49
 
 
105
 
106
  title = '让文字自由传播:<a href="https://github.com/breezedeus/antiOCR">antiOCR</a>'
107
  st.markdown(f"<h1 style='text-align: center;'>{title}</h1>", unsafe_allow_html=True)
108
+ subtitle = (
109
+ '作者:<a href="https://github.com/breezedeus">breezedeus</a>; '
110
+ '欢迎加入 <a href="https://cnocr.readthedocs.io/zh/latest/contact/">交流群</a>'
111
+ )
112
+ st.markdown(
113
+ f"<div style='text-align: center;'>{subtitle}</div>", unsafe_allow_html=True
114
+ )
115
  st.markdown('')
116
  st.markdown('')
117
  desc = '<strong>antiOCR</strong> 对指定的文字(来自输入或者图片)进行处理,输出图片,此图片无法通过OCR技术识别出有意义的文字。'
 
122
  content_file = st.file_uploader('输入待转换的文字图片:', type=["png", "jpg", "jpeg", "webp"])
123
  ocr = get_ocr_model()
124
  anti = AntiOcr()
125
+ ocr_texts = None
126
  if content_file is not None:
127
  try:
128
  img = Image.open(content_file).convert('RGB')
129
  ocr_out = ocr.ocr(img)
130
+ ocr_texts = '\n'.join([out['text'] for out in ocr_out])
131
  except Exception as e:
132
  st.error(e)
133
+ if not ocr_texts:
134
+ st.warning(f'抱歉,图片中未识别出任何文字。')
135
 
136
+ texts = st.text_area(
137
+ '或者,直接输入待转换的文字:', value=ocr_texts or '', height=120, placeholder=default_texts
138
+ )
139
+ texts = texts or default_texts
140
+
141
+ enter = st.button("生成图片")
142
+ if (content_file is not None and ocr_texts) or enter:
143
+ with st.spinner('图片生成中…'):
144
+ logger.info('\ngenerating an image for texts:\n %s', texts)
145
+ out_img = anti(
146
+ texts,
147
+ char_reverse_ratio=char_reverse_ratio,
148
+ char_to_pinyin_ratio=char_to_pinyin_ratio,
149
+ text_color=text_color,
150
+ min_font_size=min_font_size,
151
+ max_font_size=max_font_size,
152
+ bg_image=bg_image,
153
+ bg_gen_config=bg_gen_config,
154
+ font_fp=font_fp,
155
+ )
156
+ st.subheader('输出图片')
157
+ st.image(out_img)
158
+ download_image_button(out_img)
159
+
160
+ st.markdown('**对输出图片进行OCR,结果如下(如果依旧出现敏感词,尝试重新生成图片):**')
161
+ ocr_out = ocr.ocr(out_img)
162
+ new_texts = [out['text'] for out in ocr_out]
163
+ st.text('\n'.join(new_texts))
164
 
165
 
166
  if __name__ == '__main__':