Functions that prepare label files and split the data into train validation and test sets

Imports

Typical Usage

Imports
Define directories
get stats on images
Well Croping & Resizing link to another section
Convert directory of CSV files to JSON files link to another section
Resize jpg & json files link to another section Create label png images based on Json files and store in dest directory") link to another section Split into Train, Val and Test link to another section Save label files without color pallet info link to another section Test Create DataBunch link to another section Show example images link to another section

Default Class Labels

Well Croping & Resizing

crop2well[source]

crop2well(img, thres_adjust=1.0, bg_color=[0, 0, 0], op='crop')

Returns a square colour and gray image centered on the well with width same as the height. thres_adjust is optional otsu threshold scaling multiplier Also returns the offset to the original image and the well region properties

well_circle_mask[source]

well_circle_mask(img, well_regionprops)

check_well_crop[source]

check_well_crop(img, well_regionprops)

resize_crop2well_one[source]

resize_crop2well_one(fn, i, src_path, dest_path, height)

Pandas and CSV

shuffle_csv[source]

shuffle_csv(file_csv, random_state=None)

split_filenames[source]

split_filenames(file_csv, num_train=0.7, num_val=0.15)

shuffle file names, split into train valid and test and update file_data.csv with labels

:param file_csv: csv file with file names and data :param num_train: :param num_val: :param seed: :return:

Other

get_image_size[source]

get_image_size(fn)

make_label_img_from_json[source]

make_label_img_from_json(jdata, img, radius=20)

returns a label img with filled circles at places of interest json data contains info to markup an image with circles

This function creates two markup up files

jdata = json.load(open(TEST_JSON))
img = np_open_image(TEST_JPG)
img, lab_img, num_points, label_cnt_dict = make_label_img_from_json(jdata, img, radius=20)
plot_images({'RGB':img, 'P':lab_img}, figsize=(12, 5))
lab_img.shape

sav_pil = PIL.Image.fromarray(lab_img.astype(np.uint8), mode='P')
sav_pil.putpalette(LABEL_COLORMAP)
fn = Path(TEST_JPG)
sav_pil.save(TEST_PNG)

load_pil = np_open_image(TEST_PNG)
show_img(np_open_image(TEST_PNG))

print(lab_img.min(), lab_img.max())
0 11

create_one_label[source]

create_one_label(fn, json_path, lab_path, colormap)

if IN_IPYTHON:
    img = np_open_image(TEST_JPG)
    data = json.load(open(TEST_JSON))
    data = json.load(open('nbs_test_data/235443 - 1.json'))
    create_one_label(TEST_JPG, TEST_PATH, TEST_PATH, LABEL_COLORMAP)
    TEST_PATH
from  cellsegment.core import *
# TEST_PATH

resize_dir[source]

resize_dir(file_data, src_path, dest_path, number_files='all', height=1200)

Resize an entire directory. Store in the dest directory :param src_path: :param dest_path: :param number_files: :param height: :return:

__crop_image[source]

__crop_image(jsonfn, imgfn, dest_path, size=200, op:str='', debug=False)

Crop an image into multiple sub-tiles centered on each egg :param jsonfn: JSON file with egg centers :param imgfn: image to be cropped :param dest_path: directory to put cropped tiles into :param size: pixel size of crop :param filename_trim: remove the scale and offset portions of filename and remove extra spaces, 226260 - 1-0.436047-221-.jpg -> 226260-1.jpg :return: number of cropped images

crop_img_dir[source]

crop_img_dir(file_data:str, json_path:str, src_path:str, dest_path:str, number_files='all', DEBUG=False)

Crop directory of image files based on json centers and store in dest directory

  • file_data: file_data.csv
  • src_path: path where json files and image files to be cropped are
  • dest_path: Destination path to store cropped files
  • number_files: Number of json files to process, leave empty for all files in directory
  • size: pixel size of crop
  • return: total number of cropped images`

move_files_to_dir[source]

move_files_to_dir(movefiles, srcpath, destpath, operation='move', extns=['.jpg', '.json', '.png'])

Move random shuffle of the source directory to the Train, Val and Test directories

:param movefiles: list of files to move :param srcpath: path where to src train files :param destpath: path where to put test files :param extns: list of extensions to try :return: cnt of files_moved, files_missed

old_move_files_to_dir[source]

old_move_files_to_dir(movefiles, srcpath, destpath, operation='move', extns=['.jpg', '.json', '.png'])

Move random shuffle of the source directory to the Train, Val and Test directories

:param movefiles: list of files to move :param srcpath: path where to src train files :param destpath: path where to put test files :param extns: list of extensions to try :return: cnt of files_moved, files_missed

create_labels_dir[source]

create_labels_dir(json_path, dest_path, number_files='all')

Create label png images based on CSV files and store in dest directory

:param json_path: Source path where json files are :param dest_path: Destination path to store created png label files :param number_files: Number of json files to process, leave empty for all files in directory :return: nil

end