Spaces:
Running
Running
yizhangliu
commited on
Commit
•
98f817f
1
Parent(s):
843d0b7
Update app.py
Browse files
app.py
CHANGED
@@ -63,10 +63,6 @@ os.environ["NUMEXPR_NUM_THREADS"] = NUM_THREADS
|
|
63 |
if os.environ.get("CACHE_DIR"):
|
64 |
os.environ["TORCH_HOME"] = os.environ["CACHE_DIR"]
|
65 |
|
66 |
-
os.environ["TORCH_HOME"] = './'
|
67 |
-
|
68 |
-
BUILD_DIR = os.environ.get("LAMA_CLEANER_BUILD_DIR", "app/build")
|
69 |
-
|
70 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
71 |
|
72 |
HF_TOKEN_SD = os.environ.get('HF_TOKEN_SD')
|
@@ -74,60 +70,12 @@ HF_TOKEN_SD = os.environ.get('HF_TOKEN_SD')
|
|
74 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
75 |
print(f'device = {device}')
|
76 |
|
77 |
-
def get_image_ext(img_bytes):
|
78 |
-
w = imghdr.what("", img_bytes)
|
79 |
-
if w is None:
|
80 |
-
w = "jpeg"
|
81 |
-
return w
|
82 |
-
|
83 |
-
def diffuser_callback(i, t, latents):
|
84 |
-
pass
|
85 |
-
|
86 |
-
def preprocess_image(image):
|
87 |
-
w, h = image.size
|
88 |
-
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
|
89 |
-
image = image.resize((w, h), resample=PIL.Image.LANCZOS)
|
90 |
-
image = np.array(image).astype(np.float32) / 255.0
|
91 |
-
image = image[None].transpose(0, 3, 1, 2)
|
92 |
-
image = torch.from_numpy(image)
|
93 |
-
return 2.0 * image - 1.0
|
94 |
-
|
95 |
-
def preprocess_mask(mask):
|
96 |
-
mask = mask.convert("L")
|
97 |
-
w, h = mask.size
|
98 |
-
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
|
99 |
-
mask = mask.resize((w // 8, h // 8), resample=PIL.Image.NEAREST)
|
100 |
-
mask = np.array(mask).astype(np.float32) / 255.0
|
101 |
-
mask = np.tile(mask, (4, 1, 1))
|
102 |
-
mask = mask[None].transpose(0, 1, 2, 3) # what does this step do?
|
103 |
-
mask = 1 - mask # repaint white, keep black
|
104 |
-
mask = torch.from_numpy(mask)
|
105 |
-
return mask
|
106 |
-
|
107 |
-
def load_img_1_(nparr, gray: bool = False):
|
108 |
-
# alpha_channel = None
|
109 |
-
# nparr = np.frombuffer(img_bytes, np.uint8)
|
110 |
-
if gray:
|
111 |
-
np_img = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
|
112 |
-
else:
|
113 |
-
np_img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
|
114 |
-
if len(np_img.shape) == 3 and np_img.shape[2] == 4:
|
115 |
-
alpha_channel = np_img[:, :, -1]
|
116 |
-
np_img = cv2.cvtColor(np_img, cv2.COLOR_BGRA2RGB)
|
117 |
-
else:
|
118 |
-
np_img = cv2.cvtColor(np_img, cv2.COLOR_BGR2RGB)
|
119 |
-
|
120 |
-
return np_img, alpha_channel
|
121 |
-
|
122 |
model = None
|
123 |
|
124 |
def model_process(image, mask, alpha_channel, ext):
|
125 |
global model
|
126 |
original_shape = image.shape
|
127 |
interpolation = cv2.INTER_CUBIC
|
128 |
-
|
129 |
-
# image_pil = Image.fromarray(image)
|
130 |
-
# mask_pil = Image.fromarray(mask).convert("L")
|
131 |
|
132 |
size_limit = "Original"
|
133 |
print(f'size_limit_2_ = {size_limit}')
|
@@ -269,15 +217,8 @@ def predict(input):
|
|
269 |
ext = 'png'
|
270 |
|
271 |
output = model_process(image, mask, alpha_channel, ext)
|
272 |
-
|
273 |
-
# output = mask #output.images[0]
|
274 |
-
# output = pipe(prompt = prompt, image=init_image, mask_image=mask,guidance_scale=7.5)
|
275 |
-
# output = input["mask"]
|
276 |
-
# output = None
|
277 |
return output #, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
278 |
|
279 |
-
print(f'liuyz_500_here_')
|
280 |
-
|
281 |
css = '''
|
282 |
.container {max-width: 1150px;margin: auto;padding-top: 1.5rem}
|
283 |
#image_upload{min-height:512px}
|
@@ -375,7 +316,7 @@ with image_blocks as demo:
|
|
375 |
full_width=True,
|
376 |
)
|
377 |
|
378 |
-
with gr.Column():
|
379 |
image_out = gr.Image(label="Output").style(height=512)
|
380 |
'''
|
381 |
with gr.Group(elem_id="share-btn-container"):
|
|
|
63 |
if os.environ.get("CACHE_DIR"):
|
64 |
os.environ["TORCH_HOME"] = os.environ["CACHE_DIR"]
|
65 |
|
|
|
|
|
|
|
|
|
66 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
67 |
|
68 |
HF_TOKEN_SD = os.environ.get('HF_TOKEN_SD')
|
|
|
70 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
71 |
print(f'device = {device}')
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
model = None
|
74 |
|
75 |
def model_process(image, mask, alpha_channel, ext):
|
76 |
global model
|
77 |
original_shape = image.shape
|
78 |
interpolation = cv2.INTER_CUBIC
|
|
|
|
|
|
|
79 |
|
80 |
size_limit = "Original"
|
81 |
print(f'size_limit_2_ = {size_limit}')
|
|
|
217 |
ext = 'png'
|
218 |
|
219 |
output = model_process(image, mask, alpha_channel, ext)
|
|
|
|
|
|
|
|
|
|
|
220 |
return output #, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
221 |
|
|
|
|
|
222 |
css = '''
|
223 |
.container {max-width: 1150px;margin: auto;padding-top: 1.5rem}
|
224 |
#image_upload{min-height:512px}
|
|
|
316 |
full_width=True,
|
317 |
)
|
318 |
|
319 |
+
# with gr.Column():
|
320 |
image_out = gr.Image(label="Output").style(height=512)
|
321 |
'''
|
322 |
with gr.Group(elem_id="share-btn-container"):
|