File size: 7,456 Bytes
3afb283 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
#!/bin/python3
# ============================================================================
# Jimut Bahan Pal
# May, 27, 2021
# A script to generate Mask RCNN Results
# ============================================================================
import os
import cv2
import json
import glob
import math
import random
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
vibrant_colors = [[0,0,255], [0,255,0], [0,255,255], [255,0,0], [255,0,255], [255,255,0]]
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()
# print(root)
anno = []
image_name_attr = ".//image[@name='{}']".format(image_name)
for image_tag in root.iterfind(image_name_attr):
# print("Image tag = ",image_tag)
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'])
# print("box = ",box)
image['shapes'].append(box)
image['shapes'].sort(key=lambda x: int(x.get('z_order', 0)))
anno.append(image)
return anno
all_folders_root = 'Blood SmearAnalysis'
all_folders = glob.glob('{}/*'.format(all_folders_root))
for folders in tqdm(all_folders):
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:
try:
valid_image_names_ = valid_image_names.split('/')[-1]
print("Image Name = ",valid_image_names_)
annot = parse_anno_file(file_name,valid_image_names_)
# print("valid image names_ = ",valid_image_names_)
print("Annotation = ",annot)
# print("--"*20)
annot = annot[0]
# print(json.dumps(annot, indent=4, sort_keys=True))
im_height = annot['height']
im_width = annot['width']
im_id = annot['id']
im_name = annot['name']
im_shapes = annot['shapes']
get_im_path = folders+"/"+im_name
get_image = cv2.imread(get_im_path,cv2.IMREAD_COLOR)
# plt.imshow(get_image[:,:,::-1])
# plt.show()
# print(im_height)
# print(im_width)
# print(im_id)
# print("Annotation name = ",im_name)
name_ = im_name.split('.')[0]
# read image as RGB and add alpha (transparency)
im = Image.open(valid_image_names).convert("RGBA")
imArray = np.asarray(im)
count = 0
get_all_masks = []
get_bbox_coords = [] # x, y, w, h, label, col
for shape in im_shapes:
count += 1
# save_name = SAVE_FOLDER_NAME+"/"+rev_folder_map[subfolder_name]+"/"+name_+"_"+str(count)+".jpg"
# print("Save Name = ",save_name)
# print(shape)
points = shape['points']
label = shape['label']
print(points)
all_points = points.split(';')
# print(all_points)
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)
#print("X = ",x," Y = ",y)
x_y.append((x,y))
# print(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
# print(max_x, " ", min_x, " ",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)
# plt.imshow(maskIm)
# plt.show()
# =========================================
# create the mask image with certain colour
maskIm = np.array(maskIm) * 255
# plt.imshow(maskIm)
# plt.show()
print(maskIm.max())
act_mask = np.zeros_like(get_image)
act_mask[:,:,0] = maskIm
act_mask[:,:,1] = maskIm
act_mask[:,:,2] = maskIm
print("--"*40,get_image.shape,", ",act_mask.shape)
# mask_image_out = get_image.copy()
green_mask = get_image.copy()
col = vibrant_colors[random.randint(0,5)]
green_mask[(act_mask==255).all(-1)] = col
get_bbox_coords.append([min_x,min_y,gap_x,gap_y, label, col])
# plt.imshow(green_mask[:,:,::-1])
# plt.show()
get_all_masks.append(green_mask)
# =========================================
print("--"*40,len(get_all_masks))
final_masked_im = np.zeros_like(get_image)
final_masked_im = float(1/(len(get_all_masks)+1))*get_image
print("fff",final_masked_im.max())
for image in get_all_masks:
print("max = ",image.max(),"min = ",image.min())
final_masked_im = final_masked_im + float(1/(len(get_all_masks)+1))*image
print("Final max = ",final_masked_im.max(),"min = ",final_masked_im.min())
# plt.imshow(final_masked_im[:,:,::-1])
# plt.show()
print("fin = ",final_masked_im.max())
for items in get_bbox_coords:
x,y,w,h,name, col = int(items[0]), int(items[1]), int(items[2]), int(items[3]), items[4], items[5]
cropped_img = np.zeros((w,h,3))
cropped_img = get_image[y:y+h,x:x+w]
plt.imshow(cropped_img[:,:,::-1])
plt.show()
print(x,y,w,h,name)
#col = vibrant_colors[random.randint(0,5)]
cv2.rectangle(final_masked_im, (x,y), (x+w,y+h), col,15)
# img, text, coord, type of font, size, col, thickness
cv2.putText(final_masked_im, str(name), (x, y), 0, 3, [0,0,0], 10)
plt.imshow(final_masked_im[:,:,::-1]/255)
plt.show()
# cv2.imshow("mask", final_masked_im)
# cv2.waitKey(0)
except:
print("PASS")
|