Cyrile commited on
Commit
deacd55
·
verified ·
1 Parent(s): 44aad78

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +23 -0
README.md CHANGED
@@ -49,6 +49,7 @@ Now, let's compare the performance of this model with other models.
49
  ### Direct Use
50
 
51
  ```python
 
52
  import torch
53
  from transformers import AutoImageProcessor, AutoModel
54
 
@@ -67,6 +68,28 @@ segmentation = img_proc.post_process_semantic_segmentation(
67
  output,
68
  target_sizes=[img.size[::-1]]
69
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  ```
71
 
72
  ### Citation
 
49
  ### Direct Use
50
 
51
  ```python
52
+ import cv2
53
  import torch
54
  from transformers import AutoImageProcessor, AutoModel
55
 
 
68
  output,
69
  target_sizes=[img.size[::-1]]
70
  )
71
+
72
+ def detect_bboxes(masks: np.ndarray):
73
+ detected_blocks = []
74
+ contours, _ = cv2.findContours(masks.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
75
+ for contour in list(contours):
76
+ if len(list(contour)) >= 4:
77
+ # smallest rectangle containing all points in the contour is computed
78
+ x, y, width, height = cv2.boundingRect(contour)
79
+ bounding_box = [x, y, x + width, y + height]
80
+ detected_blocks.append(bounding_box)
81
+ return detected_blocks
82
+
83
+ pred_bbox = []
84
+ for segment in segmentation:
85
+ bboxes, labels = [], []
86
+ for ii in range(1, len(items)):
87
+ mm = segment == ii
88
+ if mm.sum() > 0:
89
+ bbx, lab = detect_bboxes(mm.numpy())
90
+ bboxes.extend(bbx)
91
+ labels.extend([ii]*len(bbx))
92
+ pred_bbox.append(dict(bboxes=bboxes, labels=lables))
93
  ```
94
 
95
  ### Citation