class SegmentationEvaluator(object): def __init__(self): self.System_Masks = [] self.GT_Masks = [] self.gt_db = None self.system_db = None self.specificity = {} self.true_negative_rate = self.specificity self.sensitivity = {} self.true_positive_rate = self.sensitivity self.precision = {} self.positive_predictive_value = self.precision self.negative_predictive_value = {} self.false_positive_rate = {} self.fall_out = self.false_positive_rate self.false_negative_rate = {} self.false_discovery_rate = {} self.accuracy = {} self.F1_score = {} self.MCC = {} self.informedness = {} self.markedness = {} self.positive_rate = {} self.LeeLiu_rate = {} def load_GT_masks_from_clickpoints(self, path): self.gt_db = DataFileExtended(path) # self.GT_Masks.extend([str(m.image.timestamp) for m in self.gt_db.getMasks()]) timestamps = self.gt_db.db.execute_sql( "select image.timestamp from image inner join mask on mask.image_id=image.id where mask.data is not null" ).fetchall() self.GT_Masks.extend([str(t[0]) for t in timestamps]) # self.GT_Masks.extend(set([str(m.image.timestamp) for m in self.gt_db.getMasks()]).update(set(self.GT_Masks))) def load_System_masks_from_clickpoints(self, path): self.system_db = DataFileExtended(path) # self.System_Masks.extend([str(m.image.timestamp) for m in self.system_db.getMasks()]) timestamps = self.system_db.db.execute_sql( "select image.timestamp from image inner join mask on mask.image_id=image.id where mask.data is not null" ).fetchall() self.System_Masks.extend([str(t[0]) for t in timestamps]) # for im in self.system_db.getImages(): # if im.mask is not None: # print(im.timestamp) # self.System_Masks.append(str(im.timestamp)) # self.System_Masks.extend(set([str(m.image.timestamp) for m in self.system_db.getMasks()]).update(set(self.System_Masks_Masks))) def match(self, gt_inverse=True, system_inverse=True): stamps = set(self.GT_Masks).intersection(self.System_Masks) for stamp in stamps: if system_inverse: sm = ~self.system_db.getMask(image=self.system_db.getImages( timestamp=stamp)[0]).data.astype(bool) else: sm = self.system_db.getMask(image=self.system_db.getImages( timestamp=stamp)[0]).data.astype(bool) if gt_inverse: gt = ~self.gt_db.getMask(image=self.gt_db.getImages( timestamp=stamp)[0]).data.astype(bool) else: gt = self.gt_db.getMask(image=self.gt_db.getImages( timestamp=stamp)[0]).data.astype(bool) P = np.sum(gt).astype(float) TP = np.sum(sm & gt).astype(float) FP = np.sum(sm & (~gt)).astype(float) N = np.sum(~gt).astype(float) TN = np.sum((~sm) & (~gt)).astype(float) FN = np.sum((~sm) & gt).astype(float) self.specificity.update({stamp: TN / N}) self.sensitivity.update({stamp: TP / P}) self.precision.update({stamp: TP / (TP + FP)}) self.negative_predictive_value.update({stamp: TN / (TN + FN)}) self.false_positive_rate.update({stamp: FP / N}) self.false_negative_rate.update({stamp: FN / (TP + FN)}) self.false_discovery_rate.update({stamp: FP / (TP + FP)}) self.accuracy.update({stamp: (TP + TN) / (TP + FN + TN + FP)}) self.F1_score.update({stamp: 2 * TP / (2 * TP + FP + FN)}) self.MCC.update({ stamp: (TP * TN - FP * FN) / ((TP + FP) * (TP + FN) * (TN + FP) * (TN + FN)) }) self.informedness.update({stamp: TP / P + TN / N - 1}) self.markedness.update( {stamp: TP / (TP + FP) + TN / (TN + FN) - 1}) self.positive_rate.update( {stamp: (TP + FP) / (sm.shape[0] * sm.shape[1])}) self.LeeLiu_rate.update({ stamp: (TP / P)**2 / ((TP + FP) / (sm.shape[0] * sm.shape[1])) })
# Init_Background from Image_Median # Initialize segmentation with init_image and start updating the first 10 frames. init_buffer = [] for i in range(2): while True: img, meta = cam.getNewestImage() if img is not None: print("Got img from cam") init_buffer.append(img) print(init_buffer[-1].shape) print(init_buffer[-1].dtype) break # init = np.array(np.median([init_buffer], axis=0)) NoMask = db_start.getMask(frame=0).data.astype(bool) # VB = ViBeSegmentation(n=3, init_image=np.array(np.median(init_buffer, axis=0)), n_min=3, r=30 , phi=1) #Starter # VB = ViBeSegmentation(n=3, init_image=np.array(np.median(init_buffer, axis=0)), n_min=3, r=30 , phi=1) #Starter2 # VB = ViBeSegmentation(n=8, init_image=np.array(np.median(init_buffer, axis=0)), n_min=8, r=50 , phi=1) #Starter3 # VB = ViBeSegmentation(n=3, init_image=np.array(np.median(init_buffer, axis=0)), n_min=3, r=75 , phi=1) #Starter4 - VIS # VB.Samples[0] = np.amax(init_buffer, axis=0) # VB.Samples[-1] = np.amin(init_buffer, axis=0) # import matplotlib.pyplot as plt # plt.figure() # plt.imshow(np.amax(VB.Samples, axis=0)) # plt.figure() # plt.imshow(np.amin(VB.Samples, axis=0)) # plt.show()
# Segmentation step SegMap = VB.detect(image.data, do_neighbours=False) print(time()-start) start = time() # print(SegMap.shape) # print(image.data.shape) # Setting Mask in ClickPoints db.setMask(image=image, data=(PT_Mask_Type.index*(~SegMap).astype(np.uint8))) print("Mask save") SegMap = db.getMask(image=image).data Mask = ~SegMap.astype(bool) Positions = AD.detect(Mask) X = np.asarray([[pos.PositionX, pos.PositionY] for pos in Positions]) Positions = [pos for pos in Positions if np.sum(((pos.PositionX-X.T[0])**2+(pos.PositionY-X.T[1])**2)**0.5 < 200) < 10] print("Found %s animals!"%len(Positions)) if len(Positions)==0: continue # for pos1 in Positions: # a = float(pos1.Log_Probability) # dists = [np.linalg.norm([pos1.PositionX-pos2.PositionX, # pos1.PositionY - pos2.PositionY]) for pos2 in Positions] # pos1.Log_Probability -= np.log(np.mean(dists)) # print(str(a), str(pos1.Log_Probability), str(a-pos1.Log_Probability))