Vivien commited on
Commit
b57c4d6
·
1 Parent(s): ac49d38

Recover previous version

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