ChristopherMarais commited on
Commit
8ef1088
·
1 Parent(s): 56c09ce

Update app.py

Browse files

add old segmentation

Files changed (1) hide show
  1. app.py +60 -54
app.py CHANGED
@@ -11,6 +11,8 @@ from torchvision.ops import box_convert
11
  from torchvision.transforms.functional import to_tensor
12
  from torchvision.transforms import GaussianBlur
13
 
 
 
14
 
15
  # Define a custom transform for Gaussian blur
16
  def gaussian_blur(x, p=0.5, kernel_size_min=3, kernel_size_max=20, sigma_min=0.1, sigma_max=3):
@@ -55,62 +57,62 @@ def unkown_prob_calc(probs, wedge_threshold, wedge_magnitude=1, wedge='strict'):
55
  unknown_prob = 1-kown_prob
56
  return(unknown_prob)
57
 
58
- def load_image(image_source):
59
- transform = T.Compose(
60
- [
61
- T.RandomResize([800], max_size=1333),
62
- T.ToTensor(),
63
- T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
64
- ]
65
- )
66
- image_source = image_source.convert("RGB")
67
 
68
- image_transformed, _ = transform(image_source, None)
69
- return image_transformed
70
-
71
- # load object detection model
72
- od_model = load_model(
73
- model_checkpoint_path="groundingdino_swint_ogc.pth",
74
- model_config_path="GroundingDINO_SwinT_OGC.cfg.py",
75
- device="cpu")
76
- print("Object detection model loaded")
77
-
78
- def detect_objects(og_image, model=od_model, prompt="bug . insect", device="cpu"):
79
- TEXT_PROMPT = prompt
80
- BOX_TRESHOLD = 0.35
81
- TEXT_TRESHOLD = 0.25
82
- DEVICE = device # cuda or cpu
83
 
84
- # Convert numpy array to PIL Image if needed
85
- if isinstance(og_image, np.ndarray):
86
- og_image_obj = Image.fromarray(og_image)
87
- else:
88
- og_image_obj = og_image # Assuming og_image is already a PIL Image
89
-
90
- # Transform the image
91
- image_transformed = load_image(image_source = og_image_obj)
92
 
93
- # Your model prediction code here...
94
- boxes, logits, phrases = grounding_dino_predict(
95
- model=model,
96
- image=image_transformed,
97
- caption=TEXT_PROMPT,
98
- box_threshold=BOX_TRESHOLD,
99
- text_threshold=TEXT_TRESHOLD,
100
- device=DEVICE)
101
-
102
- # Use og_image_obj directly for further processing
103
- height, width = og_image_obj.size
104
- boxes_norm = boxes * torch.Tensor([height, width, height, width])
105
- xyxy = box_convert(
106
- boxes=boxes_norm,
107
- in_fmt="cxcywh",
108
- out_fmt="xyxy").numpy()
109
- img_lst = []
110
- for i in range(len(boxes_norm)):
111
- crop_img = og_image_obj.crop((xyxy[i]))
112
- img_lst.append(crop_img)
113
- return (img_lst)
114
 
115
 
116
  # load beetle classifier model
@@ -123,7 +125,11 @@ print("Classification model loaded")
123
  def predict_beetle(img):
124
  print("Detecting & classifying beetles...")
125
  # Split image into smaller images of detected objects
126
- image_lst = detect_objects(og_image=img, model=od_model, prompt="bug . insect", device="cpu")
 
 
 
 
127
  print("Objects detected")
128
  # get predictions for all segments
129
  conf_dict_lst = []
 
11
  from torchvision.transforms.functional import to_tensor
12
  from torchvision.transforms import GaussianBlur
13
 
14
+ from Ambrosia import pre_process_image
15
+
16
 
17
  # Define a custom transform for Gaussian blur
18
  def gaussian_blur(x, p=0.5, kernel_size_min=3, kernel_size_max=20, sigma_min=0.1, sigma_max=3):
 
57
  unknown_prob = 1-kown_prob
58
  return(unknown_prob)
59
 
60
+ # def load_image(image_source):
61
+ # transform = T.Compose(
62
+ # [
63
+ # T.RandomResize([800], max_size=1333),
64
+ # T.ToTensor(),
65
+ # T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
66
+ # ]
67
+ # )
68
+ # image_source = image_source.convert("RGB")
69
 
70
+ # image_transformed, _ = transform(image_source, None)
71
+ # return image_transformed
72
+
73
+ # # load object detection model
74
+ # od_model = load_model(
75
+ # model_checkpoint_path="groundingdino_swint_ogc.pth",
76
+ # model_config_path="GroundingDINO_SwinT_OGC.cfg.py",
77
+ # device="cpu")
78
+ # print("Object detection model loaded")
79
+
80
+ # def detect_objects(og_image, model=od_model, prompt="bug . insect", device="cpu"):
81
+ # TEXT_PROMPT = prompt
82
+ # BOX_TRESHOLD = 0.35
83
+ # TEXT_TRESHOLD = 0.25
84
+ # DEVICE = device # cuda or cpu
85
 
86
+ # # Convert numpy array to PIL Image if needed
87
+ # if isinstance(og_image, np.ndarray):
88
+ # og_image_obj = Image.fromarray(og_image)
89
+ # else:
90
+ # og_image_obj = og_image # Assuming og_image is already a PIL Image
91
+
92
+ # # Transform the image
93
+ # image_transformed = load_image(image_source = og_image_obj)
94
 
95
+ # # Your model prediction code here...
96
+ # boxes, logits, phrases = grounding_dino_predict(
97
+ # model=model,
98
+ # image=image_transformed,
99
+ # caption=TEXT_PROMPT,
100
+ # box_threshold=BOX_TRESHOLD,
101
+ # text_threshold=TEXT_TRESHOLD,
102
+ # device=DEVICE)
103
+
104
+ # # Use og_image_obj directly for further processing
105
+ # height, width = og_image_obj.size
106
+ # boxes_norm = boxes * torch.Tensor([height, width, height, width])
107
+ # xyxy = box_convert(
108
+ # boxes=boxes_norm,
109
+ # in_fmt="cxcywh",
110
+ # out_fmt="xyxy").numpy()
111
+ # img_lst = []
112
+ # for i in range(len(boxes_norm)):
113
+ # crop_img = og_image_obj.crop((xyxy[i]))
114
+ # img_lst.append(crop_img)
115
+ # return (img_lst)
116
 
117
 
118
  # load beetle classifier model
 
125
  def predict_beetle(img):
126
  print("Detecting & classifying beetles...")
127
  # Split image into smaller images of detected objects
128
+ # image_lst = detect_objects(og_image=img, model=od_model, prompt="bug . insect", device="cpu")
129
+ pre_process = pre_process_image(manual_thresh_buffer=0.15, image = img) # use image_dir if directory of image used
130
+ pre_process.segment(cluster_num=2,
131
+ image_edge_buffer=50)
132
+ image_lst = pre_process.col_image_lst
133
  print("Objects detected")
134
  # get predictions for all segments
135
  conf_dict_lst = []