-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
264 lines (205 loc) · 9.3 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import os
from typing import List
import urllib.request
import cv2
import numpy as np
import supervision as sv
import torch
from groundingdino.util.inference import Model
from segment_anything import SamPredictor, sam_model_registry
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if not torch.cuda.is_available():
print("WARNING: CUDA not available. GroundingDINO will run very slowly.")
def image_resize(img, size=(480, 480)):
h, w = img.shape[:2]
c = img.shape[2] if len(img.shape)>2 else 1
if h == w:
return cv2.resize(img, size, cv2.INTER_AREA)
dif = h if h > w else w
interpolation = cv2.INTER_AREA if dif > (size[0]+size[1])//2 else cv2.INTER_CUBIC
x_pos = (dif - w)//2
y_pos = (dif - h)//2
if len(img.shape) == 2:
mask = np.zeros((dif, dif), dtype=img.dtype)
mask[y_pos:y_pos+h, x_pos:x_pos+w] = img[:h, :w]
else:
mask = np.zeros((dif, dif, c), dtype=img.dtype)
mask[y_pos:y_pos+h, x_pos:x_pos+w, :] = img[:h, :w, :]
return cv2.resize(mask, size, interpolation)
def mask_to_polygon(mask: np.ndarray) -> np.ndarray:
"""
Converts a binary mask to ONE polygon.
Parameters:
mask (np.ndarray): A binary mask represented as a 2D NumPy array of
shape `(H, W)`, where H and W are the height and width of
the mask, respectively.
Returns:
List[np.ndarray]: A list of polygons, where each polygon is represented by a
NumPy array of shape `(N, 2)`, containing the `x`, `y` coordinates
of the points. Polygons with fewer points than `MIN_POLYGON_POINT_COUNT = 3`
are excluded from the output.
"""
contours, _ = cv2.findContours(
mask.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
return cv2.convexHull(np.vstack(contours))
def crop_obb(img, obb):
# get the parameter of the small rectangle
center = obb[0]
size = obb[1]
angle = obb[2]
center, size = tuple(map(int, center)), tuple(map(int, size))
# get row and col num in img
height, width = img.shape[0], img.shape[1]
# print("width: {}, height: {}".format(width, height))
M = cv2.getRotationMatrix2D(center, angle, 1)
img_rot = cv2.warpAffine(img, M, (width, height))
img_crop = cv2.getRectSubPix(img_rot, size, center)
return img_crop
def overlay(image, mask, color, alpha, resize=None):
"""Combines image and its segmentation mask into a single image.
Params:
image: Training image. np.ndarray,
mask: Segmentation mask. np.ndarray,
color: Color for segmentation mask rendering. tuple[int, int, int] = (255, 0, 0)
alpha: Segmentation mask's transparency. float = 0.5,
resize: If provided, both image and its mask are resized before blending them together.
tuple[int, int] = (1024, 1024))
Returns:
image_combined: The combined image. np.ndarray
"""
color = color[::-1]
colored_mask = np.expand_dims(mask, 0).repeat(3, axis=0)
colored_mask = np.moveaxis(colored_mask, 0, -1)
masked = np.ma.MaskedArray(image, mask=colored_mask, fill_value=color)
image_overlay = masked.filled()
if resize is not None:
image = cv2.resize(image.transpose(1, 2, 0), resize)
image_overlay = cv2.resize(image_overlay.transpose(1, 2, 0), resize)
image_combined = cv2.addWeighted(image, 1 - alpha, image_overlay, alpha, 0)
return image_combined
# find the most confident detection in detections
def most_confident_detection(detections):
if detections.confidence is None:
return None
max_idx = np.argmax(detections.confidence)
return sv.Detections(
xyxy=detections.xyxy[max_idx : max_idx + 1],
mask=detections.mask[max_idx : max_idx + 1] if detections.mask is not None else None,
confidence=detections.confidence[max_idx : max_idx + 1],
class_id=detections.class_id[max_idx : max_idx + 1] if detections.class_id is not None else None,
tracker_id=detections.tracker_id[max_idx : max_idx + 1] if detections.tracker_id is not None else None,
)
# find the detection with smallest area in detections
def smallest_detection(detections):
if detections.xyxy is None:
return None
areas = (detections.xyxy[:, 2] - detections.xyxy[:, 0]) * (detections.xyxy[:, 3] - detections.xyxy[:, 1])
min_idx = np.argmin(areas)
return sv.Detections(
xyxy=detections.xyxy[min_idx : min_idx + 1],
mask=detections.mask[min_idx : min_idx + 1] if detections.mask is not None else None,
confidence=detections.confidence[min_idx : min_idx + 1],
class_id=detections.class_id[min_idx : min_idx + 1] if detections.class_id is not None else None,
tracker_id=detections.tracker_id[min_idx : min_idx + 1] if detections.tracker_id is not None else None,
)
def combine_detections(detections_list, overwrite_class_ids):
if len(detections_list) == 0:
return sv.Detections.empty()
if overwrite_class_ids is not None and len(overwrite_class_ids) != len(
detections_list
):
raise ValueError(
"Length of overwrite_class_ids must match the length of detections_list."
)
xyxy = []
mask = []
confidence = []
class_id = []
tracker_id = []
for idx, detection in enumerate(detections_list):
xyxy.append(detection.xyxy)
if detection.mask is not None:
mask.append(detection.mask)
if detection.confidence is not None:
confidence.append(detection.confidence)
if detection.class_id is not None:
if overwrite_class_ids is not None:
# Overwrite the class IDs for the current Detections object
class_id.append(
np.full_like(
detection.class_id, overwrite_class_ids[idx], dtype=np.int64
)
)
else:
class_id.append(detection.class_id)
if detection.tracker_id is not None:
tracker_id.append(detection.tracker_id)
xyxy = np.vstack(xyxy)
mask = np.vstack(mask) if mask else None
confidence = np.hstack(confidence) if confidence else None
class_id = np.hstack(class_id) if class_id else None
tracker_id = np.hstack(tracker_id) if tracker_id else None
return sv.Detections(
xyxy=xyxy,
mask=mask,
confidence=confidence,
class_id=class_id,
tracker_id=tracker_id,
)
def load_grounding_dino():
CACHE_DIR = os.path.expanduser("./.cache")
GROUDNING_DINO_CACHE_DIR = os.path.join(CACHE_DIR, "groundingdino")
GROUNDING_DINO_CONFIG_PATH = os.path.join(
GROUDNING_DINO_CACHE_DIR, "cfg_odvg.py"
# GROUDNING_DINO_CACHE_DIR, "GroundingDINO_SwinB_cfg.py"
)
GROUNDING_DINO_CHECKPOINT_PATH = os.path.join(
GROUDNING_DINO_CACHE_DIR, "gdinot-1.8m-odvg.pth"
# GROUDNING_DINO_CACHE_DIR, "groundingdino_swinb_cogcoor.pth"
)
try:
print("trying to load grounding dino directly")
grounding_dino_model = Model(
model_config_path=GROUNDING_DINO_CONFIG_PATH,
model_checkpoint_path=GROUNDING_DINO_CHECKPOINT_PATH,
device=str(DEVICE),
)
return grounding_dino_model
except Exception:
print("downloading DINO model weights")
if not os.path.exists(GROUDNING_DINO_CACHE_DIR):
os.makedirs(GROUDNING_DINO_CACHE_DIR)
if not os.path.exists(GROUNDING_DINO_CHECKPOINT_PATH):
# url = "https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha2/groundingdino_swinb_cogcoor.pth"
url = "https://github.com/longzw1997/Open-GroundingDino/releases/download/v0.1.0/gdinot-1.8m-odvg.pth"
urllib.request.urlretrieve(url, GROUNDING_DINO_CHECKPOINT_PATH)
if not os.path.exists(GROUNDING_DINO_CONFIG_PATH):
url = "https://raw.githubusercontent.com/longzw1997/Open-GroundingDino/refs/heads/main/config/cfg_odvg.py"
# url = "https://raw.githubusercontent.com/IDEA-Research/GroundingDINO/refs/heads/main/groundingdino/config/GroundingDINO_SwinB_cfg.py"
urllib.request.urlretrieve(url, GROUNDING_DINO_CONFIG_PATH)
grounding_dino_model = Model(
model_config_path=GROUNDING_DINO_CONFIG_PATH,
model_checkpoint_path=GROUNDING_DINO_CHECKPOINT_PATH,
device=str(DEVICE),
)
# grounding_dino_model.to(DEVICE)
return grounding_dino_model
def load_SAM():
# Check if segment-anything library is already installed
CACHE_DIR = os.path.expanduser("./.cache")
SAM_CACHE_DIR = os.path.join(CACHE_DIR, "segment_anything")
SAM_CHECKPOINT_PATH = os.path.join(SAM_CACHE_DIR, "sam_vit_h_4b8939.pth")
url = "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth"
# Create the destination directory if it doesn't exist
os.makedirs(os.path.dirname(SAM_CHECKPOINT_PATH), exist_ok=True)
# Download the file if it doesn't exist
if not os.path.isfile(SAM_CHECKPOINT_PATH):
print("downloading SAM model weights")
urllib.request.urlretrieve(url, SAM_CHECKPOINT_PATH)
SAM_ENCODER_VERSION = "vit_h"
sam = sam_model_registry[SAM_ENCODER_VERSION](checkpoint=SAM_CHECKPOINT_PATH).to(
device=DEVICE
)
sam_predictor = SamPredictor(sam)
return sam_predictor