Still trying to understand how it is split
Browse files- understand.py +48 -0
understand.py
CHANGED
@@ -4,6 +4,7 @@ import torch
|
|
4 |
import pathlib
|
5 |
import numpy as np
|
6 |
from PIL import Image
|
|
|
7 |
|
8 |
from transformers import DetrFeatureExtractor, DetrForSegmentation, MaskFormerImageProcessor, MaskFormerForInstanceSegmentation
|
9 |
# from transformers.models.detr.feature_extraction_detr import rgb_to_id
|
@@ -214,6 +215,53 @@ array([[False, False, False, ..., False, False, False],
|
|
214 |
"""
|
215 |
|
216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
217 |
"""
|
218 |
import cv2 as cv
|
219 |
contours, hierarchy = cv.findContours(visual_mask, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
|
|
|
4 |
import pathlib
|
5 |
import numpy as np
|
6 |
from PIL import Image
|
7 |
+
import cv2 as cv
|
8 |
|
9 |
from transformers import DetrFeatureExtractor, DetrForSegmentation, MaskFormerImageProcessor, MaskFormerForInstanceSegmentation
|
10 |
# from transformers.models.detr.feature_extraction_detr import rgb_to_id
|
|
|
215 |
"""
|
216 |
|
217 |
|
218 |
+
def contour_map(map_to_use, label_id):
|
219 |
+
"""
|
220 |
+
map_to_use: You have to pass in `results["segmentation"]`
|
221 |
+
"""
|
222 |
+
if torch.cuda.is_available():
|
223 |
+
mask = (map_to_use.cpu().numpy() == label_id)
|
224 |
+
else:
|
225 |
+
mask = (map_to_use.numpy() == label_id)
|
226 |
+
|
227 |
+
visual_mask = (mask* 255).astype(np.uint8)
|
228 |
+
|
229 |
+
"""
|
230 |
+
>>> mask = (results["segmentation"].cpu().numpy() == 1)
|
231 |
+
>>> visual_mask = (mask* 255).astype(np.uint8)
|
232 |
+
>>> import cv2 as cv
|
233 |
+
>>> contours, hierarchy = cv.findContours(visual_mask, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
|
234 |
+
>>> contours.shape
|
235 |
+
Traceback (most recent call last):
|
236 |
+
File "<stdin>", line 1, in <module>
|
237 |
+
AttributeError: 'tuple' object has no attribute 'shape'
|
238 |
+
>>> contours[0].shape
|
239 |
+
(7, 1, 2)
|
240 |
+
>>> shrunk = contours[0][:, 0, :]
|
241 |
+
>>> shrunk
|
242 |
+
array([[400, 340],
|
243 |
+
[399, 341],
|
244 |
+
[400, 342],
|
245 |
+
[401, 342],
|
246 |
+
[402, 341],
|
247 |
+
[403, 341],
|
248 |
+
[402, 340]], dtype=int32)
|
249 |
+
>>> get_coordinates_for_bb_simple(results["segmentation"], 1)
|
250 |
+
((300, 399), (392, 538))
|
251 |
+
>>> shrunk = contours[1][:, 0, :]
|
252 |
+
>>> max(shrunk[:, 0])
|
253 |
+
538
|
254 |
+
>>> min(shrunk[:, 0])
|
255 |
+
409
|
256 |
+
>>> min(shrunk[:, 1])
|
257 |
+
300
|
258 |
+
>>> max(shrunk[:, 1])
|
259 |
+
392
|
260 |
+
>>>
|
261 |
+
"""
|
262 |
+
|
263 |
+
|
264 |
+
|
265 |
"""
|
266 |
import cv2 as cv
|
267 |
contours, hierarchy = cv.findContours(visual_mask, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
|