Vijish commited on
Commit
bf5d203
1 Parent(s): 21e9f6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -6
app.py CHANGED
@@ -14,6 +14,7 @@ import numpy as np
14
  import torchvision.transforms as T
15
  from PIL import Image,ImageOps,ImageFilter
16
  from io import BytesIO
 
17
 
18
 
19
 
@@ -71,11 +72,9 @@ urllib.request.urlretrieve(MODEL_URL, "popd.pkl")
71
  path = Path(".")
72
  learn=load_learner(path, 'popd.pkl')
73
 
74
-
75
- uploaded_file = st.file_uploader("Choose an image...")
76
- if uploaded_file is not None:
77
- img_fast = open_image(uploaded_file)
78
- a = PIL.Image.open(uploaded_file).convert('RGB')
79
  p,img_hr,b = learn.predict(img_fast)
80
  x = np.minimum(np.maximum(image2np(img_hr.data*255), 0), 255).astype(np.uint8)
81
  img = PIL.Image.fromarray(x).convert('RGB')
@@ -113,4 +112,34 @@ if uploaded_file is not None:
113
  for ne in neigh:
114
  queue.append(ne)
115
 
116
- st.image(img, caption='PoP ArT')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  import torchvision.transforms as T
15
  from PIL import Image,ImageOps,ImageFilter
16
  from io import BytesIO
17
+ import os
18
 
19
 
20
 
 
72
  path = Path(".")
73
  learn=load_learner(path, 'popd.pkl')
74
 
75
+ def predict(image):
76
+ img_fast = open_image(image)
77
+ a = PIL.Image.open(image).convert('RGB')
 
 
78
  p,img_hr,b = learn.predict(img_fast)
79
  x = np.minimum(np.maximum(image2np(img_hr.data*255), 0), 255).astype(np.uint8)
80
  img = PIL.Image.fromarray(x).convert('RGB')
 
112
  for ne in neigh:
113
  queue.append(ne)
114
 
115
+ return st.image(img, caption='PoP ArT')
116
+
117
+ SIDEBAR_OPTION_DEMO_IMAGE = "Select a Demo Image"
118
+ SIDEBAR_OPTION_UPLOAD_IMAGE = "Upload an Image"
119
+
120
+ SIDEBAR_OPTIONS = [SIDEBAR_OPTION_DEMO_IMAGE, SIDEBAR_OPTION_UPLOAD_IMAGE]
121
+
122
+
123
+ app_mode = st.sidebar.selectbox("Please select from the following", SIDEBAR_OPTIONS)
124
+
125
+ if app_mode == SIDEBAR_OPTION_DEMO_IMAGE:
126
+ st.sidebar.write(" ------ ")
127
+ directory = os.path.join(Images)
128
+ photos = []
129
+ for file in os.listdir(directory):
130
+ filepath = os.path.join(directory, file)
131
+ if imghdr.what(filepath) is not None:
132
+ photos.append(file)
133
+ photos.sort()
134
+ option = st.sidebar.selectbox('Please select a sample image, then click Magic Time button', photos)
135
+ pressed = st.sidebar.button('PoP')
136
+ if pressed:
137
+ st.empty()
138
+ t.sidebar.write('Please wait for the magic to happen! This may take up to a minute.')
139
+ pic = os.path.join(directory, option)
140
+ predict(pic)
141
+
142
+ elif app_mode == SIDEBAR_OPTION_UPLOAD_IMAGE:
143
+ uploaded_file = st.file_uploader("Choose an image...")
144
+ if uploaded_file is not None:
145
+ predict(uploaded_file)