kushagra124 commited on
Commit
7c8f933
·
1 Parent(s): 7fb0d5c

adding app with CLIP image segmentation

Browse files
Files changed (1) hide show
  1. app.py +1 -6
app.py CHANGED
@@ -1,5 +1,4 @@
1
  from turtle import title
2
- import os
3
  import gradio as gr
4
  from transformers import pipeline
5
  import numpy as np
@@ -7,7 +6,6 @@ from PIL import Image
7
  import torch
8
  import cv2
9
  from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation,AutoProcessor,AutoConfig
10
- from skimage.measure import label, regionprops
11
 
12
  processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
13
  model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")
@@ -21,7 +19,6 @@ def create_rgb_mask(mask):
21
 
22
 
23
  def detect_using_clip(image,prompts=[],threshould=0.4):
24
- h,w = image.shape[:2]
25
  predicted_masks = list()
26
  inputs = processor(
27
  text=prompts,
@@ -36,14 +33,13 @@ def detect_using_clip(image,prompts=[],threshould=0.4):
36
  for i,prompt in enumerate(prompts):
37
  predicted_image = torch.sigmoid(preds[i][0]).detach().cpu().numpy()
38
  predicted_image = np.where(predicted_image>threshould,255,0)
39
-
40
  predicted_masks.append(create_rgb_mask(predicted_image))
 
41
  return predicted_masks
42
 
43
  def visualize_images(image,predicted_images):
44
  alpha = 0.7
45
  # H,W = image.shape[:2]
46
- prompt = prompt.lower()
47
  image_resize = cv2.resize(image,(352,352))
48
  resize_image_copy = image_resize.copy()
49
 
@@ -59,7 +55,6 @@ def shot(image, labels_text):
59
  else:
60
  prompts = [labels_text]
61
  prompts = list(map(lambda x: x.strip(),prompts))
62
-
63
  predicted_images = detect_using_clip(image,prompts=prompts)
64
 
65
  category_image = visualize_images(image=image,predicted_images=predicted_images)
 
1
  from turtle import title
 
2
  import gradio as gr
3
  from transformers import pipeline
4
  import numpy as np
 
6
  import torch
7
  import cv2
8
  from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation,AutoProcessor,AutoConfig
 
9
 
10
  processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
11
  model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")
 
19
 
20
 
21
  def detect_using_clip(image,prompts=[],threshould=0.4):
 
22
  predicted_masks = list()
23
  inputs = processor(
24
  text=prompts,
 
33
  for i,prompt in enumerate(prompts):
34
  predicted_image = torch.sigmoid(preds[i][0]).detach().cpu().numpy()
35
  predicted_image = np.where(predicted_image>threshould,255,0)
 
36
  predicted_masks.append(create_rgb_mask(predicted_image))
37
+
38
  return predicted_masks
39
 
40
  def visualize_images(image,predicted_images):
41
  alpha = 0.7
42
  # H,W = image.shape[:2]
 
43
  image_resize = cv2.resize(image,(352,352))
44
  resize_image_copy = image_resize.copy()
45
 
 
55
  else:
56
  prompts = [labels_text]
57
  prompts = list(map(lambda x: x.strip(),prompts))
 
58
  predicted_images = detect_using_clip(image,prompts=prompts)
59
 
60
  category_image = visualize_images(image=image,predicted_images=predicted_images)