from yolo_v8_segmentation.viz_utils import *
yolo_v8_segmentation
segementation training and inference with yolov8 model
This file will become your README and also the index of your documentation.
Install
pip install yolo_v8_segmentation
How to use
Visualize mask and image
= Path(r'/home/hasan/Schreibtisch/projects/data/microscopy/patch_train_masks')
msk_path = Path(r'/home/hasan/Schreibtisch/projects/data/microscopy/patch_train_images/') img_path
= 2
idx = img_path.ls()[idx]
image = msk_path.ls()[idx] msk
overlay_mask_border_on_image
overlay_mask_border_on_image (im_path, msk_path, border_color=(0, 1, 0), border_width=1)
*Overlays the border of a binary mask on a grayscale image and displays the result using matplotlib.
Args: image (numpy.ndarray): Grayscale image. mask (numpy.ndarray): Binary mask of the same size as the image. border_color (tuple): RGB color for the mask border in the range [0, 1]. border_width (int): Width of the border.
Returns: None: The function displays a plot.*
=image, msk_path=msk) overlay_mask_border_on_image(im_path
=image, msk_path=msk) overlay_mask(im_path
Yolo annotation visualization
- Upto now it is just a folder where there are some image and mask pairs. Previous method shows the masks and images
- In case data is prepared in
.txt
format for training yolo, it is important to check it whether the annotations are correct or not. This function will help to visualize the annotations on the image.
= Path(r'/home/hasan/Schreibtisch/projects/data/microscopy/yolo_dataset_train')
yolo_mask_path = Path(r'/home/hasan/Schreibtisch/projects/data/microscopy/patch_train_images/') im_path
=im_path, msk_path=yolo_mask_path) overlay_yolo_mask(im_path
- in case specific image wanted to see, then one need to pass the image name to the function
- here I am using im_name=None, so random image is selected
=im_path, msk_path=yolo_mask_path, im_name=None, fill=True, alpha=0.3) overlay_yolo_mask(im_path
overlay_yolo_mask(=im_path,
im_path=yolo_mask_path,
msk_path=None,
im_name='red',
color=True,
fill=0.3) alpha
Convert label to yolo format
First coco format json file
- Normaly there are some labelling tool where you can label your images and then upload them direct in the yolo format.
- But we are supossing that, you have some binary masks and then you want to convert them to yolo format.
- First we will convert to coco format and then to yolo format.
# in my case I have only one class and others are background, in case of more class, one should add name and id for the masks
= {
category_ids "object": 1,
}
# where masks(in my case binary masks) are stored
= Path(r'/home/hasan/Schreibtisch/projects/data/microscopy/patch_train_masks')
trn_msk_path # this will be json path for the coco format
= Path(r'/home/hasan/Schreibtisch/projects/data/microscopy/patch_train_masks/train.json')
trn_json_path
process_masks(=trn_msk_path,
mask_path=trn_json_path,
json_path=category_ids) category_ids
Now coco foramt
create_yolo_dataset(=trn_img_path,# train images path
img_path=trn_output_path, # output for each image normally some txt file
output_path=trn_json_path, # json path for coco format
json_path )
# for yolo to run one needs to create a yaml file to tell where the data is situated
# coco expect following format
# train: /path/to/train/images
# label: /path/to/train/labels
# valid: /path/to/valid/images
# valid_label: /path/to/valid/labels
create_yaml(=trn_json_path, # coco format json path
json_path=f'{path}/data.yaml', # yaml path for saving
yaml_path=yolo_trn_images, # where train images are stored
train_path=trn_img_path, # where validaiton images are stored
val_path )