def updateResultsButton(self, dt=None): try: # todo: contact dropbox local_recon = '/home/pi/recon' for local_file in os.listdir(local_recon): if local_file.endswith('.raw.log.iphone.log'): btn = Button(text=local_file, size_hint_y=None, height=44) btn.bind(on_release=lambda btn: self._show_carousel(btn.text)) self._dropdown.add_widget(btn) except: # do nothing print('error')
def __init__(self, frames, func, **kwargs): super().__init__(**kwargs) self.frame_slider = Slider(min=0, max=frames - 1, value=1, size_hint=(.7, .6), pos_hint={ 'x': .15, 'y': .4 }) self.frame_slider.bind(value=partial(self.change_frame, func)) self.frame_text = ntlabel(text='Frame: ' + str(0), size_hint=(.6, .4), pos_hint={ 'x': .2, 'y': 0 }, style=2) self.frame_minus = Button(text='<<', size_hint=(.15, .6), pos_hint={ 'x': .0, 'y': .4 }, markup=True) self.frame_plus = Button(text='>>', size_hint=(.15, .6), pos_hint={ 'x': .85, 'y': .4 }, markup=True) self.frame_minus.bind(on_press=self.frame_backward) self.frame_plus.bind(on_press=self.frame_forward) self.frames = frames self.slider_layout = FloatLayout(size=(500, 500), pos=self.pos) self.slider_layout.add_widget(self.frame_slider) self.slider_layout.add_widget(self.frame_text) self.slider_layout.add_widget(self.frame_minus) self.slider_layout.add_widget(self.frame_plus) with self.canvas: self.add_widget(self.slider_layout) self.bind(pos=self.update_size, size=self.update_size ) # Maintain image size on scaling of parent layout
def __init__(self, features, track_param, frames, **kwargs): super().__init__(**kwargs) self.tracking_object = trackcells.TrackCells(features=features, track_param=track_param, frames=frames) self.track_message = Label( text='[b][color=000000] Tracking cells [/b][/color]', markup=True, size_hint=(.2, .05), pos_hint={ 'x': .4, 'y': .65 }) self.track_counter = Label(text='[b][color=000000] [/b][/color]', markup=True, size_hint=(.2, .05), pos_hint={ 'x': .4, 'y': .6 }) self.cancel_btn = Button(text='Cancel', markup=True, size_hint=(.2, .05), pos_hint={ 'x': .4, 'y': .5 }) self.cancel_btn.bind(on_release=self.cancel_tracking) self.layout = FloatLayout(size=(Window.width, Window.height)) self.add_flag = True self.optimise_flag = False self.count = 0 self.optimise_count = 0 self.sweep = 0 self.cancel_flag = False with self.canvas: self.add_widget(self.layout) self.layout.add_widget(self.track_counter) self.layout.add_widget(self.track_message) self.layout.add_widget(self.cancel_btn)
def add_btn(layout, text, func, group=None): btn = Button(text=text, group=group, halign='center', valign='middle') btn.bind(on_press=func) btn.bind(size=btn.setter('text_size')) layout.add_widget(btn) return
def __init__(self, movie, labels, tracks, stored_tracks, features, **kwargs): super().__init__(**kwargs) self.movie = movie self.tracks = tracks self.features = features self.labels = labels self.keyboard = Window.request_keyboard(self.keyboard_closed, self) self.keyboard.bind(on_key_down=self.key_print) self.tr_layout = FloatLayout(size=(Window.width, Window.height)) # Stored Tracks self.store_ids = set() for i in range(len(stored_tracks)): if stored_tracks[i] == 1: self.store_ids = self.store_ids.union( set(self.tracks[self.tracks[:, 4] == i, 0])) self.store_layout = FloatLayout(size=(Window.width, Window.height)) self.track_disp = IndexedDisplay(size_hint=(.43, .43), pos_hint={ 'x': .12, 'y': .46 }) self.tr_layout.add_widget(self.track_disp) self.mov_disp = ImDisplay(size_hint=(.43, .43), pos_hint={ 'x': .56, 'y': .46 }) self.tr_layout.add_widget(self.mov_disp) self.track_ids = np.zeros(1) self.current_frame = 0 self.channel = 0 self.mov_disp.create_im( self.movie.read_im(self.channel, self.current_frame), 'PastelHeat') inds = self.features['tracking'][:, 1] mask = np.equal(inds, 0) if any(mask): self.frame_feats = self.features['tracking'][mask, :] im_temp = self.labels[0, :, :] mapping = self.features['tracking'][:, 11].astype(int) self.track_disp.create_im(im_temp, 'Random', mapping) self.frame_slider = guitools.FrameSlider(self.movie.frames, self.change_frame, size_hint=(.39, .06), pos_hint={ 'x': .05, 'y': .91 }) self.tr_layout.add_widget(self.frame_slider) self.tracking_window = TrackingData(size_hint=(.43, .43), pos_hint={ 'x': .12, 'y': .46 }) self.jump_window = Jump(size_hint=(.87, .3), pos_hint={ 'x': .12, 'y': .12 }) layout4 = GridLayout(cols=1, padding=2, size_hint=(.1, .78), pos_hint={ 'x': .01, 'y': .115 }) self.cell_mark = CellMark(size_hint=(.43, .43), pos_hint={ 'x': .12, 'y': .46 }) self.cell_mark_2 = CellMark(size_hint=(.43, .43), pos_hint={ 'x': .56, 'y': .46 }) self.track_btn1 = ToggleButton(text=' Select Cell (z) ', markup=True, halign='center', valign='middle') self.track_btn2 = ToggleButton(text=' Add Segment (c)', markup=True, halign='center', valign='middle') self.track_btn3 = ToggleButton(text='Remove Segment(v)', markup=True, halign='center', valign='middle') self.track_btn4 = ToggleButton(text='Swap Tracks (x)', markup=True, halign='center', valign='middle') self.track_btn5 = ToggleButton(text='Jump (w)', markup=True, halign='center', valign='middle') self.track_btn6 = ToggleButton(text='New Track (n)', markup=True, halign='center', valign='middle') self.track_btn9 = Button(text='Store Track', markup=True, halign='center', valign='middle') self.track_btn10 = Button(text='Export All to CSV', markup=True, halign='center', valign='middle') self.track_btn11 = Button(text='Export Sel to CSV', markup=True, halign='center', valign='middle') self.track_btn1.bind( on_press=partial(self.tracking_window.state_change, state=1)) self.track_btn2.bind( on_press=partial(self.tracking_window.state_change, state=2)) self.track_btn3.bind( on_press=partial(self.tracking_window.state_change, state=3)) self.track_btn4.bind( on_press=partial(self.tracking_window.state_change, state=4)) self.track_btn5.bind(on_press=partial(self.jump_window.jump, flag=0)) self.track_btn6.bind( on_press=partial(self.tracking_window.state_change, state=5)) self.track_btn9.bind(on_press=self.store_track) self.track_btn10.bind(on_press=self.save_csv) self.track_btn11.bind(on_press=self.save_sel_csv) layout4.add_widget(self.track_btn1) layout4.add_widget(self.track_btn2) layout4.add_widget(self.track_btn3) layout4.add_widget(self.track_btn4) layout4.add_widget(self.track_btn6) layout4.add_widget(self.track_btn5) layout4.add_widget(self.track_btn9) layout4.add_widget(self.track_btn10) layout4.add_widget(self.track_btn11) for child in layout4.children: child.bind(size=child.setter('text_size')) self.layout4 = layout4 self.show_feat = [0, 1, 2] self.graph = GraphTrack(self.movie.frames, size_hint=(.87, .32), pos_hint={ 'x': .12, 'y': .12 }) self.graph_buttons = GridLayout(cols=4, spacing=[2, 2], size_hint=(.5, .08), pos_hint={ 'x': .47, 'y': .9 }) self.text_input1 = TextInput(text='1', multiline=False) self.text_input2 = TextInput(text='2', multiline=False) self.text_input3 = TextInput(text='3', multiline=False) self.text_input1.bind(on_text_validate=partial(self.feat_change, 0)) self.text_input2.bind(on_text_validate=partial(self.feat_change, 1)) self.text_input3.bind(on_text_validate=partial(self.feat_change, 2)) self.graph_buttons.add_widget(self.text_input1) self.graph_buttons.add_widget(self.text_input2) self.graph_buttons.add_widget(self.text_input3) self.clear_button = Button(text=' Clear Events ') self.clear_button.bind(on_press=self.clear_events) self.graph_buttons.add_widget(self.clear_button) self.event_flag1 = ToggleButton(text='Event 1') self.event_flag2 = ToggleButton(text='Event 2') self.event_flag3 = ToggleButton(text='Event 3') self.event_flag1.bind(on_press=partial(self.jump_window.jump, flag=1)) self.event_flag2.bind(on_press=partial(self.jump_window.jump, flag=2)) self.event_flag3.bind(on_press=partial(self.jump_window.jump, flag=3)) self.graph_buttons.add_widget(self.event_flag1) self.graph_buttons.add_widget(self.event_flag2) self.graph_buttons.add_widget(self.event_flag3) # Drop down menu for choosing which channel self.channel_choice = DropDown() for i in range(self.movie.channels): channel_btn = ToggleButton(text='Channel ' + str(i + 1), group='channel', size_hint_y=None) channel_btn.bind(on_press=partial(self.change_channel, i)) self.channel_choice.add_widget(channel_btn) self.main_button = Button(text=' Channel ') self.main_button.bind(on_release=self.channel_choice.open) self.channel_choice.bind( on_select=lambda instance, x: setattr(self.main_button, 'text', x)) self.graph_buttons.add_widget(self.main_button) mask = self.tracks[:, 0] == self.tracks[ 0, 0] # Test if selection is in track array and identify it self.track_ind = self.tracks[mask, 4] # Set the selected track index with self.canvas: self.add_widget(self.tr_layout) self.add_widget(self.store_layout) self.tr_layout.add_widget(self.layout4) self.tr_layout.add_widget(self.tracking_window) self.tr_layout.add_widget(self.jump_window) self.tr_layout.add_widget(self.cell_mark) self.tr_layout.add_widget(self.cell_mark_2) self.tr_layout.add_widget(self.graph) self.tr_layout.add_widget(self.graph_buttons) for i in range(len(stored_tracks)): self.store_layout.add_widget( CellMark(size_hint=(.43, .43), pos_hint={ 'x': .12, 'y': .46 }))
class TrackingUI(Widget): def __init__(self, movie, labels, tracks, stored_tracks, features, **kwargs): super().__init__(**kwargs) self.movie = movie self.tracks = tracks self.features = features self.labels = labels self.keyboard = Window.request_keyboard(self.keyboard_closed, self) self.keyboard.bind(on_key_down=self.key_print) self.tr_layout = FloatLayout(size=(Window.width, Window.height)) # Stored Tracks self.store_ids = set() for i in range(len(stored_tracks)): if stored_tracks[i] == 1: self.store_ids = self.store_ids.union( set(self.tracks[self.tracks[:, 4] == i, 0])) self.store_layout = FloatLayout(size=(Window.width, Window.height)) self.track_disp = IndexedDisplay(size_hint=(.43, .43), pos_hint={ 'x': .12, 'y': .46 }) self.tr_layout.add_widget(self.track_disp) self.mov_disp = ImDisplay(size_hint=(.43, .43), pos_hint={ 'x': .56, 'y': .46 }) self.tr_layout.add_widget(self.mov_disp) self.track_ids = np.zeros(1) self.current_frame = 0 self.channel = 0 self.mov_disp.create_im( self.movie.read_im(self.channel, self.current_frame), 'PastelHeat') inds = self.features['tracking'][:, 1] mask = np.equal(inds, 0) if any(mask): self.frame_feats = self.features['tracking'][mask, :] im_temp = self.labels[0, :, :] mapping = self.features['tracking'][:, 11].astype(int) self.track_disp.create_im(im_temp, 'Random', mapping) self.frame_slider = guitools.FrameSlider(self.movie.frames, self.change_frame, size_hint=(.39, .06), pos_hint={ 'x': .05, 'y': .91 }) self.tr_layout.add_widget(self.frame_slider) self.tracking_window = TrackingData(size_hint=(.43, .43), pos_hint={ 'x': .12, 'y': .46 }) self.jump_window = Jump(size_hint=(.87, .3), pos_hint={ 'x': .12, 'y': .12 }) layout4 = GridLayout(cols=1, padding=2, size_hint=(.1, .78), pos_hint={ 'x': .01, 'y': .115 }) self.cell_mark = CellMark(size_hint=(.43, .43), pos_hint={ 'x': .12, 'y': .46 }) self.cell_mark_2 = CellMark(size_hint=(.43, .43), pos_hint={ 'x': .56, 'y': .46 }) self.track_btn1 = ToggleButton(text=' Select Cell (z) ', markup=True, halign='center', valign='middle') self.track_btn2 = ToggleButton(text=' Add Segment (c)', markup=True, halign='center', valign='middle') self.track_btn3 = ToggleButton(text='Remove Segment(v)', markup=True, halign='center', valign='middle') self.track_btn4 = ToggleButton(text='Swap Tracks (x)', markup=True, halign='center', valign='middle') self.track_btn5 = ToggleButton(text='Jump (w)', markup=True, halign='center', valign='middle') self.track_btn6 = ToggleButton(text='New Track (n)', markup=True, halign='center', valign='middle') self.track_btn9 = Button(text='Store Track', markup=True, halign='center', valign='middle') self.track_btn10 = Button(text='Export All to CSV', markup=True, halign='center', valign='middle') self.track_btn11 = Button(text='Export Sel to CSV', markup=True, halign='center', valign='middle') self.track_btn1.bind( on_press=partial(self.tracking_window.state_change, state=1)) self.track_btn2.bind( on_press=partial(self.tracking_window.state_change, state=2)) self.track_btn3.bind( on_press=partial(self.tracking_window.state_change, state=3)) self.track_btn4.bind( on_press=partial(self.tracking_window.state_change, state=4)) self.track_btn5.bind(on_press=partial(self.jump_window.jump, flag=0)) self.track_btn6.bind( on_press=partial(self.tracking_window.state_change, state=5)) self.track_btn9.bind(on_press=self.store_track) self.track_btn10.bind(on_press=self.save_csv) self.track_btn11.bind(on_press=self.save_sel_csv) layout4.add_widget(self.track_btn1) layout4.add_widget(self.track_btn2) layout4.add_widget(self.track_btn3) layout4.add_widget(self.track_btn4) layout4.add_widget(self.track_btn6) layout4.add_widget(self.track_btn5) layout4.add_widget(self.track_btn9) layout4.add_widget(self.track_btn10) layout4.add_widget(self.track_btn11) for child in layout4.children: child.bind(size=child.setter('text_size')) self.layout4 = layout4 self.show_feat = [0, 1, 2] self.graph = GraphTrack(self.movie.frames, size_hint=(.87, .32), pos_hint={ 'x': .12, 'y': .12 }) self.graph_buttons = GridLayout(cols=4, spacing=[2, 2], size_hint=(.5, .08), pos_hint={ 'x': .47, 'y': .9 }) self.text_input1 = TextInput(text='1', multiline=False) self.text_input2 = TextInput(text='2', multiline=False) self.text_input3 = TextInput(text='3', multiline=False) self.text_input1.bind(on_text_validate=partial(self.feat_change, 0)) self.text_input2.bind(on_text_validate=partial(self.feat_change, 1)) self.text_input3.bind(on_text_validate=partial(self.feat_change, 2)) self.graph_buttons.add_widget(self.text_input1) self.graph_buttons.add_widget(self.text_input2) self.graph_buttons.add_widget(self.text_input3) self.clear_button = Button(text=' Clear Events ') self.clear_button.bind(on_press=self.clear_events) self.graph_buttons.add_widget(self.clear_button) self.event_flag1 = ToggleButton(text='Event 1') self.event_flag2 = ToggleButton(text='Event 2') self.event_flag3 = ToggleButton(text='Event 3') self.event_flag1.bind(on_press=partial(self.jump_window.jump, flag=1)) self.event_flag2.bind(on_press=partial(self.jump_window.jump, flag=2)) self.event_flag3.bind(on_press=partial(self.jump_window.jump, flag=3)) self.graph_buttons.add_widget(self.event_flag1) self.graph_buttons.add_widget(self.event_flag2) self.graph_buttons.add_widget(self.event_flag3) # Drop down menu for choosing which channel self.channel_choice = DropDown() for i in range(self.movie.channels): channel_btn = ToggleButton(text='Channel ' + str(i + 1), group='channel', size_hint_y=None) channel_btn.bind(on_press=partial(self.change_channel, i)) self.channel_choice.add_widget(channel_btn) self.main_button = Button(text=' Channel ') self.main_button.bind(on_release=self.channel_choice.open) self.channel_choice.bind( on_select=lambda instance, x: setattr(self.main_button, 'text', x)) self.graph_buttons.add_widget(self.main_button) mask = self.tracks[:, 0] == self.tracks[ 0, 0] # Test if selection is in track array and identify it self.track_ind = self.tracks[mask, 4] # Set the selected track index with self.canvas: self.add_widget(self.tr_layout) self.add_widget(self.store_layout) self.tr_layout.add_widget(self.layout4) self.tr_layout.add_widget(self.tracking_window) self.tr_layout.add_widget(self.jump_window) self.tr_layout.add_widget(self.cell_mark) self.tr_layout.add_widget(self.cell_mark_2) self.tr_layout.add_widget(self.graph) self.tr_layout.add_widget(self.graph_buttons) for i in range(len(stored_tracks)): self.store_layout.add_widget( CellMark(size_hint=(.43, .43), pos_hint={ 'x': .12, 'y': .46 })) def track_frame_update(self): if self.track_ids.any: self.cell_mark.remove_dot() self.cell_mark_2.remove_dot() mask = np.intersect1d(self.track_ids, self.frame_feats[:, 0]) if mask: cell_center = self.frame_feats[self.frame_feats[:, 0] == mask[0], [2, 3]] self.cell_mark.draw_dot(cell_center.copy(), self.movie.dims, 1., 1., 1., 50) self.cell_mark_2.draw_dot(cell_center.copy(), self.movie.dims, 1., 0., 0., 70) for i in range(len(self.parent.fov['tracks_stored'])): self.store_layout.children[i].remove_dot() count = 0 if len(self.store_ids) > 0: mask2 = self.store_ids.intersection(set(self.frame_feats[:, 0])) if len(mask2) > 0: for i in mask2: cell_center = self.frame_feats[self.frame_feats[:, 0] == i, [2, 3]] self.store_layout.children[count].draw_dot( cell_center, self.movie.dims, 0., 0., 0., 70) count += 1 def change_frame(self, val): if val >= self.movie.frames: val = self.movie.frames - 1 if val < 0: val = 0 if self.track_btn5.state == 'down': self.track_btn5.state = 'normal' val = int(val * self.movie.frames) self.current_frame = int(val) im_temp = self.labels[int(val), :, :] self.frame_slider.frame_slider.value = val mapping = self.features['tracking'][:, 11].astype(int) self.track_disp.update_im(im_temp.astype(float), mapping) self.mov_disp.update_im( self.movie.read_im(self.channel, self.current_frame)) inds = self.features['tracking'][:, 1] mask = inds == self.current_frame self.frame_feats = self.features['tracking'][mask, :] self.track_frame_update() self.graph.update_frame(self.current_frame) def clear_events(self, instance): mask2 = self.tracks[:, 4] == self.track_ind[0] self.track_ids = self.tracks[mask2, 0].astype(int) self.features['tracking'][self.track_ids, 12] = 0 self.modify_update() def add_event(self, xpos, val): frame = np.round(self.movie.frames * xpos) mask2 = self.tracks[:, 4] == self.track_ind[0] self.track_ids = self.tracks[mask2, 0].astype(int) time_points = self.features['tracking'][self.track_ids, 1] event_ind = np.argmin(np.abs(time_points - frame)) event_ind = self.track_ids[event_ind] if val == 1: self.event_flag1.state = 'normal' self.features['tracking'][event_ind, 12] = 1 if val == 2: self.event_flag2.state = 'normal' self.features['tracking'][event_ind, 12] = 2 if val == 3: self.event_flag3.state = 'normal' self.features['tracking'][event_ind, 12] = 3 self.modify_update() def modify_update(self): im_temp = self.labels[self.current_frame, :, :] mapping = self.features['tracking'][:, 11].astype(int) self.track_disp.update_im(im_temp.astype(float), mapping) self.canvas.ask_update() mask2 = self.tracks[:, 4] == self.track_ind[0] self.track_ids = self.tracks[mask2, 0].astype(int) self.track_frame_update() self.map_ind = self.features['tracking'][self.track_ids[0], 11] self.graph.update_graph(self.features, self.track_ids, self.show_feat) def track_amend(self, pos, flag): mod_flag = False # Calculate nearest segment if 0 < flag <= 5: pos = np.asarray( [[pos[0] * self.movie.dims[1], pos[1] * self.movie.dims[0]]]) d = distance.cdist(self.frame_feats[:, [2, 3]], pos) # Calculate distance from frame segments sel = self.frame_feats[np.argmin( d), :] # Choose closest segment to mouse click mask = np.equal( self.tracks[:, 0], sel[0]) # Test if selection is in track array and identify it # Select Cell if flag == 1: self.track_btn1.state = 'normal' if np.any(mask) and np.min(d) < 50: self.track_ind = self.tracks[ mask, 4] # Set the selected track index self.modify_update( ) # Display this as cell marked with black dot # Add segment to cell track if flag == 2: self.track_btn2.state = 'normal' if not np.any(mask) and np.min(d) < 50: frame_ids = self.tracks[ self.tracks[:, 5] == self.current_frame, :] # Get all cell tracks in frame mask2 = np.equal( frame_ids[:, 4], self.track_ind ) # Test if selected track already has segment in frame if np.any(mask2): feat_id = frame_ids[ mask2, 0] # get unique id of segment in frame self.features['tracking'][ self.features['tracking'][:, 0] == feat_id, 11] = 1 self.features['tracking'][ self.features['tracking'][:, 0] == sel[0], 11] = self.map_ind self.tracks[self.tracks[:, 0] == feat_id, 0] = sel[0] else: # where to insert the track i = 0 while self.tracks[i, 4] != self.track_ind: i += 1 else: while self.tracks[ i, 5] < self.current_frame and i + 1 < self.tracks.shape[ 0]: i += 1 if i + 1 == self.tracks.shape[0]: self.tracks = np.vstack((self.tracks, [ sel[0], 0, 0, 0, self.track_ind, self.current_frame, 0, 0 ])) self.features['tracking'][ self.features['tracking'][:, 0] == sel[0], 11] = self.map_ind else: self.tracks = np.insert( self.tracks, i, [ sel[0], 0, 0, 0, self.track_ind, self.current_frame, 0, 0 ], 0) self.features['tracking'][ self.features['tracking'][:, 0] == sel[0], 11] = self.map_ind mod_flag = True self.modify_update() # Remove segment from cell track if flag == 3: self.track_btn3.state = 'normal' if np.any(mask) and np.min(d) < 50: self.features['tracking'][ self.features['tracking'][:, 0] == sel[0], 11] = 1 ind = np.where(mask) self.tracks = np.delete(self.tracks, ind[0][0], 0) mod_flag = True self.modify_update() # Swap tracks in proceeding frames if flag == 4: self.track_btn4.state = 'normal' if np.any(mask) and np.min( d ) < 50 and self.current_frame != 0 and self.current_frame != self.movie.frames: # rows of selected track proceeding frame sel_mask = self.tracks[:, 4] == self.track_ind[0] sel_track = self.tracks[sel_mask, :] # rows of track to swap proceeding frame swap_ind = self.tracks[mask, 4] if swap_ind == self.track_ind[0]: return swap_mask = self.tracks[:, 4] == swap_ind if not np.count_nonzero(swap_mask): return swap_track = self.tracks[swap_mask, :] self.tracks = self.tracks[ np.logical_not(np.logical_or(sel_mask, swap_mask)), :] # perform swap sel1 = sel_track[sel_track[:, 5] < self.current_frame, :] sel2 = sel_track[sel_track[:, 5] >= self.current_frame, :] swap1 = swap_track[swap_track[:, 5] < self.current_frame, :] swap2 = swap_track[swap_track[:, 5] >= self.current_frame, :] swapped_1 = np.zeros(1) swapped_2 = np.zeros(1) if np.count_nonzero(sel1) and np.count_nonzero(swap2): swapped_1 = np.vstack((sel1, swap2)) else: if np.count_nonzero(sel1): swapped_1 = sel1 if np.count_nonzero(swap2): swapped_1 = swap2 if np.count_nonzero(swap1) and np.count_nonzero(sel2): swapped_2 = np.vstack((swap1, sel2)) else: if np.count_nonzero(swap1): swapped_2 = swap1 if np.count_nonzero(sel2): swapped_2 = sel2 if np.count_nonzero(swapped_1): if np.count_nonzero(sel1): swapped_1[:, 4] = sel1[0, 4] if np.count_nonzero(sel2): swapped_1[:, 4] = sel2[0, 4] if np.count_nonzero(swapped_2): if np.count_nonzero(swap1): swapped_2[:, 4] = swap1[0, 4] if np.count_nonzero(swap2): swapped_2[:, 4] = swap2[0, 4] # update labels if np.count_nonzero(swapped_1): self.tracks = np.vstack((self.tracks, swapped_1)) map_ind1 = self.features['tracking'][int(swapped_1[0, 0]), 11] self.features['tracking'][swapped_1[:, 0].astype(int), 11] = map_ind1 if np.count_nonzero(swapped_2): self.tracks = np.vstack((self.tracks, swapped_2)) map_ind2 = self.features['tracking'][int(swapped_2[0, 0]), 11] self.features['tracking'][swapped_2[:, 0].astype(int), 11] = map_ind2 mod_flag = True self.modify_update() # Create new track if flag == 5: self.track_btn6.state = 'normal' if not np.any(mask) and np.min(d) < 50: # Create new track self.track_ind = np.asarray([max(self.tracks[:, 4]) + 1]) r = 252 * np.random.rand() + 3 self.map_ind = r.astype(int) self.tracks = np.vstack((self.tracks, [ sel[0], 0, 0, 0, self.track_ind, self.current_frame, 0, 0 ])) self.features['tracking'][ self.features['tracking'][:, 0] == sel[0], 11] = r self.modify_update() stored_temp = self.parent.fov['tracks_stored'][...] stored_temp = np.append(stored_temp, 0) del self.parent.fov['tracks_stored'] self.parent.fov.create_dataset("tracks_stored", data=stored_temp) self.store_layout.add_widget( CellMark(size_hint=(.43, .43), pos_hint={ 'x': .12, 'y': .46 })) mod_flag = True if mod_flag: del self.parent.fov['tracks'] self.parent.fov.create_dataset("tracks", data=self.tracks) def keyboard_closed(self): self.keyboard.unbind(on_key_down=self.key_print) self.keyboard = None def key_print(self, keyboard, keycode, text, modifiers): key = keycode[1] if key == 'a': self.change_frame(self.current_frame - 1) self.canvas.ask_update() if key == 'd': self.change_frame(self.current_frame + 1) self.canvas.ask_update() if key == 'z': if self.track_btn1.state == 'normal': self.track_btn1.state = 'down' else: self.track_btn1.state = 'normal' self.tracking_window.keyboard_press(self.track_btn1.state, 1) if key == 'c': if self.track_btn2.state == 'normal': self.track_btn2.state = 'down' else: self.track_btn2.state = 'normal' self.tracking_window.keyboard_press(self.track_btn2.state, 2) if key == 'v': if self.track_btn3.state == 'normal': self.track_btn3.state = 'down' else: self.track_btn3.state = 'normal' self.tracking_window.keyboard_press(self.track_btn3.state, 3) if key == 'x': if self.track_btn4.state == 'normal': self.track_btn4.state = 'down' else: self.track_btn4.state = 'normal' self.tracking_window.keyboard_press(self.track_btn4.state, 4) if key == 'n': if self.track_btn6.state == 'normal': self.track_btn6.state = 'down' else: self.track_btn6.state = 'normal' self.tracking_window.keyboard_press(self.track_btn6.state, 5) if key == 'w': if self.track_btn5.state == 'normal': self.track_btn5.state = 'down' else: self.track_btn5.state = 'normal' self.jump_window.k_jump(self.track_btn5.state, flag=0) if key == 'i': if self.event_flag1.state == 'normal': self.event_flag1.state = 'down' else: self.event_flag1.state = 'normal' self.jump_window.k_jump(self.event_flag1.state, flag=1) if key == 'o': if self.event_flag2.state == 'normal': self.event_flag2.state = 'down' else: self.event_flag2.state = 'normal' self.jump_window.k_jump(self.event_flag2.state, flag=2) if key == 'p': if self.event_flag3.state == 'normal': self.event_flag3.state = 'down' else: self.event_flag3.state = 'normal' self.jump_window.k_jump(self.event_flag3.state, flag=3) def store_track(self, instance): if self.parent.fov['tracks_stored'][int(self.track_ind)] == 0: self.parent.fov['tracks_stored'][int(self.track_ind)] = 1 self.store_ids = self.store_ids.union( set(self.tracks[self.tracks[:, 4] == self.track_ind, 0])) else: self.parent.fov['tracks_stored'][int(self.track_ind)] = 0 self.store_ids = self.store_ids.difference( set(self.tracks[self.tracks[:, 4] == self.track_ind, 0])) self.modify_update() def save_csv(self, instance): trackcells.save_csv(self.features, self.tracks, self.parent.csv_file) #trackcells.save_iscb(self.features, self.tracks, self.parent.csv_file, self.labels, self.frames) # Need to update since changes def save_sel_csv(self, instance): trackcells.save_sel_csv(self.features, self.tracks, self.parent.fov['tracks_stored'], self.parent.sel_csv_file) def feat_change(self, flag, instance): if instance.text.isdigit(): num = int(''.join([instance.text])) - 1 if 0 <= num < self.features['data'].shape[1]: self.show_feat[flag] = num self.modify_update() self.keyboard = Window.request_keyboard(self.keyboard_closed, self) self.keyboard.bind(on_key_down=self.key_print) def change_channel(self, val, instance): self.channel = int(val) self.mov_disp.update_im( self.movie.read_im(self.channel, self.current_frame)) def remove(self): self.tr_layout.clear_widgets() self.store_layout.clear_widgets() def update_size(self, window, width, height): self.tr_layout.width = width self.tr_layout.height = height self.store_layout.width = width self.store_layout.height = height
class RunTracking(Widget): def __init__(self, features, track_param, frames, **kwargs): super().__init__(**kwargs) self.tracking_object = trackcells.TrackCells(features=features, track_param=track_param, frames=frames) self.track_message = Label( text='[b][color=000000] Tracking cells [/b][/color]', markup=True, size_hint=(.2, .05), pos_hint={ 'x': .4, 'y': .65 }) self.track_counter = Label(text='[b][color=000000] [/b][/color]', markup=True, size_hint=(.2, .05), pos_hint={ 'x': .4, 'y': .6 }) self.cancel_btn = Button(text='Cancel', markup=True, size_hint=(.2, .05), pos_hint={ 'x': .4, 'y': .5 }) self.cancel_btn.bind(on_release=self.cancel_tracking) self.layout = FloatLayout(size=(Window.width, Window.height)) self.add_flag = True self.optimise_flag = False self.count = 0 self.optimise_count = 0 self.sweep = 0 self.cancel_flag = False with self.canvas: self.add_widget(self.layout) self.layout.add_widget(self.track_counter) self.layout.add_widget(self.track_message) self.layout.add_widget(self.cancel_btn) def cancel_tracking(self, instance): self.add_flag = False self.optimise_flag = False self.cancel_flag = True self.parent.cancel_flag = True self.cancel_btn.text = 'Tracking Canceled' def update_count(self): if self.add_flag: self.track_counter.text = '[b][color=000000]' + str( self.count) + '[/b][/color]' else: self.track_counter.text = '[b][color=000000]' + str( self.optimise_count) + '[/b][/color]' def update_message(self, val): if val >= 0: self.track_message.text = '[b][color=000000]Optimising Tracks Sweep ' + str( val) + ' [/b][/color]' self.tracking_state = val else: self.track_message.text = '[b][color=000000]Tracking Completed | Total segments: ' + \ str(self.segment_count) + ' | Total double segments: ' + \ str(self.double_segment) + '[/b][/color]' def add_track(self): if self.add_flag: self.add_flag = self.tracking_object.addtrack() self.count += 1 if not self.add_flag: self.optimise_flag = True self.update_message(self.sweep + 1) return False if self.optimise_flag: self.optimise_flag = self.tracking_object.optimisetrack() self.optimise_count += 1 if not self.optimise_flag and self.sweep < 1: self.optimise_count = 0 self.sweep += 1 self.update_message(self.sweep + 1) self.optimise_flag = True return False if not self.add_flag and not self.optimise_flag: if not self.cancel_flag: self.optimise_count = 0 self.tracks, self.features, self.segment_count, self.double_segment = self.tracking_object.get( ) self.update_message(-1) return True def test_cancel(self): return self.cancel_flag def get(self): return self.tracks, self.features def update_size(self, window, width, height): self.width = width self.height = height
class TestCamera(App): def __init__(self, **kwargs): super(TestCamera, self).__init__(**kwargs) self._camera = None def _camera_toggle(self, val): self._camera.play = not self._camera.play def _led_toggle(self, val): if self._led_state == GPIO.LOW: self._led_state = GPIO.HIGH self._toggle.text = "LED ON" else: self._led_state = GPIO.LOW self._toggle.text = "LED OFF" GPIO.output(LED_Pin, self._led_state) def _show_demo_results(self, val): popup = Popup(title='Reconstruction Results', content=Label(text= 'TOTAL TARGET CELLS : 52\n'+ 'TOTAL FOUND CELLS : 77\n'+ 'TOTAL FILTERED CELLS : 0\n'+ 'TOTAL BEADS FOR TARGET CELLS : 235\n'+ 'AVERAGE BEADS/CELL : 4'), size_hint=(None, None), size=(400, 200)) popup.open() def _show_carousel(self, log_file): local_recon = '/home/pi/recon' prefix = log_file.split('.', 1)[0] carousel = Carousel(direction='right') for filename in os.listdir(local_recon): if filename.startswith(prefix) and filename.endswith('.png'): src = os.path.join(local_recon, filename) image = Image(source=src, allow_stretch=True, size=(620, 460)) carousel.add_widget(image) popup = Popup(title='Reconstruction Results', content=carousel, size_hint=(None, None), size=(640, 480)) popup.open() def _request_capture(self, val): self._camera.capture__full_res_frame() def _request_ref_capture(self, val): self._camera.capture__full_res_ref() def _toggle_object_detection(self, val): detecting = not self._camera.get_object_detection() self._camera.set_object_detection(detecting) if not detecting: self._object_detection.source = '/home/pi/d3-ui/img/object_detection_off.png' else: self._object_detection.source = '/home/pi/d3-ui/img/object_detection_on.png' def _change_exposure(self, instance, val): self._camera.set_exposure(int(val)) def _do_reset_scatter(self, val): # move the mat = Matrix().scale(10,10,10).translate(0,-150,0) self._scatter.transform = mat def _auto_change_exposure(self, val): # go in steps self._auto_centroiding = True self._auto_centroid.source = '/home/pi/d3-ui/img/distribution_centroid_on.png' t = Thread(name='centroid_thread', target=self._do_auto_change_exposure) t.start() def _auto_change_exposure_done(self, l): # go in steps self._auto_centroiding = True self._auto_centroid.source = '/home/pi/d3-ui/img/distribution_centroid_off.png' # basic pid loop.... def _do_auto_change_exposure(self): epsilon = 1 previous_error = 0 integral = 0 error = 127 - self._histogram.centroid dt = 1 Kp = 0.5 #Kd = 0.6 #Ki = 0.6 Kd = 0.1 Ki = 0 tries = 0 max_tries = 15 while abs(error) > epsilon and tries < max_tries: try: integral = integral + error*dt derivative = (error - previous_error)/dt output = Kp*error + Ki*integral + Kd*derivative + self._camera.get_exposure() # set exposure to the output if(output < self._exposure_slider.max and output > self._exposure_slider.min): self._camera.set_exposure(int(output)) # let the exposure settle sleep(.4) previous_error = error error = 127 - self._histogram.centroid tries += 1 except: e = sys.exc_info()[0] Logger.exception('Exception! %s', e) Clock.schedule_once(self._auto_change_exposure_done) def _exit_app(self, val): self._camera.play = False # give it a 1/4 sec to shu down the camera sleep(0.25) App.get_running_app().stop() import sys sys.exit() def _update_histogram(self, val): frame = self._camera.get_current_frame() self._histogram.set_data(frame) #self._histogram.set_pil_image(self._camera.get_current_frame()) #npImage = np.array(frame) #hist_full = cv2.calcHist([npImage],[0],None,[256],[0,256]).ravel() #self._plot.points = [(x, hist_full[x]) for x in range(0,256)] #plt.plot(hist_full,color = 'b') #plt.clf() #plt.hist(npImage.ravel(), 256,[0,256]) #plt.xlim([0,256]) #self._histogram.draw() def _update_fps(self, l): self._fps.text = "FPS: %d" % self._camera.get_fps() self._exposure.text = "Exposure: %d" % self._camera.get_exposure() self._centroid.text = "C: %d" % self._histogram.centroid # todo: change this to be event-driven if self._camera.is_uploading(): if self._uploading.pos_hint['pos'][0] < 0: self._uploading.pos_hint = {'pos':(0.0,0.9)} self._uploadingAmt.pos_hint = {'pos': (0.0, 0.8)} self._upload_progress.pos_hint = {'pos':(0.2,0.9)} #self._demo.pos_hint = {'pos': (0.0, 0.7)} self._snap.enabled = False self._upload_progress.value = 100 * self._camera.get_uploaded_size() / self._camera.get_total_upload_size() self._uploadingAmt.text = '%d kB' % int(self._camera.get_uploaded_size() / 1000) else: if self._uploading.pos_hint['pos'][0] >= 0: try: self._uploading.pos_hint = {'pos':(-1,-1)} self._upload_progress.pos_hint = {'pos':(-1,-1)} self._uploadingAmt.pos_hint = {'pos': (-1, -1)} self._upload_progress.pos_hint = {'pos':(-1,-1)} #self._demo.pos_hint = {'pos': (-1, -1)} self._snap.enabled = True except: e = sys.exc_info()[0] Logger.exception('Exception! %s', e) def updateImages(self): if not self._is_updating: t = Thread(name='image_thread', target=self._do_download) t.start() def _do_download(self): try: self._is_updating = True local_recon ='/home/pi/recon' if not os.path.exists(local_recon): os.makedirs(local_recon) dbx = dropbox.Dropbox("Yk7MLEza3NAAAAAAAAABGyzVVQi_3q7CkUoPjSjO6tWId31ogOM0KiBcdowZoB0b") # dbx = dropbox.Dropbox("Yk7MLEza3NAAAAAAAAAAp3MyYSImy0N0-3IMflqMPenGwEWJPqxAWeOAFzKu6y9A") # remote_path = '/input_test/D3RaspberryPi/%s' % os.path.basename(file) remote_recon = '/input/D3RaspberryPi/recon/' mode = dropbox.files.WriteMode.overwrite #mtime = os.path.getmtime(file) # list the folder contents try: res = dbx.files_list_folder(remote_recon) for entry in res.entries: local_file = os.path.join(local_recon, entry.name) if not os.path.exists (local_file): # download and overwrite it md, dl_res = dbx.files_download(entry.path_lower) file = open(local_file, 'w') file.write(dl_res.content) file.close #delete items no longer in dropbox # for localfile in os.listdir(local_recon): # local_file = os.path.join(local_recon, entry.name) # if not os.path.exists (local_file): # # download and overwrite it # md, dl_res = dbx.files_download(local_file) # file = open(local_file, 'w') # file.write(dl_res.content) # file.close Clock.schedule_once(self.updateResultsButton, 0.5) except dropbox.exceptions.ApiError as err: print('*** API error', err) return None except: e = sys.exc_info()[0] Logger.exception('Exception! %s', e) Clock.schedule_once(self.updateResultsButton, 0.5) self._is_updating = False def updateResultsButton(self, dt=None): try: # todo: contact dropbox local_recon = '/home/pi/recon' for local_file in os.listdir(local_recon): if local_file.endswith('.raw.log.iphone.log'): btn = Button(text=local_file, size_hint_y=None, height=44) btn.bind(on_release=lambda btn: self._show_carousel(btn.text)) self._dropdown.add_widget(btn) except: # do nothing print('error') def build(self): GPIO.setmode(GPIO.BCM) GPIO.setup(LED_Pin, GPIO.OUT) self._led_state = GPIO.LOW GPIO.output(LED_Pin, self._led_state) #layout = BoxLayout(orientation='vertical') layout = FloatLayout(size=(800, 480), pos=(0,0)) self._toggle = ToggleButton(text='LED OFF', size_hint=(0.2,0.2), pos_hint={'pos':(0.8,0)}) self._snap = Button(text='Capture', size_hint=(0.2,0.2), pos_hint={'pos':(0.8,0.2)}) self._snapref = Button(text='Reference', size_hint=(0.2,0.2), pos_hint={'pos':(0.8,0.4)}) self._demo = Button(text='Demo Results', size_hint=(0.2,0.1), pos_hint={'pos': (0.0, 0.7)}) self._auto_centroid = ImageButton(size_hint=(0.1,0.1), pos_hint={'pos':(0.7,0)}, source='/home/pi/d3-ui/img/distribution_centroid_off.png') self._object_detection = ImageButton(size_hint=(0.1,0.1), pos_hint={'pos':(0.7,0.1)}, source='/home/pi/d3-ui/img/object_detection_off.png') self._reset_scatter = ImageButton(size_hint=(0.1,0.1), pos_hint={'pos':(0.7,0.2)}, source='/home/pi/d3-ui/img/reset_scatter.png') self._exit = Button(text='X', size_hint=(0.05,0.05), pos_hint={'pos':(0.95,0.95)}) self._fps = Label(text='FPS: 0', size_hint=(0.1,0.1), pos_hint={'pos':(0.8,0.9)}) self._uploading = Label(text='Uploading...', size_hint=(0.2,0.1), pos_hint={'pos':(-1,-1)}, color=[0,0,1,0]) self._uploadingAmt = Label(text='', size_hint=(0.2,0.1), pos_hint={'pos':(-1,-1)}, color=[0,0,1,0]) self._exposure = Label(text='Exposure: 0', size_hint=(0.2,0.1), pos_hint={'pos':(0,0)}) self._centroid = Label(text='C:0', size_hint=(0.1,0.1), pos_hint={'pos':(0.79,0.83)}, color=[1,0,0,1]) self._exposure_slider = Slider(min=0, max=2500, value=333, size_hint=(0.5,0.1), pos_hint={'pos':(0.2,0)} ) self._upload_progress = ProgressBar(max=100, size_hint=(0.5,0.1), pos_hint={'pos':(-1,-1)}) self._camera = CameraD3(resolution=(640,480), fourcc="GREY", capture_resolution=(3872, 2764), capture_fourcc="Y16 ", size_hint=(1,1), pos_hint={'pos':(0,0)}, play=True, ) self._dropdown = DropDown() # create a big main button self._imageResultsButton = Button(text='Image Explorer',pos_hint={'pos': (0.0, 0.6)}, size_hint=(0.2, 0.1)) # show the dropdown menu when the main button is released # note: all the bind() calls pass the instance of the caller (here, the # mainbutton instance) as the first argument of the callback (here, # dropdown.open.). self._imageResultsButton.bind(on_release=self._dropdown.open) # one last thing, listen for the selection in the dropdown list and # assign the data to the button text. self._dropdown.bind(on_select=lambda instance, x: setattr(self._imageResultsButton, 'text', x)) # self._camera = CameraD3(resolution=(1280,720), # play=True, fourcc="GREY") # self._camera = CameraD3(resolution=(3872, 2764), # play=True, fourcc="Y16 ") # self._camera = CameraD3(resolution=(1920,1080), # play=True, fourcc="GREY") # self._camera = CameraD3(resolution=(2560, 1920), # play=True, fourcc="GREY") self._histogram = Histogram( size_hint=(0.2,0.3), pos_hint={'pos':(0.8,0.6)}) self._demo.bind(on_press=self._show_demo_results) self._toggle.bind(on_press=self._led_toggle) self._snap.bind(on_press=self._request_capture) self._snapref.bind(on_press=self._request_ref_capture) self._exit.bind(on_press=self._exit_app) self._auto_centroid.bind(on_press=self._auto_change_exposure) self._object_detection.bind(on_press=self._toggle_object_detection) self._exposure_slider.bind(value=self._change_exposure) self._reset_scatter.bind(on_press=self._do_reset_scatter) #update.bind(on_press=self._update_histogram) self._camera.fbind('on_frame_complete',self._update_histogram) self._scatter = Scatter(size_hint=(None, None), size=(200,200),) self._scatter.add_widget(self._camera) #layout.add_widget(self._camera) layoutInner = FloatLayout(size_hint=(0.8, 1), pos_hint={'x':0,'y':0}) layoutInner.add_widget(self._scatter) layout.add_widget(layoutInner) mat = Matrix().scale(10,10,10).translate(0,-150,0) self._scatter.apply_transform(mat) layout.add_widget(self._imageResultsButton) layout.add_widget(self._uploading) layout.add_widget(self._uploadingAmt) layout.add_widget(self._demo) layout.add_widget(self._histogram) layout.add_widget(self._snap) layout.add_widget(self._snapref) layout.add_widget(self._exit) layout.add_widget(self._centroid) layout.add_widget(self._exposure_slider) layout.add_widget(self._upload_progress) layout.add_widget(self._auto_centroid) layout.add_widget(self._object_detection) layout.add_widget(self._reset_scatter) layout.add_widget(self._exposure) layout.add_widget(self._fps) Clock.schedule_interval(self._update_fps, 2) layout.add_widget(self._toggle) #layout.add_widget(update) self._is_updating = False self.updateImages() return layout
def build(self): GPIO.setmode(GPIO.BCM) GPIO.setup(LED_Pin, GPIO.OUT) self._led_state = GPIO.LOW GPIO.output(LED_Pin, self._led_state) #layout = BoxLayout(orientation='vertical') layout = FloatLayout(size=(800, 480), pos=(0,0)) self._toggle = ToggleButton(text='LED OFF', size_hint=(0.2,0.2), pos_hint={'pos':(0.8,0)}) self._snap = Button(text='Capture', size_hint=(0.2,0.2), pos_hint={'pos':(0.8,0.2)}) self._snapref = Button(text='Reference', size_hint=(0.2,0.2), pos_hint={'pos':(0.8,0.4)}) self._demo = Button(text='Demo Results', size_hint=(0.2,0.1), pos_hint={'pos': (0.0, 0.7)}) self._auto_centroid = ImageButton(size_hint=(0.1,0.1), pos_hint={'pos':(0.7,0)}, source='/home/pi/d3-ui/img/distribution_centroid_off.png') self._object_detection = ImageButton(size_hint=(0.1,0.1), pos_hint={'pos':(0.7,0.1)}, source='/home/pi/d3-ui/img/object_detection_off.png') self._reset_scatter = ImageButton(size_hint=(0.1,0.1), pos_hint={'pos':(0.7,0.2)}, source='/home/pi/d3-ui/img/reset_scatter.png') self._exit = Button(text='X', size_hint=(0.05,0.05), pos_hint={'pos':(0.95,0.95)}) self._fps = Label(text='FPS: 0', size_hint=(0.1,0.1), pos_hint={'pos':(0.8,0.9)}) self._uploading = Label(text='Uploading...', size_hint=(0.2,0.1), pos_hint={'pos':(-1,-1)}, color=[0,0,1,0]) self._uploadingAmt = Label(text='', size_hint=(0.2,0.1), pos_hint={'pos':(-1,-1)}, color=[0,0,1,0]) self._exposure = Label(text='Exposure: 0', size_hint=(0.2,0.1), pos_hint={'pos':(0,0)}) self._centroid = Label(text='C:0', size_hint=(0.1,0.1), pos_hint={'pos':(0.79,0.83)}, color=[1,0,0,1]) self._exposure_slider = Slider(min=0, max=2500, value=333, size_hint=(0.5,0.1), pos_hint={'pos':(0.2,0)} ) self._upload_progress = ProgressBar(max=100, size_hint=(0.5,0.1), pos_hint={'pos':(-1,-1)}) self._camera = CameraD3(resolution=(640,480), fourcc="GREY", capture_resolution=(3872, 2764), capture_fourcc="Y16 ", size_hint=(1,1), pos_hint={'pos':(0,0)}, play=True, ) self._dropdown = DropDown() # create a big main button self._imageResultsButton = Button(text='Image Explorer',pos_hint={'pos': (0.0, 0.6)}, size_hint=(0.2, 0.1)) # show the dropdown menu when the main button is released # note: all the bind() calls pass the instance of the caller (here, the # mainbutton instance) as the first argument of the callback (here, # dropdown.open.). self._imageResultsButton.bind(on_release=self._dropdown.open) # one last thing, listen for the selection in the dropdown list and # assign the data to the button text. self._dropdown.bind(on_select=lambda instance, x: setattr(self._imageResultsButton, 'text', x)) # self._camera = CameraD3(resolution=(1280,720), # play=True, fourcc="GREY") # self._camera = CameraD3(resolution=(3872, 2764), # play=True, fourcc="Y16 ") # self._camera = CameraD3(resolution=(1920,1080), # play=True, fourcc="GREY") # self._camera = CameraD3(resolution=(2560, 1920), # play=True, fourcc="GREY") self._histogram = Histogram( size_hint=(0.2,0.3), pos_hint={'pos':(0.8,0.6)}) self._demo.bind(on_press=self._show_demo_results) self._toggle.bind(on_press=self._led_toggle) self._snap.bind(on_press=self._request_capture) self._snapref.bind(on_press=self._request_ref_capture) self._exit.bind(on_press=self._exit_app) self._auto_centroid.bind(on_press=self._auto_change_exposure) self._object_detection.bind(on_press=self._toggle_object_detection) self._exposure_slider.bind(value=self._change_exposure) self._reset_scatter.bind(on_press=self._do_reset_scatter) #update.bind(on_press=self._update_histogram) self._camera.fbind('on_frame_complete',self._update_histogram) self._scatter = Scatter(size_hint=(None, None), size=(200,200),) self._scatter.add_widget(self._camera) #layout.add_widget(self._camera) layoutInner = FloatLayout(size_hint=(0.8, 1), pos_hint={'x':0,'y':0}) layoutInner.add_widget(self._scatter) layout.add_widget(layoutInner) mat = Matrix().scale(10,10,10).translate(0,-150,0) self._scatter.apply_transform(mat) layout.add_widget(self._imageResultsButton) layout.add_widget(self._uploading) layout.add_widget(self._uploadingAmt) layout.add_widget(self._demo) layout.add_widget(self._histogram) layout.add_widget(self._snap) layout.add_widget(self._snapref) layout.add_widget(self._exit) layout.add_widget(self._centroid) layout.add_widget(self._exposure_slider) layout.add_widget(self._upload_progress) layout.add_widget(self._auto_centroid) layout.add_widget(self._object_detection) layout.add_widget(self._reset_scatter) layout.add_widget(self._exposure) layout.add_widget(self._fps) Clock.schedule_interval(self._update_fps, 2) layout.add_widget(self._toggle) #layout.add_widget(update) self._is_updating = False self.updateImages() return layout
class IDSGUI(App): def __init__(self): super(IDSGUI, self).__init__() self._camera = None def _led_toggle(self, val): if self._led_state == GPIO.LOW: self._led_state = GPIO.HIGH self._toggle.text = "LED ON" else: self._led_state = GPIO.LOW self._toggle.text = "LED OFF" GPIO.output(LED_Pin, self._led_state) def _start_count(self, val): pass # fake_bin = int((np.random.randn() * 100) + 320) # self._tracker_histogram.add_track(max(0, min(640, fake_bin))) # (x,y) = self._camera.get_roi_offset() # self._camera.set_roi_offset(x+50,y+50) def _toggle_object_detection(self, val): pass # detecting = not self._camera.get_object_detection() # self._camera.set_object_detection(detecting) # if not detecting: # self._object_detection.source = self.img_path('object_detection_off.png') # else: # self._object_detection.source = self.img_path('object_detection_on.png') def _change_exposure(self, instance, val): pass # self._camera.set_exposure(int(val)) def _do_reset_scatter(self, val): # move the mat = Matrix().scale(10, 10, 10).translate(0, -150, 0) self._scatter.transform = mat def _exit_app(self, val): # self._camera.play = False # give it a 1/4 sec to shu down the camera sleep(0.25) App.get_running_app().stop() import sys sys.exit() def _update_histogram(self, val): pass # frame = self._camera.get_current_frame() # self._histogram.set_data(frame) def _update_fps(self, l): pass # self._fps.text = "FPS: %d" % self._camera.get_fps() # self._temp.text = "Temp: %s" % self._camera.get_temp() # self._exposure.text = "Exposure: %d" % self._camera.get_exposure() # self._centroid.text = "C: %d" % self._histogram.centroid def _update_roi(self, roi, roi2): pass # roi = self._roi_tracker.roi_Offset # self._camera.set_roi_offset(roi[0], roi[1]) def img_path(self, image_name): return os.path.join(os.path.dirname(__file__), 'img', image_name) def init_GPIO(self): GPIO.setmode(GPIO.BCM) GPIO.setup(LED_Pin, GPIO.OUT) self._led_state = GPIO.LOW GPIO.output(LED_Pin, self._led_state) def get_ip_address(self): try: import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) addr = s.getsockname()[0] s.close() return addr except: return "Error" def build(self): self.init_GPIO() layout = FloatLayout(size=(800, 480), pos=(0, 0)) self._toggle = ToggleButton(text='LED OFF', size_hint=(0.2, 0.2), pos_hint={'pos': (0.8, 0)}) self._snap = Button(text='COUNT', size_hint=(0.2, 0.2), pos_hint={'pos': (0.8, 0.2)}) self._object_detection = ImageButton( size_hint=(0.1, 0.1), pos_hint={'pos': (0.7, 0.0)}, source=self.img_path('object_detection_off.png')) self._reset_scatter = ImageButton( size_hint=(0.1, 0.1), pos_hint={'pos': (0.7, 0.1)}, source=self.img_path('reset_scatter.png')) self._logo = Image(size_hint=(0.3, 0.2), pos_hint={'pos': (-0.03, 0.84)}, source=self.img_path('ids.png')) self._exit = ImageButton(size_hint=(0.05, 0.05), pos_hint={'pos': (0.96, 0.95)}, source=self.img_path('close-white.png')) red, white, kivy_blue = (1, 0, 0, 1), (1, 1, 1, 1), (51. / 255, 164. / 255, 206. / 255, 1) self._fps = Label(text='FPS: 0', size_hint=(0.1, 0.1), pos_hint={'pos': (0.8, 0.9)}, color=red) self._temp = Label(text='Temp: 0', size_hint=(0.1, 0.1), pos_hint={'pos': (0.6, 0.9)}, color=white) dev_ip = self.get_ip_address() self._ip = Label(text='IP: %s' % dev_ip, size_hint=(0.1, 0.1), pos_hint={'pos': (0.4, 0.9)}, color=white) self._exposure = Label(text='Exposure: 0', size_hint=(0.2, 0.1), pos_hint={'pos': (0, 0)}) self._centroid = Label(text='C:0', size_hint=(0.1, 0.1), pos_hint={'pos': (0.79, 0.83)}, color=white) self._exposure_slider = Slider(min=0, max=2500, value=1358, size_hint=(0.5, 0.1), pos_hint={'pos': (0.2, 0)}) # self._camera = Camera2131(resolution=(1280,960), # fourcc="GREY", # capture_resolution=(3872, 2764), # capture_fourcc="Y16 ", # size_hint=(1,1), # pos_hint={'pos':(0,0)}, # play=True, ) # self._camera = CameraIDS(resolution=(640, 480), # fourcc="GREY", # capture_resolution=(640, 480), # capture_fourcc="GREY", # size_hint=(1,1), # pos_hint={'pos':(0,0)}, # play=True) self._histogram = Histogram(size_hint=(0.2, 0.25), pos_hint={'pos': (0.8, 0.65)}) self._tracker_histogram = TrackerHistogram( size_hint=(0.2, 0.25), pos_hint={'pos': (0.8, 0.4)}) # self._roi_tracker = ROITracker(self._camera.resolution, # size_hint=(0.2,0.25), pos_hint={'pos':(0.02,0.6)}) # self._roi_tracker.bind(roi_Offset=self._update_roi) # self._demo.bind(on_press=self._show_demo_results) self._toggle.bind(on_press=self._led_toggle) self._snap.bind(on_press=self._start_count) self._exit.bind(on_press=self._exit_app) self._object_detection.bind(on_press=self._toggle_object_detection) self._exposure_slider.bind(value=self._change_exposure) self._reset_scatter.bind(on_press=self._do_reset_scatter) # self._camera.fbind('on_frame_complete',self._update_histogram) self._scatter = Scatter( size_hint=(None, None), size=(200, 200), ) # self._scatter.add_widget(self._camera) # layoutInner = FloatLayout(size_hint=(0.8, 1), pos_hint={'x':0,'y':0}) # layoutInner.add_widget(self._scatter) layout.add_widget(self._scatter) mat = Matrix().scale(10, 10, 10).translate(0, -150, 0) self._scatter.apply_transform(mat) layout.add_widget(self._histogram) layout.add_widget(self._tracker_histogram) layout.add_widget(self._snap) layout.add_widget(self._exit) layout.add_widget(self._exposure_slider) layout.add_widget(self._object_detection) layout.add_widget(self._reset_scatter) layout.add_widget(self._exposure) layout.add_widget(self._fps) layout.add_widget(self._temp) layout.add_widget(self._ip) Clock.schedule_interval(self._update_fps, 2) layout.add_widget(self._toggle) layout.add_widget(self._logo) # layout.add_widget(self._roi_tracker) self._is_updating = False return layout
def build(self): self.init_GPIO() layout = FloatLayout(size=(800, 480), pos=(0, 0)) self._toggle = ToggleButton(text='LED OFF', size_hint=(0.2, 0.2), pos_hint={'pos': (0.8, 0)}) self._snap = Button(text='COUNT', size_hint=(0.2, 0.2), pos_hint={'pos': (0.8, 0.2)}) self._object_detection = ImageButton( size_hint=(0.1, 0.1), pos_hint={'pos': (0.7, 0.0)}, source=self.img_path('object_detection_off.png')) self._reset_scatter = ImageButton( size_hint=(0.1, 0.1), pos_hint={'pos': (0.7, 0.1)}, source=self.img_path('reset_scatter.png')) self._logo = Image(size_hint=(0.3, 0.2), pos_hint={'pos': (-0.03, 0.84)}, source=self.img_path('ids.png')) self._exit = ImageButton(size_hint=(0.05, 0.05), pos_hint={'pos': (0.96, 0.95)}, source=self.img_path('close-white.png')) red, white, kivy_blue = (1, 0, 0, 1), (1, 1, 1, 1), (51. / 255, 164. / 255, 206. / 255, 1) self._fps = Label(text='FPS: 0', size_hint=(0.1, 0.1), pos_hint={'pos': (0.8, 0.9)}, color=red) self._temp = Label(text='Temp: 0', size_hint=(0.1, 0.1), pos_hint={'pos': (0.6, 0.9)}, color=white) dev_ip = self.get_ip_address() self._ip = Label(text='IP: %s' % dev_ip, size_hint=(0.1, 0.1), pos_hint={'pos': (0.4, 0.9)}, color=white) self._exposure = Label(text='Exposure: 0', size_hint=(0.2, 0.1), pos_hint={'pos': (0, 0)}) self._centroid = Label(text='C:0', size_hint=(0.1, 0.1), pos_hint={'pos': (0.79, 0.83)}, color=white) self._exposure_slider = Slider(min=0, max=2500, value=1358, size_hint=(0.5, 0.1), pos_hint={'pos': (0.2, 0)}) # self._camera = Camera2131(resolution=(1280,960), # fourcc="GREY", # capture_resolution=(3872, 2764), # capture_fourcc="Y16 ", # size_hint=(1,1), # pos_hint={'pos':(0,0)}, # play=True, ) # self._camera = CameraIDS(resolution=(640, 480), # fourcc="GREY", # capture_resolution=(640, 480), # capture_fourcc="GREY", # size_hint=(1,1), # pos_hint={'pos':(0,0)}, # play=True) self._histogram = Histogram(size_hint=(0.2, 0.25), pos_hint={'pos': (0.8, 0.65)}) self._tracker_histogram = TrackerHistogram( size_hint=(0.2, 0.25), pos_hint={'pos': (0.8, 0.4)}) # self._roi_tracker = ROITracker(self._camera.resolution, # size_hint=(0.2,0.25), pos_hint={'pos':(0.02,0.6)}) # self._roi_tracker.bind(roi_Offset=self._update_roi) # self._demo.bind(on_press=self._show_demo_results) self._toggle.bind(on_press=self._led_toggle) self._snap.bind(on_press=self._start_count) self._exit.bind(on_press=self._exit_app) self._object_detection.bind(on_press=self._toggle_object_detection) self._exposure_slider.bind(value=self._change_exposure) self._reset_scatter.bind(on_press=self._do_reset_scatter) # self._camera.fbind('on_frame_complete',self._update_histogram) self._scatter = Scatter( size_hint=(None, None), size=(200, 200), ) # self._scatter.add_widget(self._camera) # layoutInner = FloatLayout(size_hint=(0.8, 1), pos_hint={'x':0,'y':0}) # layoutInner.add_widget(self._scatter) layout.add_widget(self._scatter) mat = Matrix().scale(10, 10, 10).translate(0, -150, 0) self._scatter.apply_transform(mat) layout.add_widget(self._histogram) layout.add_widget(self._tracker_histogram) layout.add_widget(self._snap) layout.add_widget(self._exit) layout.add_widget(self._exposure_slider) layout.add_widget(self._object_detection) layout.add_widget(self._reset_scatter) layout.add_widget(self._exposure) layout.add_widget(self._fps) layout.add_widget(self._temp) layout.add_widget(self._ip) Clock.schedule_interval(self._update_fps, 2) layout.add_widget(self._toggle) layout.add_widget(self._logo) # layout.add_widget(self._roi_tracker) self._is_updating = False return layout
class ToolsPanel(StackLayout): btn_create_stream = '' btn_create_node = '' def __init__(self, **kwargs): super(ToolsPanel, self).__init__(**kwargs) self.medium = kwargs.get('medium', '') self.orientation = 'tb-lr' btn_size = "30px" self.btn_create_stream = ToggleButton( size=(btn_size, btn_size), size_hint=(None, None), background_normal='images/stream_up.png', background_down='images/stream_down.png', border=(0,0,0,0), group='tools', state='normal') self.btn_create_node = ToggleButton( size=(btn_size, btn_size), size_hint=(None, None), background_normal='images/node_up.png', background_down='images/node_down.png', border=(0,0,0,0), group='tools', state='normal') self.btn_create_link = ToggleButton( size=(btn_size, btn_size), size_hint=(None, None), background_normal='images/link_up.png', background_down='images/link_down.png', border=(0,0,0,0), group='tools', state='normal') self.btn_settings = Button( size=(btn_size, btn_size), size_hint=(None, None), background_normal='images/gear_up.png', background_down='images/gear_down.png', border=(0,0,0,0)) # self.btn_multi_select = ToggleButton( # size=(btn_size, btn_size), # size_hint=(None, None), # border=(0,0,0,0), # group='tools', # state='normal') self.add_widget(self.btn_create_stream) self.add_widget(self.btn_create_node) self.add_widget(self.btn_create_link) self.add_widget(self.btn_settings) # self.add_widget(self.btn_multi_select) self.spacing=(10, 10) self.padding=(10, 10) self.btn_create_stream.bind(on_release=partial(self.medium.on_btn_create_stream_released, self.btn_create_stream)) self.btn_create_node.bind(on_release=partial(self.medium.on_btn_create_node_released, self.btn_create_node)) self.btn_create_link.bind(on_release=partial(self.medium.on_btn_create_link_released, self.btn_create_link)) self.btn_settings.bind(on_release=partial(self.medium.on_btn_settings_released, self.btn_settings))
def _create_popup(self, instance): # create the popup content = BoxLayout(orientation='vertical', spacing='5dp') popup_width = min(0.95 * Window.width, dp(700)) self.popup = Popup(content=content, title=self.title, size_hint=(None, None), size=(popup_width, '400dp')) self.popup.height = 0.95 * Window.height # add all the options #content.add_widget(Widget(size_hint_y=None, height=1)) scrolls_box = BoxLayout(orientation='horizontal', spacing='5dp') layout_all = GridLayout(cols=1, padding=5, spacing=5, size_hint=(None, None)) layout_all.bind(minimum_height=layout_all.setter('height')) scroll_all = ScrollView(size_hint=(None, None), size=(300, 320), pos_hint={ 'center_x': .5, 'center_y': .5 }, do_scroll_x=False) lex_gen = (lex[0] for lex in lexers.get_all_lexers()) for lex in lex_gen: btn = Button(text=str(lex), size=(300, 30), size_hint=(None, None)) btn.bind(on_release=self._add_lang) layout_all.add_widget(btn) scroll_all.add_widget(layout_all) scrolls_box.add_widget(scroll_all) layout_use = GridLayout(cols=1, padding=5, spacing=5, size_hint=(None, None)) layout_use.bind(minimum_height=layout_use.setter('height')) scroll_use = ScrollView(size_hint=(None, None), size=(300, 320), pos_hint={ 'center_x': .5, 'center_y': .5 }, do_scroll_x=False) for opt in self.options: btn = Button(text=str(opt), size=(300, 30), size_hint=(None, None)) btn.bind(on_release=self._rm_lang) layout_use.add_widget(btn) scroll_use.add_widget(layout_use) scrolls_box.add_widget(scroll_use) content.add_widget(scrolls_box) btn_ok = Button(text='OK', size_hint_y=None, height=dp(50)) btn_ok.bind(on_release=self._set_option) btn_cl = Button(text='Cancel', size_hint_y=None, height=dp(50)) btn_cl.bind(on_release=self.popup.dismiss) content.add_widget(btn_ok) content.add_widget(btn_cl) # and open the popup ! self.popup.open()