sigyllly commited on
Commit
b69abfd
1 Parent(s): 00c41dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -19
app.py CHANGED
@@ -24,34 +24,25 @@ def process_image(image, prompt, threshold, alpha_value, draw_rectangles):
24
  preds = outputs.logits
25
 
26
  pred = torch.sigmoid(preds)
27
-
28
- if len(pred.shape) == 4: # Check if the shape is (batch_size, channels, height, width)
29
- mat = pred[0, 0].cpu().numpy() # Access the first channel of the first batch
30
- else:
31
- mat = pred[0].cpu().numpy() # If the shape is (channels, height, width)
32
-
33
- mask = Image.fromarray(np.uint8(mat * 255), "L") # Convert to PIL Image
34
-
35
- # Convert the mask to a NumPy array for calculation
36
- mask_array = np.array(mask)
37
 
38
  # normalize the mask
39
- mask_min = mask_array.min()
40
- mask_max = mask_array.max()
41
- mask_array = (mask_array - mask_min) / (mask_max - mask_min)
42
 
43
  # threshold the mask
44
- bmask = mask_array > threshold
45
  # zero out values below the threshold
46
- mask_array[mask_array < threshold] = 0
47
 
48
- bmask = Image.fromarray((bmask * 255).astype(np.uint8), "L")
49
 
50
  return bmask
51
 
52
-
53
-
54
-
55
  @app.route('/')
56
  def index():
57
  return "Hello, World! clipseg2"
 
24
  preds = outputs.logits
25
 
26
  pred = torch.sigmoid(preds)
27
+ mat = pred.cpu().numpy()
28
+ mask = Image.fromarray(np.uint8(mat * 255), "L")
29
+ mask = mask.resize(image.size)
30
+ mask = np.array(mask)[:, :, 0]
 
 
 
 
 
 
31
 
32
  # normalize the mask
33
+ mask_min = mask.min()
34
+ mask_max = mask.max()
35
+ mask = (mask - mask_min) / (mask_max - mask_min)
36
 
37
  # threshold the mask
38
+ bmask = mask > threshold
39
  # zero out values below the threshold
40
+ mask[mask < threshold] = 0
41
 
42
+ bmask = Image.fromarray(bmask.astype(np.uint8) * 255, "L")
43
 
44
  return bmask
45
 
 
 
 
46
  @app.route('/')
47
  def index():
48
  return "Hello, World! clipseg2"