# Change to calculate F1 score of detection accuracy path = "salmonella" imlist = [] gtlist = [] for i in range(1, 100): imlist.append((cv2.imread(path + str(i).zfill(3) + "cell.tif", cv2.IMREAD_GRAYSCALE))) gtlist.append((((cv2.imread(path + str(i).zfill(3) + "dots.png", cv2.IMREAD_GRAYSCALE))[:, :] > 0) * 100).astype(np.uint8)) DensityNet = CNN.Density() DensityNet.load_weights("weights/salmonella.h5") # Change Weights mean = np.mean(imlist) std = np.std(imlist) for i in range(len(imlist)): imgorigin = imlist[i] img = imlist[i] gt = gtlist[i] img = (img - mean) / std img = np.expand_dims(img, -1) img = np.expand_dims(img, 0) density = DensityNet.predict(img)
import os import tensorflow as tf import numpy as np import keras import scipy.ndimage as ndimage from keras import backend as K import cv2 import CNN def act(x): return (K.sigmoid(x) * x) keras.utils.generic_utils.get_custom_objects().update({'act': keras.layers.Activation(act)}) model = CNN.Density() # Convenience function to normalise def normalize(array): return (array-np.mean(array))/np.std(array) # Read Data def read_data(path): data1=[] anno1=[] for base_path in path: imList = os.listdir(base_path) for i in range(1,(len(imList)//2)+1): img1 = cv2.imread(os.path.join(base_path, str(i).zfill(3)+"cell.tif"), cv2.IMREAD_GRAYSCALE)
def main(path, weights): timelapse = Load(path) previous = None fps = cv2.VideoCapture(path).get(cv2.CAP_PROP_FPS) DensityNet = CNN.Density() DensityNet.load_weights("weights/"+weights+".h5") vid = cv2.VideoWriter(path[:-4] + "_Cross.mp4", cv2.VideoWriter_fourcc(*'mp4v'), fps, ((timelapse[0].shape[1]//4)*4, (timelapse[0].shape[0]//4)*4)) id = 0 trees = {} frameno=0 tag=1 if timelapse: mean = np.mean(timelapse) std = np.std(timelapse) for image in timelapse: frame = (image-mean)/std frame = np.expand_dims(frame, 0) frame = np.expand_dims(frame, -1) density = DensityNet.predict(frame) density = density.squeeze(axis=(0, -1)) count = int(np.sum(density)/100) # Calculate cell count # Scale output density map factor = 255.0/np.amax(density) bwdensity = (density * factor) colour = cv2.applyColorMap(bwdensity.astype(np.uint8), cv2.COLORMAP_JET) dot, cross, id = Centroid(density, image, count, previous, id, tag) vid.write(cross) if dot: previous = dot cv2.imshow("1", colour); cv2.imshow("2", cross); cv2.waitKey(0) # Add centroids to lineage tree for cell in dot: tree = cell[2].find(".") if tree == -1: tree = cell[2] else: tree = cell[2][:tree] if tree not in trees: trees[tree] = LineageTree.LineageTree(cell, frameno) tag+=1 else: trees[tree].insert(cell) frameno+=1 # Display lineage tree for i in trees: handle = io.StringIO(str(trees[i])) tree = Phylo.read(handle, "newick") ax = Phylo.draw(tree, do_show=False) ax.ticklabel_format(axis='x', useOffset=-trees[i].frameno) ax.set_xlabel('Frame Number') plt.show() return trees