gatesla commited on
Commit
b421c59
1 Parent(s): 5e0bcd2

Making progress on adding a map

Browse files
Files changed (3) hide show
  1. app.py +13 -5
  2. interpretter_notes.py +18 -0
  3. understand.py +1 -1
app.py CHANGED
@@ -9,7 +9,7 @@ import cv2 as cv
9
  import numpy as np
10
 
11
  from transformers import DetrImageProcessor, DetrForSegmentation, MaskFormerImageProcessor, MaskFormerForInstanceSegmentation
12
- from transformers.models.detr.feature_extraction_detr import rgb_to_id
13
 
14
 
15
 
@@ -95,9 +95,17 @@ def segment_images(model_name,url_input,image_input,threshold):
95
  for r in results["segments_info"]:
96
  contour_list, hierarchy = contour_map(results["segmentation"], r["id"])
97
  label_name = model.config.id2label[r["label_id"]]
98
- return_string += f"ID: {r['id']}, Contour Length: {len(contour_list)}, Label Name: {label_name}, Score: {r['score']}\n"
99
 
100
- return image, return_string
 
 
 
 
 
 
 
 
101
 
102
  pass
103
  else:
@@ -115,8 +123,8 @@ title = """<h1 id="title">Image Segmentation with Various Models</h1>"""
115
  description = """
116
  Links to HuggingFace Models:
117
 
118
- - [facebook/detr-resnet-50-panoptic](https://huggingface.co/facebook/detr-resnet-50-panoptic)
119
- - [facebook/detr-resnet-101-panoptic](https://huggingface.co/facebook/detr-resnet-101-panoptic)
120
  - [facebook/maskformer-swin-large-coco](https://huggingface.co/facebook/maskformer-swin-large-coco)
121
  """
122
 
 
9
  import numpy as np
10
 
11
  from transformers import DetrImageProcessor, DetrForSegmentation, MaskFormerImageProcessor, MaskFormerForInstanceSegmentation
12
+ from transformers.image_transforms import id_to_rgb
13
 
14
 
15
 
 
95
  for r in results["segments_info"]:
96
  contour_list, hierarchy = contour_map(results["segmentation"], r["id"])
97
  label_name = model.config.id2label[r["label_id"]]
98
+ return_string += f"ID: {r['id']}\t Contour Count: {len(contour_list)}\t Score: {r['score']}\t Label Name: {label_name},\n"
99
 
100
+ r_shape = results["segmentation"].shape
101
+ new_image = np.zeros((r[0], r[1], 3), dtype=np.uint8)
102
+ new_image[:, :, 0] = results["segmentation"].numpy()[:, :]
103
+ new_image[:, :, 1] = (new_image[:, :, 0] * 2) %256
104
+ new_image[:, :, 2] = (new_image[:, :, 0] * 3) %256
105
+
106
+ new_image = Image.fromarray(new_image)
107
+
108
+ return new_image, return_string
109
 
110
  pass
111
  else:
 
123
  description = """
124
  Links to HuggingFace Models:
125
 
126
+ - [facebook/detr-resnet-50-panoptic](https://huggingface.co/facebook/detr-resnet-50-panoptic) (Not implemented YET)
127
+ - [facebook/detr-resnet-101-panoptic](https://huggingface.co/facebook/detr-resnet-101-panoptic) (Not implemented YET)
128
  - [facebook/maskformer-swin-large-coco](https://huggingface.co/facebook/maskformer-swin-large-coco)
129
  """
130
 
interpretter_notes.py CHANGED
@@ -154,4 +154,22 @@ def quick_function(id_number):
154
  ... print(f'{model.config.id2label[results["segments_info"][id_number-1]["label_id"]]}, {results["segments_info"][id_number -1]["score"]}, Contour Count: {len(c)}')
155
  ... show_mask_for_number_over_image(results["segmentation"],id_number, TEST_IMAGE)
156
  ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  """
 
154
  ... print(f'{model.config.id2label[results["segments_info"][id_number-1]["label_id"]]}, {results["segments_info"][id_number -1]["score"]}, Contour Count: {len(c)}')
155
  ... show_mask_for_number_over_image(results["segmentation"],id_number, TEST_IMAGE)
156
  ...
157
+ """
158
+
159
+ """
160
+ >>> m = results["segmentation"].cpu().numpy()
161
+ >>> new_dim = (m[0], m[1], 3)
162
+ >>> new_dim
163
+ (array([43, 43, 43, ..., 21, 21, 21], dtype=int32), array([43, 43, 43, ..., 21, 21, 21], dtype=int32), 3)
164
+ >>> new_dim = (m.shape[0], m.shape[1], 3)
165
+ >>> all_z = np.zeros(new_dim, dtype=np.uint8)
166
+
167
+ >>> z = np.zeros((m.shape[0], m.shape[1], 3), dtype=np.uint8)
168
+ >>> z[:, :, 0] = m[:, :]
169
+ >>> z[0,0]
170
+ array([43, 0, 0], dtype=uint8)
171
+ >>> z[0, 0]
172
+ array([43, 0, 0], dtype=uint8)
173
+ >>> m[0, 0]
174
+ 43
175
  """
understand.py CHANGED
@@ -8,7 +8,7 @@ import cv2 as cv
8
 
9
  from transformers import DetrImageProcessor, DetrForSegmentation, MaskFormerImageProcessor, MaskFormerForInstanceSegmentation
10
  # from transformers.models.detr.feature_extraction_detr import rgb_to_id
11
- from transformers.image_transforms import rgb_to_id
12
 
13
  TEST_IMAGE = Image.open(r"images/9999999_00783_d_0000358.jpg")
14
  MODEL_NAME_DETR = "facebook/detr-resnet-50-panoptic"
 
8
 
9
  from transformers import DetrImageProcessor, DetrForSegmentation, MaskFormerImageProcessor, MaskFormerForInstanceSegmentation
10
  # from transformers.models.detr.feature_extraction_detr import rgb_to_id
11
+ from transformers.image_transforms import rgb_to_id, id_to_rgb
12
 
13
  TEST_IMAGE = Image.open(r"images/9999999_00783_d_0000358.jpg")
14
  MODEL_NAME_DETR = "facebook/detr-resnet-50-panoptic"