Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -121,12 +121,13 @@ def predict(radio, dict, word_mask, prompt=""):
|
|
121 |
init_image = dict['image'].convert('RGB').resize((imgRes, imgRes))
|
122 |
filename = f"{uuid.uuid4()}.png"
|
123 |
plt.imsave(filename,torch.sigmoid(preds[0][0]))
|
124 |
-
img2 = cv2.imread(filename)
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
130 |
else:
|
131 |
img = transform(dict["image"]).unsqueeze(0)
|
132 |
|
@@ -135,25 +136,30 @@ def predict(radio, dict, word_mask, prompt=""):
|
|
135 |
img.cuda()
|
136 |
print ("yes, CUDA is available here !! ")
|
137 |
|
138 |
-
model = model.to(torch.device(device))
|
139 |
-
img = img.to(torch.device(device))
|
140 |
-
prompt = labels.to(torch.device(device))
|
141 |
#------------------
|
142 |
|
143 |
word_masks = [word_mask]
|
144 |
#with torch.cuda.amp.autocast(): #
|
145 |
with torch.no_grad():
|
146 |
preds = model(img.repeat(len(word_masks),1,1,1), word_masks)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
init_image = dict['image'].convert('RGB').resize((imgRes, imgRes))
|
148 |
filename = f"{uuid.uuid4()}.png"
|
149 |
plt.imsave(filename,torch.sigmoid(preds[0][0]))
|
150 |
-
img2 = cv2.imread(filename)
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
|
|
157 |
images = pipe(prompt = prompt, init_image=init_image, mask_image=mask, strength=0.8)["sample"]
|
158 |
return images[0]
|
159 |
|
|
|
121 |
init_image = dict['image'].convert('RGB').resize((imgRes, imgRes))
|
122 |
filename = f"{uuid.uuid4()}.png"
|
123 |
plt.imsave(filename,torch.sigmoid(preds[0][0]))
|
124 |
+
ret,img2 = cv2.imread(filename)
|
125 |
+
if ret == True:
|
126 |
+
gray_image = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
|
127 |
+
(thresh, bw_image) = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY)
|
128 |
+
cv2.cvtColor(bw_image, cv2.COLOR_BGR2RGB)
|
129 |
+
mask = Image.fromarray(np.uint8(bw_image)).convert('RGB')
|
130 |
+
os.remove(filename)
|
131 |
else:
|
132 |
img = transform(dict["image"]).unsqueeze(0)
|
133 |
|
|
|
136 |
img.cuda()
|
137 |
print ("yes, CUDA is available here !! ")
|
138 |
|
|
|
|
|
|
|
139 |
#------------------
|
140 |
|
141 |
word_masks = [word_mask]
|
142 |
#with torch.cuda.amp.autocast(): #
|
143 |
with torch.no_grad():
|
144 |
preds = model(img.repeat(len(word_masks),1,1,1), word_masks)[0]
|
145 |
+
|
146 |
+
|
147 |
+
model = model.to(torch.device(device))
|
148 |
+
img = img.to(torch.device(device))
|
149 |
+
prompt = labels.to(torch.device(device))
|
150 |
+
|
151 |
init_image = dict['image'].convert('RGB').resize((imgRes, imgRes))
|
152 |
filename = f"{uuid.uuid4()}.png"
|
153 |
plt.imsave(filename,torch.sigmoid(preds[0][0]))
|
154 |
+
ret,img2 = cv2.imread(filename)
|
155 |
+
|
156 |
+
if ret == True:
|
157 |
+
gray_image = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
|
158 |
+
(thresh, bw_image) = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY)
|
159 |
+
cv2.cvtColor(bw_image, cv2.COLOR_BGR2RGB)
|
160 |
+
mask = Image.fromarray(np.uint8(bw_image)).convert('RGB')
|
161 |
+
os.remove(filename)
|
162 |
+
with autocast(device): #"cuda"
|
163 |
images = pipe(prompt = prompt, init_image=init_image, mask_image=mask, strength=0.8)["sample"]
|
164 |
return images[0]
|
165 |
|