def get_lungs_size(img, min_area=int(1024 * 1024 * 0.05)): mask = np.zeros((1024, 1024), dtype=int) # Try hog right = lf.find_right_lung_hog(img) left = lf.find_left_lung_hog(img) # If hog don't find lungs then try lbp method if right is None: right = lf.find_right_lung_lbp(img) left = lf.find_left_lung_lbp(img) # If lbp don't find lungs then try haar method if right is None: right = lf.find_right_lung_haar(img) left = lf.find_left_lung_haar(img) # If don't find then return mask filled mask if (right is None) or (left is None): return 1024, 1024 x_r, y_r, width_r, height_r = right x_l, y_l, width_l, height_l = left # If found lungs is very small if (width_r * height_r < min_area) or (width_l * height_l < min_area): return 1024, 1024 x_l_union = min(x_r, x_l) x_r_union = max(x_r + width_r, x_l + width_l) y_u_union = max(y_r + height_r, y_l + height_l) y_d_union = min(y_r, y_l) w = x_r_union - x_l_union h = y_u_union - y_d_union return w, h
def lungs_finder_segmentator(img, is_union=True, min_area=int(1024 * 1024 * 0.05)): mask = np.zeros((1024, 1024), dtype=int) # Try hog right = lf.find_right_lung_hog(img) left = lf.find_left_lung_hog(img) # If hog don't find lungs then try lbp method if right is None: right = lf.find_right_lung_lbp(img) left = lf.find_left_lung_lbp(img) # If lbp don't find lungs then try haar method if right is None: right = lf.find_right_lung_haar(img) left = lf.find_left_lung_haar(img) # If don't find then return mask filled mask if (right is None) or (left is None): return mask + 255 x_r, y_r, width_r, height_r = right x_l, y_l, width_l, height_l = left # If found lungs is very small if (width_r * height_r < min_area) or (width_l * height_l < min_area): return mask + 255 if is_union: x_l_union = min(x_r, x_l) x_r_union = max(x_r + width_r, x_l + width_l) y_u_union = max(y_r + height_r, y_l + height_l) y_d_union = min(y_r, y_l) mask[y_d_union:y_u_union, x_l_union:x_r_union] = 255 else: mask[y_r:y_r + height_r, x_r:x_r + width_r] = 255 mask[y_l:y_l + height_l, x_l:x_l + width_l] = 255 return mask
def scan(argv): path_to_folder, use_labels = parse_argv(argv) walks = list(os.walk(path_to_folder)) i = 0 while i < len(walks): path, directories, files = walks[i] files = [file for file in files if not file[0] == "."] j = 0 while j < len(files): file = files[j] _, extension = os.path.splitext(file) if extension == ".dcm" and dicom_support: parsed = dicomparser.DicomParser(path + os.sep + file) image = np.array(parsed.GetImage(), dtype=np.uint8) if parsed.GetImageData( )["photometricinterpretation"] == "MONOCHROME1": image = 255 - image image = cv2.equalizeHist(image) image = cv2.medianBlur(image, 3) elif extension in [ ".bmp", ".pbm", ".pgm", ".ppm", ".sr", ".ras", ".jpeg", ".jpg", ".jpe", ".png", ".tiff", ".tif" ]: image = cv2.imread(path + os.sep + file, 0) image = cv2.equalizeHist(image) image = cv2.medianBlur(image, 3) else: j += 1 continue scaled_image = proportional_resize(image, 512) right_lung_hog_rectangle = lf.find_right_lung_hog(scaled_image) left_lung_hog_rectangle = lf.find_left_lung_hog(scaled_image) right_lung_lbp_rectangle = lf.find_right_lung_lbp(scaled_image) left_lung_lbp_rectangle = lf.find_left_lung_lbp(scaled_image) right_lung_haar_rectangle = lf.find_right_lung_haar(scaled_image) left_lung_haar_rectangle = lf.find_left_lung_haar(scaled_image) color_image = cv2.cvtColor(scaled_image, cv2.COLOR_GRAY2BGR) if right_lung_hog_rectangle is not None: x, y, width, height = right_lung_hog_rectangle cv2.rectangle(color_image, (x, y), (x + width, y + height), (59, 254, 211), 2) if use_labels: cv2.putText(color_image, "HOG Right", (x + 5, y + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (59, 254, 211), 1) if left_lung_hog_rectangle is not None: x, y, width, height = left_lung_hog_rectangle cv2.rectangle(color_image, (x, y), (x + width, y + height), (59, 254, 211), 2) if use_labels: cv2.putText(color_image, "HOG Left", (x + 5, y + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (59, 254, 211), 1) if right_lung_lbp_rectangle is not None: x, y, width, height = right_lung_lbp_rectangle cv2.rectangle(color_image, (x, y), (x + width, y + height), (130, 199, 0), 2) if use_labels: cv2.putText(color_image, "LBP Right", (x + 5, y + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (130, 199, 0), 1) if left_lung_lbp_rectangle is not None: x, y, width, height = left_lung_lbp_rectangle cv2.rectangle(color_image, (x, y), (x + width, y + height), (130, 199, 0), 2) if use_labels: cv2.putText(color_image, "LBP Left", (x + 5, y + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (130, 199, 0), 1) if right_lung_haar_rectangle is not None: x, y, width, height = right_lung_haar_rectangle cv2.rectangle(color_image, (x, y), (x + width, y + height), (245, 199, 75), 2) if use_labels: cv2.putText(color_image, "HAAR Right", (x + 5, y + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (245, 199, 75), 1) if left_lung_haar_rectangle is not None: x, y, width, height = left_lung_haar_rectangle cv2.rectangle(color_image, (x, y), (x + width, y + height), (245, 199, 75), 2) if use_labels: cv2.putText(color_image, "HAAR Left", (x + 5, y + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (245, 199, 75), 1) cv2.imshow("lungs-finder", color_image) found_lungs = lf.get_lungs(scaled_image) if found_lungs is not None: cv2.imshow("Found lungs", found_lungs) code = cv2.waitKey(0) while code not in [2, 3, 27, 32]: code = cv2.waitKey(0) if code == 27: exit(0) elif code in [3, 32]: j += 1 else: if j > 0: j -= 1 else: if i > 0: i -= 2 i += 1
def main(data): decoded_data = base64.b64decode(data) np_data = np.fromstring(decoded_data, np.uint8) img = cv2.imdecode(np_data, cv2.IMREAD_UNCHANGED) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) image2 = cv2.equalizeHist(img_gray) image = cv2.medianBlur(image2, 3) scaled_image = image # cv2.resize(image, (1024,1024)) right_lung_hog_rectangle = lf.find_right_lung_hog(scaled_image) left_lung_hog_rectangle = lf.find_left_lung_hog(scaled_image) right_lung_lbp_rectangle = lf.find_right_lung_lbp(scaled_image) left_lung_lbp_rectangle = lf.find_left_lung_lbp(scaled_image) right_lung_haar_rectangle = lf.find_right_lung_haar(scaled_image) left_lung_haar_rectangle = lf.find_left_lung_haar(scaled_image) color_image = cv2.cvtColor(scaled_image, cv2.COLOR_GRAY2BGR) if (right_lung_hog_rectangle is not None) and (left_lung_hog_rectangle is not None): x1, y1, width1, height1 = right_lung_hog_rectangle x2, y2, width2, height2 = left_lung_hog_rectangle cimage1 = image2[np.minimum(y1, y2):np.maximum(y1 + height1, y2 + height2), x1:x2 + width2] pole1 = width1 * height1 + width2 * height2 if (right_lung_lbp_rectangle is not None) and (left_lung_lbp_rectangle is not None): x1, y1, width1, height1 = right_lung_lbp_rectangle x2, y2, width2, height2 = left_lung_lbp_rectangle cimage2 = image2[np.minimum(y1, y2):np.maximum(y1 + height1, y2 + height2), x1:x2 + width2] pole2 = width1 * height1 + width2 * height2 if (right_lung_haar_rectangle is not None) and (left_lung_haar_rectangle is not None): x1, y1, width1, height1 = right_lung_haar_rectangle x2, y2, width2, height2 = left_lung_haar_rectangle cimage3 = image2[np.minimum(y1, y2):np.maximum(y1 + height1, y2 + height2), x1:x2 + width2] pole3 = width1 * height1 + width2 * height2 if (pole1 >= pole2) and (pole1 >= pole3) and (pole1 > 0): cimage = cimage1 if (pole2 > pole1) and (pole2 >= pole3): cimage = cimage2 if (pole3 > pole1) and (pole3 > pole2): cimage = cimage3 if (cimage.shape[0] > 0) and (cimage.shape[1] > 0): image = cimage filename = join(dirname(__file__), "resnet_cropped_CvHvP_detection2.h5") model = tf.keras.models.load_model(filename) filename = join(dirname(__file__), "unet_lung_seg_serial.hdf5") model_croping = tf.keras.models.load_model(filename) ## image_c = cimage image_c = cv2.resize(image_c, (512, 512), interpolation=cv2.INTER_AREA) image_c = np.array(image_c) / 255 image_c = image_c.astype('float32') image_to_predict = image_c.reshape(1, 512, 512, 1) predict_img = model_croping.predict(image_to_predict) mask = predict_img[0, :, :, 0] mask[mask > 0.5] = 1 mask[mask <= 0.5] = 0.35 mask = cv2.medianBlur(mask, 3) #mask = cv2.resize(mask, (224,224), interpolation = cv2.INTER_AREA) #img = skimage.morphology.remove_small_objects(img,min_size=10000, connectivity=8) mask = skimage.morphology.opening(mask, disk(10)) mask = skimage.morphology.dilation(mask, disk(20)) mask = cv2.resize(mask, (224, 224), interpolation=cv2.INTER_AREA) image_size = 224 image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA) #image = np.random.random((image_size, image_size, 3)) x_test = np.array(image) / 255 x_test = x_test.reshape(-1, image_size, image_size, 3) x_test = x_test.astype('float32') dim = (image_size, image_size) # resize image image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA) img_tensor = x_test conv_layer = model.get_layer('conv5_block3_2_conv') heatmap_model = tf.keras.models.Model( [model.inputs], [model.get_layer('conv5_block3_2_conv').output, model.output]) with tf.GradientTape() as tape: conv_output, predictions = heatmap_model(img_tensor) loss = predictions[:, np.argmax(predictions[0])] grads = tape.gradient(loss, conv_output) pooled_grads = K.mean(grads, axis=(0, 1, 2)) heatmap = tf.reduce_mean(tf.multiply(pooled_grads, conv_output), axis=-1) heatmap = np.maximum(heatmap, 0) max_heat = np.max(heatmap) if max_heat == 0: max_heat = 1e-10 heatmap /= max_heat heatmap = heatmap[0, :, :] heatmap = cv2.resize(heatmap, dim, interpolation=cv2.INTER_AREA) heatmap = 1 - heatmap result_image = image result_image[:, :, 1] = image[:, :, 1] * heatmap result_image[:, :, 2] = image[:, :, 1] * heatmap result_image[:, :, 0] = result_image[:, :, 0] * mask result_image[:, :, 1] = result_image[:, :, 1] * mask result_image[:, :, 2] = result_image[:, :, 2] * mask pil_im = Image.fromarray(result_image) if pil_im.mode != 'RGB': pil_im = pil_im.convert('RGB') buff = io.BytesIO() pil_im.save(buff, format="PNG") img_str = base64.b64encode(buff.getvalue()) return "" + str(img_str, 'utf-8')
def pred(data): decoded_data = base64.b64decode(data) np_data = np.fromstring(decoded_data, np.uint8) img = cv2.imdecode(np_data, cv2.IMREAD_UNCHANGED) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) image2 = cv2.equalizeHist(img_gray) image = cv2.medianBlur(image2, 3) scaled_image = image # cv2.resize(image, (1024,1024)) right_lung_hog_rectangle = lf.find_right_lung_hog(scaled_image) left_lung_hog_rectangle = lf.find_left_lung_hog(scaled_image) right_lung_lbp_rectangle = lf.find_right_lung_lbp(scaled_image) left_lung_lbp_rectangle = lf.find_left_lung_lbp(scaled_image) right_lung_haar_rectangle = lf.find_right_lung_haar(scaled_image) left_lung_haar_rectangle = lf.find_left_lung_haar(scaled_image) color_image = cv2.cvtColor(scaled_image, cv2.COLOR_GRAY2BGR) if (right_lung_hog_rectangle is not None) and (left_lung_hog_rectangle is not None): x1, y1, width1, height1 = right_lung_hog_rectangle x2, y2, width2, height2 = left_lung_hog_rectangle cimage1 = image2[np.minimum(y1, y2):np.maximum(y1 + height1, y2 + height2), x1:x2 + width2] pole1 = width1 * height1 + width2 * height2 if (right_lung_lbp_rectangle is not None) and (left_lung_lbp_rectangle is not None): x1, y1, width1, height1 = right_lung_lbp_rectangle x2, y2, width2, height2 = left_lung_lbp_rectangle cimage2 = image2[np.minimum(y1, y2):np.maximum(y1 + height1, y2 + height2), x1:x2 + width2] pole2 = width1 * height1 + width2 * height2 if (right_lung_haar_rectangle is not None) and (left_lung_haar_rectangle is not None): x1, y1, width1, height1 = right_lung_haar_rectangle x2, y2, width2, height2 = left_lung_haar_rectangle cimage3 = image2[np.minimum(y1, y2):np.maximum(y1 + height1, y2 + height2), x1:x2 + width2] pole3 = width1 * height1 + width2 * height2 if (pole1 >= pole2) and (pole1 >= pole3) and (pole1 > 0): cimage = cimage1 if (pole2 > pole1) and (pole2 >= pole3): cimage = cimage2 if (pole3 > pole1) and (pole3 > pole2): cimage = cimage3 if (cimage.shape[0] > 0) and (cimage.shape[1] > 0): image = cimage filename = join(dirname(__file__), "unet2_lung_seg_hist.hdf5") model_croping = keras.models.load_model(filename, custom_objects={ 'dice_coef_loss': dice_coef_loss, 'dice_coef': dice_coef }) filename = join(dirname(__file__), "resnet_cropped_CvHvP_detection.hdf5'") model = keras.models.load_model(filename) ## image_c = cimage image_c = cv2.resize(image_c, (512, 512), interpolation=cv2.INTER_AREA) image_c = np.array(image_c) / 255 image_c = image_c.astype('float32') image_to_predict = image_c.reshape(1, 512, 512, 1) predict_img = model_croping.predict(image_to_predict) mask = predict_img[0, :, :, 0] mask[mask > 0.5] = 1 mask[mask <= 0.5] = 0.35 mask = cv2.medianBlur(mask, 3) #mask = cv2.resize(mask, (224,224), interpolation = cv2.INTER_AREA) #img = skimage.morphology.remove_small_objects(img,min_size=10000, connectivity=8) mask = skimage.morphology.opening(mask, disk(10)) mask = skimage.morphology.dilation(mask, disk(20)) mask = cv2.resize(mask, (224, 224), interpolation=cv2.INTER_AREA) image_size = 224 image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA) #image = np.random.random((image_size, image_size, 3)) x_test = np.array(image) / 255 x_test = x_test.reshape(-1, image_size, image_size, 3) x_test = x_test.astype('float32') prediction = model.predict(x_test) pred = np.argmax(prediction, axis=1) probability = prediction.max() result = 'unknown' if pred[0] == 0: result = 'Covid_19' if pred[0] == 1: result = 'Healthy Lungs!' if pred[0] == 2: result = 'Pneumonia' probability = round(probability * 100, 2) a = (f'The pattient has {result} with probability of {probability} %') return a