Skip to content

Commit

Permalink
add script for labelling geo images using sam and saving results npz …
Browse files Browse the repository at this point in the history
…format
  • Loading branch information
SVivdich02 committed Jun 17, 2024
1 parent de0b4d0 commit ff25f9b
Show file tree
Hide file tree
Showing 2 changed files with 313 additions and 7 deletions.
266 changes: 259 additions & 7 deletions geo_seg_debug.ipynb

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions geo_seg_image_sam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright (c) 2023, Sofia Vivdich and Anastasiia Kornilova
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import cv2
import numpy as np

from segment_anything import sam_model_registry, SamAutomaticMaskGenerator

from src.constants import CHECKPOINT_PATH
from src.constants import DEVICE
from src.constants import MODEL_TYPE


def get_array_instances_by_image_sam(cam_image):
sam = sam_model_registry[MODEL_TYPE](checkpoint=CHECKPOINT_PATH).to(device=DEVICE)

mask_generator = SamAutomaticMaskGenerator(sam)

return mask_generator.generate(cam_image)


def main():
n = 43

for i in range(n):
file_name_read = ''
file_name_write = ''

if (i <= 9):
file_name_read = 'images_cams/images/0000000{}.jpg'.format(i)
file_name_write = 'geo-seg/vfm-labels/sam/0000000{}.npz'.format(i)
else:
file_name_read = 'images_cams/images/000000{}.jpg'.format(i)
file_name_write = 'geo-seg/vfm-labels/sam/000000{}.npz'.format(i)

instances_array = get_array_instances_by_image_sam(cv2.imread(file_name_read))

np.savez_compressed(file_name_write, masks=instances_array)
print("Finish i={}".format(i))


if __name__ == "__main__":
main()

0 comments on commit ff25f9b

Please sign in to comment.