|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
import cv2 |
|
import json |
|
import glob |
|
import math |
|
import argparse |
|
import shutil |
|
import numpy as np |
|
from lxml import etree |
|
from tqdm import tqdm |
|
from PIL import Image, ImageDraw |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
|
|
SAVE_FOLDER_NAME = 'classification_data' |
|
if not os.path.exists(SAVE_FOLDER_NAME): |
|
os.makedirs(SAVE_FOLDER_NAME) |
|
|
|
def rgba2rgb( rgba, background=(0,0,0) ): |
|
""" |
|
Converts a given rgba image to rgb |
|
""" |
|
row, col, ch = rgba.shape |
|
if ch == 3: |
|
return rgba |
|
assert ch == 4, 'RGBA image has 4 channels.' |
|
rgb = np.zeros( (row, col, 3), dtype='float32' ) |
|
r, g, b, a = rgba[:,:,0], rgba[:,:,1], rgba[:,:,2], rgba[:,:,3] |
|
a = np.asarray( a, dtype='float32' ) / 255.0 |
|
R, G, B = background |
|
rgb[:,:,0] = r * a + (1.0 - a) * R |
|
rgb[:,:,1] = g * a + (1.0 - a) * G |
|
rgb[:,:,2] = b * a + (1.0 - a) * B |
|
return np.asarray( rgb, dtype='uint8') |
|
|
|
def parse_anno_file(cvat_xml,image_name): |
|
""" |
|
Parses annotation file and returns the details of annotation |
|
for the given image ID |
|
""" |
|
root = etree.parse(cvat_xml).getroot() |
|
|
|
anno = [] |
|
image_name_attr = ".//image[@name='{}']".format(image_name) |
|
for image_tag in root.iterfind(image_name_attr): |
|
|
|
image = {} |
|
for key, value in image_tag.items(): |
|
image[key] = value |
|
image['shapes'] = [] |
|
for poly_tag in image_tag.iter('polygon'): |
|
polygon = {'type': 'polygon'} |
|
for key, value in poly_tag.items(): |
|
polygon[key] = value |
|
image['shapes'].append(polygon) |
|
for box_tag in image_tag.iter('box'): |
|
box = {'type': 'box'} |
|
for key, value in box_tag.items(): |
|
box[key] = value |
|
box['points'] = "{0},{1};{2},{1};{2},{3};{0},{3}".format( |
|
box['xtl'], box['ytl'], box['xbr'], box['ybr']) |
|
|
|
image['shapes'].append(box) |
|
image['shapes'].sort(key=lambda x: int(x.get('z_order', 0))) |
|
anno.append(image) |
|
|
|
return anno |
|
|
|
FOLDERS_LIST = ["MYELOCYTE", "BAND CELLS", "NEUTROPHILS", "BASOPHILS", "EOSINOPHILS", "PROMYELOCYTES", "BLAST CELLS", "LYMPHOCYTES", "METAMYELOCYTES", "MONOCYTES"] |
|
|
|
|
|
|
|
folder_map = {} |
|
rev_folder_map = {} |
|
folder_map = { |
|
"band": "BAND CELLS", |
|
"basophil": "BASOPHILS", |
|
"blast": "BLAST CELLS", |
|
"eosinophil": "EOSINOPHILS", |
|
"lymphocyte": "LYMPHOCYTES", |
|
"metamyelocyte": "METAMYELOCYTES", |
|
"monocyte": "MONOCYTES", |
|
"myelocyte": "MYELOCYTE", |
|
"neutrophil": "NEUTROPHILS", |
|
"promyelocyte": "PROMYELOCYTES" |
|
} |
|
|
|
|
|
|
|
for item in folder_map: |
|
rev_folder_map[folder_map[item]] = item |
|
|
|
|
|
|
|
|
|
|
|
all_folders_root = 'RV-PBS' |
|
all_folders = glob.glob('{}/*'.format(all_folders_root)) |
|
|
|
for folders in tqdm(all_folders): |
|
if folders.split('/')[-1] not in FOLDERS_LIST: |
|
continue |
|
|
|
|
|
all_files_per_folder = glob.glob('{}/*'.format(folders)) |
|
|
|
all_valid_files_per_folder = [] |
|
|
|
for files in all_files_per_folder: |
|
|
|
get_extension = files.split('.')[-1] |
|
|
|
if get_extension == 'jpg' or get_extension == 'png': |
|
all_valid_files_per_folder.append(files) |
|
|
|
|
|
|
|
|
|
|
|
|
|
file_name = folders+"/annotations.xml" |
|
|
|
for valid_image_names in all_valid_files_per_folder: |
|
subfolder_name = valid_image_names.split('/')[1] |
|
|
|
subfolder_save = SAVE_FOLDER_NAME+"/"+rev_folder_map[subfolder_name] |
|
if not os.path.exists(subfolder_save): |
|
os.makedirs(subfolder_save) |
|
valid_image_names_ = valid_image_names.split('/')[-1] |
|
|
|
annot = parse_anno_file(file_name,valid_image_names_) |
|
|
|
|
|
|
|
try: |
|
annot = annot[0] |
|
except: |
|
continue |
|
|
|
im_height = annot['height'] |
|
im_width = annot['width'] |
|
im_id = annot['id'] |
|
im_name = annot['name'] |
|
im_shapes = annot['shapes'] |
|
|
|
|
|
|
|
|
|
|
|
name_ = im_name.split('.')[0] |
|
|
|
im = Image.open(valid_image_names).convert("RGBA") |
|
imArray = np.asarray(im) |
|
count = 0 |
|
for shape in im_shapes: |
|
count += 1 |
|
save_name = SAVE_FOLDER_NAME+"/"+rev_folder_map[subfolder_name]+"/"+name_+"_"+str(count)+".jpg" |
|
|
|
|
|
points = shape['points'] |
|
|
|
all_points = points.split(';') |
|
|
|
x_y = [] |
|
all_x = [] |
|
all_y = [] |
|
for point_ in all_points: |
|
x = float(point_.split(',')[0]) |
|
y = float(point_.split(',')[1]) |
|
all_x.append(x) |
|
all_y.append(y) |
|
|
|
x_y.append((x,y)) |
|
|
|
max_x = max(all_x) |
|
min_x = min(all_x) |
|
max_y = max(all_y) |
|
min_y = min(all_y) |
|
gap_x = max_x - min_x |
|
gap_y = max_y - min_y |
|
|
|
|
|
maskIm = Image.new('L', (imArray.shape[1], imArray.shape[0]), 0) |
|
ImageDraw.Draw(maskIm).polygon(x_y, outline=1, fill=1) |
|
mask = np.array(maskIm) |
|
|
|
|
|
newImArray = np.empty(imArray.shape,dtype='uint8') |
|
|
|
|
|
newImArray[:,:,:3] = imArray[:,:,:3] |
|
|
|
newImArray[:,:,3] = mask*255 |
|
|
|
|
|
img_extract = np.zeros((math.ceil(gap_x),math.ceil(gap_y),3)) |
|
img_extract = newImArray[math.ceil(min_y):math.ceil(max_y),math.ceil(min_x):math.ceil(max_x)] |
|
|
|
|
|
|
|
newIm = Image.fromarray(newImArray, "RGBA") |
|
|
|
img_extract = rgba2rgb(img_extract) |
|
|
|
cv2.imwrite(save_name,np.array(img_extract)) |
|
|
|
|
|
|
|
|
|
|
|
|