nightfury commited on
Commit
2e65625
1 Parent(s): c8f50a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
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
- gray_image = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
126
- (thresh, bw_image) = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY)
127
- cv2.cvtColor(bw_image, cv2.COLOR_BGR2RGB)
128
- mask = Image.fromarray(np.uint8(bw_image)).convert('RGB')
129
- os.remove(filename)
 
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
- gray_image = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
152
- (thresh, bw_image) = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY)
153
- cv2.cvtColor(bw_image, cv2.COLOR_BGR2RGB)
154
- mask = Image.fromarray(np.uint8(bw_image)).convert('RGB')
155
- os.remove(filename)
156
- with autocast(device): #"cuda"
 
 
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