ChristopherMarais commited on
Commit
6a04cfd
·
1 Parent(s): 7912d90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -58
app.py CHANGED
@@ -58,62 +58,62 @@ def unkown_prob_calc(probs, wedge_threshold, wedge_magnitude=1, wedge='strict'):
58
  unknown_prob = 1-kown_prob
59
  return(unknown_prob)
60
 
61
- # def load_image(image_source):
62
- # transform = T.Compose(
63
- # [
64
- # T.RandomResize([800], max_size=1333),
65
- # T.ToTensor(),
66
- # T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
67
- # ]
68
- # )
69
- # image_source = image_source.convert("RGB")
70
 
71
- # image_transformed, _ = transform(image_source, None)
72
- # return image_transformed
73
-
74
- # # load object detection model
75
- # od_model = load_model(
76
- # model_checkpoint_path="groundingdino_swint_ogc.pth",
77
- # model_config_path="GroundingDINO_SwinT_OGC.cfg.py",
78
- # device="cpu")
79
- # print("Object detection model loaded")
80
-
81
- # def detect_objects(og_image, model=od_model, prompt="bug . insect", device="cpu"):
82
- # TEXT_PROMPT = prompt
83
- # BOX_TRESHOLD = 0.35
84
- # TEXT_TRESHOLD = 0.25
85
- # DEVICE = device # cuda or cpu
86
 
87
- # # Convert numpy array to PIL Image if needed
88
- # if isinstance(og_image, np.ndarray):
89
- # og_image_obj = Image.fromarray(og_image)
90
- # else:
91
- # og_image_obj = og_image # Assuming og_image is already a PIL Image
92
-
93
- # # Transform the image
94
- # image_transformed = load_image(image_source = og_image_obj)
95
 
96
- # # Your model prediction code here...
97
- # boxes, logits, phrases = grounding_dino_predict(
98
- # model=model,
99
- # image=image_transformed,
100
- # caption=TEXT_PROMPT,
101
- # box_threshold=BOX_TRESHOLD,
102
- # text_threshold=TEXT_TRESHOLD,
103
- # device=DEVICE)
104
-
105
- # # Use og_image_obj directly for further processing
106
- # height, width = og_image_obj.size
107
- # boxes_norm = boxes * torch.Tensor([height, width, height, width])
108
- # xyxy = box_convert(
109
- # boxes=boxes_norm,
110
- # in_fmt="cxcywh",
111
- # out_fmt="xyxy").numpy()
112
- # img_lst = []
113
- # for i in range(len(boxes_norm)):
114
- # crop_img = og_image_obj.crop((xyxy[i]))
115
- # img_lst.append(crop_img)
116
- # return (img_lst)
117
 
118
 
119
  # load beetle classifier model
@@ -127,11 +127,13 @@ def predict_beetle(img):
127
  print("Detecting & classifying beetles...")
128
  start_time = time.perf_counter() # record how long it processes
129
  # Split image into smaller images of detected objects
130
- # image_lst = detect_objects(og_image=img, model=od_model, prompt="bug . insect", device="cpu")
131
- pre_process = pre_process_image(manual_thresh_buffer=0.15, image = img) # use image_dir if directory of image used
132
- pre_process.segment(cluster_num=2,
133
- image_edge_buffer=50)
134
- image_lst = pre_process.col_image_lst
 
 
135
  print("Objects detected")
136
  end_time = time.perf_counter()
137
  processing_time = end_time - start_time
 
58
  unknown_prob = 1-kown_prob
59
  return(unknown_prob)
60
 
61
+ def load_image(image_source):
62
+ transform = T.Compose(
63
+ [
64
+ T.RandomResize([800], max_size=1333),
65
+ T.ToTensor(),
66
+ T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
67
+ ]
68
+ )
69
+ image_source = image_source.convert("RGB")
70
 
71
+ image_transformed, _ = transform(image_source, None)
72
+ return image_transformed
73
+
74
+ # load object detection model
75
+ od_model = load_model(
76
+ model_checkpoint_path="groundingdino_swint_ogc.pth",
77
+ model_config_path="GroundingDINO_SwinT_OGC.cfg.py",
78
+ device="cpu")
79
+ print("Object detection model loaded")
80
+
81
+ def detect_objects(og_image, model=od_model, prompt="bug . insect", device="cpu"):
82
+ TEXT_PROMPT = prompt
83
+ BOX_TRESHOLD = 0.35
84
+ TEXT_TRESHOLD = 0.25
85
+ DEVICE = device # cuda or cpu
86
 
87
+ # Convert numpy array to PIL Image if needed
88
+ if isinstance(og_image, np.ndarray):
89
+ og_image_obj = Image.fromarray(og_image)
90
+ else:
91
+ og_image_obj = og_image # Assuming og_image is already a PIL Image
92
+
93
+ # Transform the image
94
+ image_transformed = load_image(image_source = og_image_obj)
95
 
96
+ # Your model prediction code here...
97
+ boxes, logits, phrases = grounding_dino_predict(
98
+ model=model,
99
+ image=image_transformed,
100
+ caption=TEXT_PROMPT,
101
+ box_threshold=BOX_TRESHOLD,
102
+ text_threshold=TEXT_TRESHOLD,
103
+ device=DEVICE)
104
+
105
+ # Use og_image_obj directly for further processing
106
+ height, width = og_image_obj.size
107
+ boxes_norm = boxes * torch.Tensor([height, width, height, width])
108
+ xyxy = box_convert(
109
+ boxes=boxes_norm,
110
+ in_fmt="cxcywh",
111
+ out_fmt="xyxy").numpy()
112
+ img_lst = []
113
+ for i in range(len(boxes_norm)):
114
+ crop_img = og_image_obj.crop((xyxy[i]))
115
+ img_lst.append(crop_img)
116
+ return (img_lst)
117
 
118
 
119
  # load beetle classifier model
 
127
  print("Detecting & classifying beetles...")
128
  start_time = time.perf_counter() # record how long it processes
129
  # Split image into smaller images of detected objects
130
+ image_lst = detect_objects(og_image=img, model=od_model, prompt="bug . insect", device="cpu")
131
+
132
+ # pre_process = pre_process_image(manual_thresh_buffer=0.15, image = img) # use image_dir if directory of image used
133
+ # pre_process.segment(cluster_num=2,
134
+ # image_edge_buffer=50)
135
+ # image_lst = pre_process.col_image_lst
136
+
137
  print("Objects detected")
138
  end_time = time.perf_counter()
139
  processing_time = end_time - start_time