def getSeenSPs(labels, myGaze, method=None): """ Finds coordinate of centroid and label index of region whose center of mass is closest to gaze-point: """ print("Getting label index of seen parts...") nLabels = np.max(labels[0]) nFrames = len(labels) seenCentroids = np.zeros((nLabels, nFrames)) seenLabel = np.zeros(nFrames) mask = np.zeros(labels[0].shape) thisCentroid = [] for i in range(nFrames): #iterate over frames distsToCentroid = [] #for j in range(nLabels+1): #iterate over labels for j in range(nLabels): #iterate over labels #print i,j,nLabels,nFrames mask = (labels[i] == j) thisCentroid = ndimage.measurements.center_of_mass(mask) ci, cj = gaze.gazeCoord2Pixel(myGaze[i, 3], myGaze[i, 4], mask.shape[1], mask.shape[0]) distsToCentroid.append( np.linalg.norm(np.array([ci, cj]) - thisCentroid)) seenCentroids[np.argmin(distsToCentroid), i] = 1 #seenLabel[i] = labels[i][np.argmin(distsToCentroid)] seenLabel[i] = np.argmin(distsToCentroid) return (seenCentroids, seenLabel)
def getMeanColorInCircle(img, gazeCoord, gaze_radius, normCst=1): ci, cj = gaze.gazeCoord2Pixel(gazeCoord[0], gazeCoord[1], img.shape[1], img.shape[0]) this_pix, _ = getValuesInCircle(img, (cj, ci), gaze_radius) this_mean = np.mean(this_pix, axis=0) / np.tile(normCst, 3) return this_mean, this_pix
def getCircleIdx(gazeCoord, shape, radius): ci, cj = gaze.gazeCoord2Pixel(gazeCoord[1], gazeCoord[0], shape[1], shape[0]) center = (ci, cj) y, x = np.ogrid[0:shape[0], 0:shape[1]] mask = (x - center[1])**2 + (y - center[0])**2 <= radius * radius return np.where(mask)
def getSeenSPsInRadius(labels, myGaze, gazeRadius): nLabels = np.max(labels[0]) nFrames = myGaze.shape[0] seenCentroids = np.zeros((nLabels, nFrames)) mask = np.zeros(labels[0].shape) thisCentroid = [] for i in range(nFrames): #iterate over labels for j in range(nLabels + 1): #iterate over frames mask = (labels[i] == j) thisCentroid = ndimage.measurements.center_of_mass(mask) ci, cj = gaze.gazeCoord2Pixel(myGaze[i, 3], myGaze[i, 4], mask.shape[1], mask.shape[0]) if np.linalg.norm(np.array([ci, cj]) - thisCentroid) <= gazeRadius: seenCentroids[j, i] = 1 return seenCentroids
def getSeenSPsInRect(labels, myGaze, w_norm, h_norm, center, frameIndSource, frameIndTarget): seenParts = [] nLabels = np.max(labels[0]) w = w_norm * labels.shape[1] h = h_norm * labels.shape[0] mask = np.zeros(labels[0].shape) ci, cj = gaze.gazeCoord2Pixel(myGaze[frameIndSource, 3], myGaze[frameIndSource, 4], mask.shape[1], mask.shape[0]) mask[int(ci - h / 2):int(ci + h / 2), int(cj - w / 2):int(cj + w / 2)] = 1 for j in range(nLabels + 1): #iterate over labels thisLabelMask = (labels[frameIndTarget] == j) thisCentroid = ndimage.measurements.center_of_mass(thisLabelMask) if mask[thisCentroid[0], thisCentroid[1]]: seenParts.append(j) return seenParts
def getSeenSPsIn(labels, myGaze): """ Finds coordinate of centroid and label index of region with gaze-point in """ print("Getting label index of seen parts...") nFrames = np.min((labels.shape[2], myGaze.shape[0])) seenLabel = np.zeros(nFrames) mask = np.zeros((labels.shape[0], labels.shape[1])) thisCentroid = [] for i in range(nFrames): #iterate over frames #for j in range(nLabels+1): #iterate over labels labelNums = np.unique(labels[:, :, i]) ci, cj = gaze.gazeCoord2Pixel(myGaze[i, 3], myGaze[i, 4], mask.shape[1], mask.shape[0]) for j in range(labelNums.shape[0]): #iterate over labels #print i,j,nFrames mask = (labels[:, :, i] == labelNums[j]) #thisCentroid = ndimage.measurements.center_of_mass(mask) if (mask[ci, cj]): break seenLabel[i] = labelNums[j] return (seenLabel)
def make_frame(idx, ref_idx, frames, labels, label_contours, gts, labels_clicked, mouse_clicks, mode='right', highlight_idx=None): if (mode == 'right'): the_idx = idx else: the_idx = ref_idx shape = labels[:, :, 0].shape colors_ = [ color_dict['red'], color_dict['green'], color_dict['blue'], color_dict['magenta'], color_dict['white'] ] mask = np.zeros(shape, dtype=np.uint8) img = utls.imread(frames[the_idx]) if (img.shape[2] > 3): img = img[:, :, 0:3] if (label_contours is not None): idx_contours = np.where(label_contours[the_idx, :, :]) img[idx_contours[0], idx_contours[1], :] = (255, 255, 255) l_ = (utls.imread(gts[the_idx]) > 0)[..., 0].astype(np.uint8) l_idx = np.where(l_) mask[l_idx[0], l_idx[1]] = 1 #Draw set of tracked labels if (mode == 'right'): labels_to_draw = [l[-1] for l in labels_clicked if (l[1] == idx)] else: print('labels_clicked: ' + str(labels_clicked)) print('idx: ' + str(idx)) labels_to_draw = [l[-1] for l in labels_clicked if (l[0] == ref_idx)] print('labels_to_draw: ' + str(labels_to_draw)) if (len(labels_to_draw) > 0): #print(self.curr_idx) for i in range(len(labels_to_draw)): this_label = labels_to_draw[i] mask_tmp = labels[:, :, the_idx] == this_label l_ = mask_tmp.astype(np.uint8) l_idx = np.where(l_) mask[l_idx[0], l_idx[1]] = 2 if (mode == 'right'): mouse_clicks_to_draw = [r for r in mouse_clicks if (r[1] == idx)] #print(mouse_clicks_to_draw) else: mouse_clicks_to_draw = [r for r in mouse_clicks if (r[0] == ref_idx)] #mouse_clicks_to_draw = mouse_clicks if (len(mouse_clicks_to_draw) > 0): for i in range(len(mouse_clicks_to_draw)): x = mouse_clicks_to_draw[i][-2] y = mouse_clicks_to_draw[i][-1] g_i, g_j = gaze.gazeCoord2Pixel(x, y, mask.shape[1], mask.shape[0]) rr, cc = circle(g_i, g_j, radius=7) mask[rr, cc] = 3 #mask = np.zeros(shape, dtype=np.uint8) #if(highlight_idx is not None): # mouse_clicks_to_draw = [r for r in mouse_clicks if(r[0] in highlight_idx)] # for i in range(len(mouse_clicks_to_draw)): # x = mouse_clicks_to_draw[i][3] # y = mouse_clicks_to_draw[i][4] # g_i, g_j = gaze.gazeCoord2Pixel(x, # y, # mask.shape[1], # mask.shape[0]) # rr, cc = circle(g_i, g_j, radius=7) # mask[rr, cc] = 4 img = color.label2rgb(mask, img, alpha=.2, bg_label=0, colors=colors_) img = np.rot90(img)[::-1, :, :] return img
def calc_training_patches(self, save=False): ps = self.conf.patch_size my_gaze = self.conf.myGaze_fg scale_factor = self.conf.scale_factor seen_patches = list() unseen_patches = list() selem = morphology.square(5) print('Getting seen patches') with progressbar.ProgressBar(maxval=len(self.conf.frameFileNames)) as bar: for i in range(len(self.conf.frameFileNames)): bar.update(i) img = utls.imread(self.conf.frameFileNames[i]) img = (color.rgb2gray(img)*255).astype(np.uint8) img = median(img, selem=selem) # Uses square of size 3 ci_seen, cj_seen = gaze.gazeCoord2Pixel( my_gaze[i, 3], my_gaze[i, 4], img.shape[1], img.shape[0]) i_, j_ = self.get_centers_negatives(ci_seen, cj_seen, ps, self.conf.overlap_ratio, img.shape) img_padded = pad(img,((ps,ps),),mode='symmetric') seen_patch = img_padded[int(ci_seen + ps/2):int(ci_seen + 3*ps/2),int(cj_seen + ps/2):int(cj_seen + 3*ps/2)] seen_patch_mean = np.mean(seen_patch) seen_patch_std = np.std(seen_patch) if(seen_patch_std == 0): seen_patch_std = 1 seen_patch = (seen_patch - seen_patch_mean) / seen_patch_std seen_patch = rescale( seen_patch, scale=scale_factor, order=1, mode='reflect', preserve_range=True) seen_patches.append((i, ci_seen, cj_seen, seen_patch.ravel())) for k in range(i_.shape[0]): unseen_patch = img[int(i_[k]- ps / 2):int(i_[k] + ps / 2), int( j_[k]- ps / 2):int(j_[k]+ ps / 2)] unseen_patch_mean = np.mean(unseen_patch) unseen_patch_std = np.std(unseen_patch) if(unseen_patch_std == 0): unseen_patch_std = 1 unseen_patch = (unseen_patch - unseen_patch_mean) / unseen_patch_std unseen_patch = rescale( unseen_patch, scale=scale_factor, order=1, mode='reflect', preserve_range=True) unseen_patches.append((i, i_[k], j_[k], unseen_patch.ravel())) if(save): seen_patches_df = pd.DataFrame(seen_patches, columns=['frame', 'c_i', 'c_j', 'patch']) unseen_patches_df = pd.DataFrame(unseen_patches, columns=['frame', 'c_i', 'c_j', 'patch']) save_path = os.path.join(self.conf.dataOutDir, 'vilar') if(not os.path.exists(save_path)): os.mkdir(save_path) seen_patches_df.to_pickle(os.path.join(save_path, 'vilar_seen_patches_df.p')) unseen_patches_df.to_pickle( os.path.join(save_path, 'vilar_unseen_patches_df.p')) return True
def getSeenColorsInCircle(img, gazeCoord, gaze_radius, standardization=None): ci, cj = gaze.gazeCoord2Pixel(gazeCoord[i, 3], gazeCoord[i, 4], imgShape[1], imgShape[0]) this_ = getValuesInCircle(img, (cj, ci), gaze_radius)