Last active
April 10, 2019 22:11
-
-
Save dmitrysarov/02c7d0aefc1e619dd91e46ddd785f8ed to your computer and use it in GitHub Desktop.
first experience with widgets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ipywidgets as widgets | |
from IPython.display import display, clear_output | |
from fxd import FxdInputFile | |
import matplotlib.pyplot as plt | |
import matplotlib.animation as animation | |
from PIL import Image | |
import pickle | |
import numpy as np | |
from copy import deepcopy | |
%config InlineBackend.close_figures=False | |
# plt.ioff() | |
import json | |
from glob import glob | |
import os | |
class Dataset_checker(object): | |
''' | |
stupid GUI for markup check, Input is list of dicts {'path_to_file': '', 'annotation': '', | |
'bbox': False, 'valid': True, 'checked': False, 'still': True}, where bbox mean is there black | |
bound around image, valid - has image pacemacker or not, cheched - item was checked, | |
still - is frame is moving or not. | |
''' | |
def __init__(self, meta_data): | |
self.meta_data = deepcopy(meta_data) | |
#find interaption point if exist | |
self.current_position = -1 | |
for num, md in enumerate(self.meta_data): | |
if md['checked'] == True: | |
self.current_position = num+1 | |
if self.current_position != -1 and md['checked'] == False: | |
break | |
# define output fields | |
self.completeness_output = widgets.Output() | |
self.show_sequence_output = widgets.Output(layout=widgets.Layout(height='500px', width='500px', border='1px solid')) | |
self.path_output = widgets.Output() | |
self.annotation_output = widgets.Output() | |
self.current_sequence_completeness_output = widgets.Output() | |
self.ax = plt.gca() | |
self.ax.set_axis_off() | |
# defile buttons and checkboxes | |
self.next_button = widgets.Button(description='next', disabled=False) | |
self.previous_button = widgets.Button(description='prev', disabled=True if self.current_position<=0 else False) | |
self.go_to_button = widgets.Button(description='go_to', disabled=False) | |
self.go_to_text = widgets.Text(value='', description='go_to: ', disabled=False) | |
self.play_button = widgets.Button(description='play', disabled=True if self.current_position==-1 else False) | |
self.submission_checkboxes = [widgets.Checkbox(value=False, description=key, disabled=False, layout=widgets.Layout(width='15%')) for key in ['bbox', 'valid', 'still']] | |
self.submission_checkboxes.append(widgets.Valid(value=False, description='checked')) | |
# stacking layaouts | |
submission_box = widgets.VBox([widgets.HBox(self.submission_checkboxes), self.current_sequence_completeness_output]) | |
goto_box = widgets.HBox([self.previous_button, self.next_button, self.play_button, self.go_to_button, self.go_to_text]) | |
self.vbox = widgets.VBox([self.completeness_output, self.path_output, self.annotation_output, self.show_sequence_output, goto_box, submission_box]) | |
#assign event functions | |
for b in [self.next_button, self.previous_button, self.go_to_button]: | |
b.on_click(self.__goto_button_click__) | |
self.play_button.on_click(self.play_sequence) | |
#backup processed data | |
checkpoint_prefix = 'processed_data_checkpoint' | |
if not os.path.isfile('{}_0.json'.format(checkpoint_prefix)): | |
self.path_to_backup_file = '{}_0.json'.format(checkpoint_prefix) | |
else: | |
list_of_backup_files = sorted(glob('{}_*.json'.format(checkpoint_prefix))) | |
self.path_to_backup_file = '{}_{}.json'.format(checkpoint_prefix, int(list_of_backup_files[-1].split('_')[-1].split('.')[0])+1) | |
def __call__(self): | |
display(self.vbox) | |
def __goto_button_click__(self, b): | |
''' | |
event function for go to buttons | |
''' | |
if self.current_position != -1: | |
self.__submission_button_click__() | |
if b.description == 'next': | |
self.current_position += 1 | |
elif b.description == 'prev': | |
self.current_position -= 1 | |
elif b.description == 'go_to': | |
self.current_position = int(self.go_to_text.value) | |
self.update_completeness() | |
if self.current_position>0: | |
self.previous_button.disabled = False | |
else: | |
self.previous_button.disabled = True | |
for scb in self.submission_checkboxes: | |
scb.value = self.meta_data[self.current_position][scb.description] | |
self.play_button.disabled = False | |
self.go_to_text.value = str(self.current_position) | |
self.show_image() | |
def __submission_button_click__(self): | |
''' | |
event function for submission button. Depricated! go to button do it | |
''' | |
for scb in self.submission_checkboxes: | |
self.meta_data[self.current_position][scb.description] = scb.value | |
self.meta_data[self.current_position]['checked'] = True | |
with open(self.path_to_backup_file, 'w') as f: | |
json.dump(self.meta_data, f) | |
def update_completeness(self): | |
''' | |
for displaying process progress | |
''' | |
with self.completeness_output: | |
clear_output(wait=True) | |
display('Mark up completeness {0:0.1f}%'.format((self.current_position+1)/len(self.meta_data)*100)) | |
def show_image(self): | |
''' | |
show first image of sequence | |
''' | |
path_to_file = self.meta_data[self.current_position]['path_to_file'] | |
annotation = self.meta_data[self.current_position]['annotation'] | |
with self.annotation_output: | |
clear_output(wait=True) | |
display('Annotation is : {}'.format(annotation)) | |
with self.path_output: | |
clear_output(wait=True) | |
display('File path : {}'.format(path_to_file)) | |
self.sequence = FxdInputFile(path_to_file).read_seq() | |
self.ax.clear() | |
self.ax.imshow(self.sequence[0,...], cmap='gray') | |
self.ax.figure.set_size_inches(10, 10) | |
self.ax.set_axis_off() | |
with self.show_sequence_output: | |
clear_output(wait=True) | |
display(self.ax.figure) | |
def play_sequence(self, b): | |
''' | |
show sequence of images | |
''' | |
for num, img in enumerate(self.sequence, 1): | |
with self.current_sequence_completeness_output: | |
clear_output(wait=True) | |
display('{0:0.1f} %'.format(num/len(self.sequence)*100)) #TODO replace with Output | |
self.ax.clear() | |
self.ax.imshow(img, cmap='gray') | |
self.ax.figure.set_size_inches(10, 10) | |
self.ax.set_axis_off() | |
with self.show_sequence_output: | |
clear_output(wait=True) | |
display(self.ax.figure) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment