Spaces:
Sleeping
Sleeping
Vivien
commited on
Commit
·
ac49d38
1
Parent(s):
fd7f424
Test to clear cache
Browse files
app.py
CHANGED
|
@@ -1,238 +1,243 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|