Martin Tomov commited on
Commit
cfccf84
β€’
1 Parent(s): 6dc32ee

sam_vit_h_4b8939.pth

Browse files
Files changed (1) hide show
  1. gsl_utils.py +9 -6
gsl_utils.py CHANGED
@@ -5,17 +5,22 @@ import numpy as np
5
  from PIL import Image, ImageChops, ImageEnhance
6
  import cv2
7
  from simple_lama_inpainting import SimpleLama
 
8
  from transformers import pipeline
9
  from huggingface_hub import hf_hub_download
10
 
11
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
12
 
13
  def load_groundingdino_model(device='cpu'):
14
- model = pipeline(model="IDEA-Research/grounding-dino-base", task="zero-shot-object-detection", device=device)
15
  return model
16
 
 
 
 
 
17
  groundingdino_model = load_groundingdino_model(device=device)
18
- sam_predictor = None # Initialize this properly using build_sam
19
  simple_lama = SimpleLama()
20
 
21
  def detect(image, model, text_prompt='insect . flower . cloud', box_threshold=0.15, text_threshold=0.15):
@@ -24,7 +29,6 @@ def detect(image, model, text_prompt='insect . flower . cloud', box_threshold=0.
24
  return results
25
 
26
  def segment(image, sam_model, boxes):
27
- # sam_moded initialized with build_sam
28
  sam_model.set_image(image)
29
  H, W, _ = image.shape
30
  boxes_xyxy = torch.Tensor(boxes) * torch.Tensor([W, H, W, H])
@@ -59,14 +63,13 @@ def dilate_mask(mask, dilate_factor=15):
59
  return mask
60
 
61
  def gsl_process_image(image):
62
- # image numpy array
63
  if not isinstance(image, np.ndarray):
64
  image = np.array(image)
65
 
66
- # load as a PIL
67
  image_pil = Image.fromarray(image)
68
 
69
- # detect insects
70
  detected_boxes = detect(image_pil, groundingdino_model)
71
  boxes = [[d['box']['xmin'], d['box']['ymin'], d['box']['xmax'], d['box']['ymax']] for d in detected_boxes]
72
  segmented_frame_masks = segment(image, sam_predictor, boxes)
 
5
  from PIL import Image, ImageChops, ImageEnhance
6
  import cv2
7
  from simple_lama_inpainting import SimpleLama
8
+ from segment_anything import build_sam, SamPredictor
9
  from transformers import pipeline
10
  from huggingface_hub import hf_hub_download
11
 
12
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
13
 
14
  def load_groundingdino_model(device='cpu'):
15
+ model = pipeline(model="ShilongLiu/GroundingDINO", task="zero-shot-object-detection", device=device)
16
  return model
17
 
18
+ def load_sam_model(device='cpu'):
19
+ sam_model = build_sam(checkpoint='sam_vit_h_4b8939.pth').to(device)
20
+ return SamPredictor(sam_model)
21
+
22
  groundingdino_model = load_groundingdino_model(device=device)
23
+ sam_predictor = load_sam_model(device=device)
24
  simple_lama = SimpleLama()
25
 
26
  def detect(image, model, text_prompt='insect . flower . cloud', box_threshold=0.15, text_threshold=0.15):
 
29
  return results
30
 
31
  def segment(image, sam_model, boxes):
 
32
  sam_model.set_image(image)
33
  H, W, _ = image.shape
34
  boxes_xyxy = torch.Tensor(boxes) * torch.Tensor([W, H, W, H])
 
63
  return mask
64
 
65
  def gsl_process_image(image):
66
+ # img numpy array
67
  if not isinstance(image, np.ndarray):
68
  image = np.array(image)
69
 
70
+ # load img as a PIL
71
  image_pil = Image.fromarray(image)
72
 
 
73
  detected_boxes = detect(image_pil, groundingdino_model)
74
  boxes = [[d['box']['xmin'], d['box']['ymin'], d['box']['xmax'], d['box']['ymax']] for d in detected_boxes]
75
  segmented_frame_masks = segment(image, sam_predictor, boxes)