-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
74 lines (55 loc) · 2.08 KB
/
app.py
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
import os
import numpy as np
import streamlit as st
import tensorflow as tf
from PIL import Image
try:
import pytesseract
import pytesseract.TesseractNotFoundError as TesseractNotFoundError
tesseract_available = True
except ImportError:
tesseract_available = False
import docclean
from docclean.utils import ImageMosaic
st.write("# Docclean")
uploaded_file = st.file_uploader("Upload an image file")
if uploaded_file:
input_file = Image.open(uploaded_file).convert("RGB")
input_file = np.asarray(input_file)
st.write("### Uploaded Image:")
st.image(input_file)
st.write("### Choose a Model:")
model = st.selectbox("", ("Vanilla", "CycleGAN"))
if model == "CycleGAN":
from tensorflow_examples.models.pix2pix import pix2pix
cg_url = "https://docclean.s3.us-east-2.amazonaws.com/cg_weights.tar.gz"
model = pix2pix.unet_generator(3, norm_type="instancenorm")
tf.keras.utils.get_file("cg_weights", cg_url, untar=True)
model.load_weights(os.path.expanduser("~/.keras/datasets/weights/cg"))
else:
ae_url = "https://docclean.s3.us-east-2.amazonaws.com/ae_weights.tar.gz"
model = docclean.autoencoder.Autoencoder().autoencoder_model
tf.keras.utils.get_file("ae_weights", ae_url, untar=True)
model.load_weights(os.path.expanduser("~/.keras/datasets/weights/ae"))
im = ImageMosaic(input_file)
batches = im.make_patches()
if model == "CycleGAN":
batches = 2 * batches - 1
out = model.predict(batches)
if model == "CycleGAN":
out = (out + 1) / 2
output_image = im.combine_patches(out)
output_image = (output_image - output_image.min()) / (
output_image.max() - output_image.min()
)
st.image(output_image)
output_image *= 255
if tesseract_available:
try:
string = pytesseract.image_to_string(
Image.fromarray(output_image.astype("uint8"), "RGB")
)
st.write("### Output from Tessaract:")
st.write(string)
except TesseractNotFoundError:
pass