Spaces:
Sleeping
Sleeping
Vivien
commited on
Commit
·
b57c4d6
1
Parent(s):
ac49d38
Recover previous version
Browse files
app.py
CHANGED
@@ -1,243 +1,238 @@
|
|
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 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
#
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
# st.image(captioned)
|
240 |
-
|
241 |
-
|
242 |
-
# if __name__ == "__main__":
|
243 |
-
# main()
|
|
|
1 |
+
import numpy as np
|
2 |
+
from PIL import ImageDraw, Image, ImageFont
|
3 |
+
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
4 |
+
import torch
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
FONTS = [
|
8 |
+
"Font: Serif - EBGaramond",
|
9 |
+
"Font: Serif - Cinzel",
|
10 |
+
"Font: Sans - Roboto",
|
11 |
+
"Font: Sans - Lato",
|
12 |
+
"Font: Display - Lobster",
|
13 |
+
"Font: Display - LilitaOne",
|
14 |
+
"Font: Handwriting - GreatVibes",
|
15 |
+
"Font: Handwriting - Pacifico",
|
16 |
+
"Font: Mono - Inconsolata",
|
17 |
+
"Font: Mono - Cutive",
|
18 |
+
]
|
19 |
+
|
20 |
+
|
21 |
+
def hex_to_rgb(hex):
|
22 |
+
rgb = []
|
23 |
+
for i in (0, 2, 4):
|
24 |
+
decimal = int(hex[i : i + 2], 16)
|
25 |
+
rgb.append(decimal)
|
26 |
+
return tuple(rgb)
|
27 |
+
|
28 |
+
|
29 |
+
@st.cache(allow_output_mutation=True)
|
30 |
+
def load():
|
31 |
+
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
32 |
+
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
33 |
+
return model, feature_extractor
|
34 |
+
|
35 |
+
|
36 |
+
model, feature_extractor = load()
|
37 |
+
|
38 |
+
|
39 |
+
def compute_depth(image):
|
40 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
41 |
+
with torch.no_grad():
|
42 |
+
outputs = model(**inputs)
|
43 |
+
predicted_depth = outputs.predicted_depth
|
44 |
+
prediction = torch.nn.functional.interpolate(
|
45 |
+
predicted_depth.unsqueeze(1),
|
46 |
+
size=image.size[::-1],
|
47 |
+
mode="bicubic",
|
48 |
+
align_corners=False,
|
49 |
+
)
|
50 |
+
return prediction.cpu().numpy()[0, 0, :, :]
|
51 |
+
|
52 |
+
|
53 |
+
def get_mask1(
|
54 |
+
shape, x, y, caption, font=None, font_size=0.08, color=(0, 0, 0), alpha=0.8
|
55 |
+
):
|
56 |
+
img_text = Image.new("RGBA", (shape[1], shape[0]), (0, 0, 0, 0))
|
57 |
+
draw = ImageDraw.Draw(img_text)
|
58 |
+
font = ImageFont.truetype(font, int(font_size * shape[1]))
|
59 |
+
draw.text(
|
60 |
+
(x * shape[1], (1 - y) * shape[0]),
|
61 |
+
caption,
|
62 |
+
fill=(*color, int(max(min(1, alpha), 0) * 255)),
|
63 |
+
font=font,
|
64 |
+
)
|
65 |
+
text = np.array(img_text)
|
66 |
+
mask1 = np.dot(np.expand_dims(text[:, :, -1] / 255, -1), np.ones((1, 3)))
|
67 |
+
return text[:, :, :-1], mask1
|
68 |
+
|
69 |
+
|
70 |
+
def get_mask2(depth_map, depth):
|
71 |
+
return np.expand_dims(
|
72 |
+
(depth_map[:, :] < depth * np.min(depth_map) + (1 - depth) * np.max(depth_map)),
|
73 |
+
-1,
|
74 |
+
)
|
75 |
+
|
76 |
+
|
77 |
+
def add_caption(
|
78 |
+
img,
|
79 |
+
caption,
|
80 |
+
depth_map=None,
|
81 |
+
x=0.5,
|
82 |
+
y=0.5,
|
83 |
+
depth=0.5,
|
84 |
+
font_size=50,
|
85 |
+
color=(255, 255, 255),
|
86 |
+
font="",
|
87 |
+
alpha=1,
|
88 |
+
):
|
89 |
+
text, mask1 = get_mask1(
|
90 |
+
img.shape,
|
91 |
+
x,
|
92 |
+
y,
|
93 |
+
caption,
|
94 |
+
font=font,
|
95 |
+
font_size=font_size,
|
96 |
+
color=color,
|
97 |
+
alpha=alpha,
|
98 |
+
)
|
99 |
+
mask2 = get_mask2(depth_map, depth)
|
100 |
+
mask = mask1 * np.dot(mask2, np.ones((1, 3)))
|
101 |
+
|
102 |
+
return ((1 - mask) * img + mask * text).astype(np.uint8)
|
103 |
+
|
104 |
+
|
105 |
+
@st.cache(max_entries=30, show_spinner=False)
|
106 |
+
def load_img(uploaded_file):
|
107 |
+
if uploaded_file is None:
|
108 |
+
img = Image.open("pulp.jpg")
|
109 |
+
default = True
|
110 |
+
else:
|
111 |
+
img = Image.open(uploaded_file)
|
112 |
+
if img.size[0] > 800 or img.size[1] > 800:
|
113 |
+
if img.size[0] < img.size[1]:
|
114 |
+
new_size = (int(800 * img.size[0] / img.size[1]), 800)
|
115 |
+
else:
|
116 |
+
new_size = (800, int(800 * img.size[1] / img.size[0]))
|
117 |
+
img = img.resize(new_size)
|
118 |
+
default = False
|
119 |
+
return np.array(img), compute_depth(img), default
|
120 |
+
|
121 |
+
|
122 |
+
def main():
|
123 |
+
st.markdown(
|
124 |
+
"""
|
125 |
+
<style>
|
126 |
+
label{
|
127 |
+
height: 0px !important;
|
128 |
+
min-height: 0px !important;
|
129 |
+
margin-bottom: 0px !important;
|
130 |
+
}
|
131 |
+
</style>
|
132 |
+
""",
|
133 |
+
unsafe_allow_html=True,
|
134 |
+
)
|
135 |
+
|
136 |
+
st.sidebar.markdown(
|
137 |
+
"""
|
138 |
+
# Depth-aware text addition
|
139 |
+
|
140 |
+
Add text ***inside*** an image!
|
141 |
+
|
142 |
+
Upload an image, enter some text and adjust the ***depth*** where you want the text to be displayed. You can also define its location and appearance (font, color, transparency and size).
|
143 |
+
|
144 |
+
Built with [PyTorch](https://pytorch.org/), Intel's [MiDaS model](https://pytorch.org/hub/intelisl_midas_v2/), [Streamlit](https://streamlit.io/), [pillow](https://python-pillow.org/) and inspired by the official [video](https://youtu.be/eTa1jHk1Lxc) of *Jenny of Oldstones* by Florence + the Machine
|
145 |
+
"""
|
146 |
+
)
|
147 |
+
|
148 |
+
uploaded_file = st.file_uploader("", type=["jpg", "jpeg"])
|
149 |
+
with st.spinner("Analyzing the image - Please wait a few seconds"):
|
150 |
+
img, depth_map, default = load_img(uploaded_file)
|
151 |
+
|
152 |
+
if default:
|
153 |
+
x0, y0, alpha0, font_size0, depth0, font0 = 0.02, 0.68, 0.99, 0.07, 0.12, 4
|
154 |
+
text0 = "Pulp Fiction"
|
155 |
+
else:
|
156 |
+
x0, y0, alpha0, font_size0, depth0, font0 = 0.1, 0.9, 0.8, 0.08, 0.5, 0
|
157 |
+
text0 = "Enter your text here"
|
158 |
+
|
159 |
+
colA, colB, colC = st.columns((13, 1, 1))
|
160 |
+
|
161 |
+
with colA:
|
162 |
+
text = st.text_input("", text0)
|
163 |
+
|
164 |
+
with colB:
|
165 |
+
st.markdown("Color:")
|
166 |
+
|
167 |
+
with colC:
|
168 |
+
color = st.color_picker("", value="#FFFFFF")
|
169 |
+
|
170 |
+
col1, _, col2 = st.columns((4, 1, 4))
|
171 |
+
|
172 |
+
with col1:
|
173 |
+
depth = st.select_slider(
|
174 |
+
"",
|
175 |
+
options=[i / 100 for i in range(101)],
|
176 |
+
value=depth0,
|
177 |
+
format_func=lambda x: "Foreground"
|
178 |
+
if x == 0.0
|
179 |
+
else "Background"
|
180 |
+
if x == 1.0
|
181 |
+
else "",
|
182 |
+
)
|
183 |
+
x = st.select_slider(
|
184 |
+
"",
|
185 |
+
options=[i / 100 for i in range(101)],
|
186 |
+
value=x0,
|
187 |
+
format_func=lambda x: "Left" if x == 0.0 else "Right" if x == 1.0 else "",
|
188 |
+
)
|
189 |
+
y = st.select_slider(
|
190 |
+
"",
|
191 |
+
options=[i / 100 for i in range(101)],
|
192 |
+
value=y0,
|
193 |
+
format_func=lambda x: "Bottom" if x == 0.0 else "Top" if x == 1.0 else "",
|
194 |
+
)
|
195 |
+
|
196 |
+
with col2:
|
197 |
+
font_size = st.select_slider(
|
198 |
+
"",
|
199 |
+
options=[0.04 + i / 100 for i in range(0, 17)],
|
200 |
+
value=font_size0,
|
201 |
+
format_func=lambda x: "Small font"
|
202 |
+
if x == 0.04
|
203 |
+
else "Large font"
|
204 |
+
if x == 0.2
|
205 |
+
else "",
|
206 |
+
)
|
207 |
+
alpha = st.select_slider(
|
208 |
+
"",
|
209 |
+
options=[i / 100 for i in range(101)],
|
210 |
+
value=alpha0,
|
211 |
+
format_func=lambda x: "Transparent"
|
212 |
+
if x == 0.0
|
213 |
+
else "Opaque"
|
214 |
+
if x == 1.0
|
215 |
+
else "",
|
216 |
+
)
|
217 |
+
font = st.selectbox("", FONTS, index=font0)
|
218 |
+
|
219 |
+
font = f"fonts/{font[6:]}.ttf"
|
220 |
+
|
221 |
+
captioned = add_caption(
|
222 |
+
img,
|
223 |
+
text,
|
224 |
+
x=x,
|
225 |
+
y=y,
|
226 |
+
depth=depth,
|
227 |
+
depth_map=depth_map,
|
228 |
+
font=font,
|
229 |
+
font_size=font_size,
|
230 |
+
alpha=alpha,
|
231 |
+
color=hex_to_rgb(color[1:]),
|
232 |
+
)
|
233 |
+
|
234 |
+
st.image(captioned)
|
235 |
+
|
236 |
+
|
237 |
+
if __name__ == "__main__":
|
238 |
+
main()
|
|
|
|
|
|
|
|
|
|