Skip to content

Commit

Permalink
labelling
Browse files Browse the repository at this point in the history
  • Loading branch information
sronilsson committed Aug 5, 2024
1 parent a4c7fbd commit 6a9f886
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 255 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Setup configuration
setuptools.setup(
name="Simba-UW-tf-dev",
version="1.99.9",
version="2.0.1",
author="Simon Nilsson, Jia Jie Choong, Sophia Hwang",
author_email="[email protected]",
description="Toolkit for computer classification and analysis of behaviors in experimental animals",
Expand Down
4 changes: 2 additions & 2 deletions simba/SimBA.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,10 @@ def activate(box, *args):


feature_tools_frm = LabelFrame(tab5, text="FEATURE TOOLS", pady=5, font=Formats.FONT_HEADER.value)
compute_feature_subset_btn = SimbaButton(parent=feature_tools_frm, txt="CALCULATE FEATURE SUBSETS", txt_clr='blue', font=Formats.FONT_REGULAR.value, cmd=FeatureSubsetExtractorPopUp, cmd_kwargs={'config_path': lambda:self.config_path}, thread=False)
compute_feature_subset_btn = SimbaButton(parent=feature_tools_frm, txt="CALCULATE FEATURE SUBSETS", txt_clr='blue', font=Formats.FONT_REGULAR.value, cmd=FeatureSubsetExtractorPopUp, cmd_kwargs={'config_path': lambda: self.config_path}, thread=False)

label_behavior_frm = CreateLabelFrameWithIcon(parent=tab7, header="LABEL BEHAVIOR", icon_name=Keys.DOCUMENTATION.value, icon_link=Links.LABEL_BEHAVIOR.value)
select_video_btn_new = SimbaButton(parent=label_behavior_frm, txt="Select video (create new video annotation)", img='label_blue', txt_clr='navy', cmd=select_labelling_video, cmd_kwargs={'config_path': lambda:self.config_path, 'threshold_dict': lambda:None, 'setting': lambda: "from_scratch", lambda: 'continuing': False}, thread=False)
select_video_btn_new = SimbaButton(parent=label_behavior_frm, txt="Select video (create new video annotation)", img='label_blue', txt_clr='navy', cmd=select_labelling_video, cmd_kwargs={'config_path': lambda :self.config_path, 'threshold_dict': lambda: None, 'setting': lambda: "from_scratch", 'continuing': lambda: False}, thread=False)
select_video_btn_continue = SimbaButton(parent=label_behavior_frm, txt="Select video (continue existing video annotation)", img='label_yellow', txt_clr='darkgoldenrod', cmd=select_labelling_video, cmd_kwargs={'config_path': lambda: self.config_path, 'threshold_dict': lambda:None, 'setting': lambda:None, 'continuing': lambda:True}, thread=False)

label_thirdpartyann = CreateLabelFrameWithIcon(parent=tab7, header="IMPORT THIRD-PARTY BEHAVIOR ANNOTATIONS", icon_name=Keys.DOCUMENTATION.value, icon_link=Links.THIRD_PARTY_ANNOTATION.value)
Expand Down
26 changes: 13 additions & 13 deletions simba/labelling/labelling_advanced_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
from simba.mixins.config_reader import ConfigReader
from simba.utils.checks import check_file_exist_and_readable, check_int
from simba.utils.enums import Formats, Options, TagNames
from simba.utils.errors import (AdvancedLabellingError, FrameRangeError,
NoDataError)
from simba.utils.lookups import get_labelling_img_kbd_bindings
from simba.utils.errors import (AdvancedLabellingError, FrameRangeError, NoDataError)
from simba.utils.lookups import get_labelling_img_kbd_bindings, get_labelling_video_kbd_bindings
from simba.utils.printing import log_event, stdout_success
from simba.utils.read_write import (get_all_clf_names, get_fn_ext,
get_video_meta_data, read_config_entry,
read_df, write_df)
read_df)


class AdvancedLabellingInterface(ConfigReader):
Expand Down Expand Up @@ -57,7 +56,9 @@ def __init__(self, config_path: str, file_path: str, continuing: bool):
self.machine_results_file_path = os.path.join(self.machine_results_dir, self.video_name + "." + self.file_type)
self.cap = cv2.VideoCapture(self.video_path)
self.img_kbd_bindings = get_labelling_img_kbd_bindings()
self.video_kbd_bindings = get_labelling_video_kbd_bindings()
self.__create_key_presses_lbl()
self.__create_video_key_presses_lbl()
self.video_meta_data = get_video_meta_data(video_path=self.video_path)
self.frame_lst = list(range(0, self.video_meta_data["frame_count"]))
self.max_frm_no = max(self.frame_lst)
Expand Down Expand Up @@ -179,14 +180,7 @@ def __init__(self, config_path: str, file_path: str, continuing: bool):
self.video_player_frm.grid(row=0, column=2, sticky=N)
self.play_video_btn = Button(self.video_player_frm, text="Open Video", command=self.play_video)
self.play_video_btn.grid(sticky=N, pady=10)
self.video_key_lbls = Label(self.video_player_frm,
text="\n\n Keyboard shortcuts for video navigation: \n p = Pause/Play"
"\n\n After pressing pause:"
"\n o = +2 frames \n e = +10 frames \n w = +1 second"
"\n\n t = -2 frames \n s = -10 frames \n x = -1 second"
"\n\n q = Close video window \n\n")

self.video_key_lbls.grid(sticky=W)
Label(self.video_player_frm, text=self.video_presses_lbl).grid(sticky=W)
self.update_img_from_video = Button(self.video_player_frm, text="Show current video frame", command=self.update_frame_from_video)
self.update_img_from_video.grid(sticky=N)
self.bind_shortcut_keys()
Expand All @@ -209,6 +203,12 @@ def bind_shortcut_keys(self):
self.main_window.bind(self.img_kbd_bindings['last_frame']['kbd'], lambda x: self.advance_frame(new_frm_number=self.max_frm_no))
self.main_window.bind(self.img_kbd_bindings['first_frame']['kbd'], lambda x: self.advance_frame(0))

def __create_video_key_presses_lbl(self):
self.video_presses_lbl = "\n\n Keyboard shortcuts for video navigation: "
for k, v in self.video_kbd_bindings.items():
self.video_presses_lbl += '\n'
self.video_presses_lbl += v['label']

def find_last_next_annotation(self, forwards: bool, present: bool):
if forwards:
sliced = self.data_df_targets.loc[self.current_frm_n.get() + 1 :,].sum(
Expand Down Expand Up @@ -558,5 +558,5 @@ def select_labelling_video_advanced(
)
#
#
# test = select_labelling_video_advanced(config_path='/Users/simon/Desktop/envs/simba/troubleshooting/two_black_animals_14bp/project_folder/project_config.ini',
# test = select_labelling_video_advanced(config_path=r"C:\troubleshooting\two_black_animals_14bp\project_folder\project_config.ini",
# continuing=False)
59 changes: 31 additions & 28 deletions simba/labelling/labelling_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
check_int, check_that_column_exist)
from simba.utils.enums import Options, TagNames
from simba.utils.errors import FrameRangeError, NoDataError
from simba.utils.lookups import get_labelling_img_kbd_bindings
from simba.utils.lookups import get_labelling_img_kbd_bindings, get_labelling_video_kbd_bindings
from simba.utils.printing import log_event, stdout_success
from simba.utils.read_write import (get_all_clf_names, get_fn_ext,
get_video_meta_data, read_config_entry,
read_df, write_df)
from simba.utils.read_write import (get_all_clf_names, get_fn_ext, get_video_meta_data, read_config_entry, read_df, write_df)

PLAY_VIDEO_SCRIPT_PATH = os.path.join(os.path.dirname(simba.__file__), "labelling/play_annotation_video.py")
PADDING = 5
Expand Down Expand Up @@ -72,7 +70,9 @@ def __init__(self,
self.video_path = file_path
self.cap = cv2.VideoCapture(self.video_path)
self.img_kbd_bindings = get_labelling_img_kbd_bindings()
self.__create_key_presses_lbl()
self.video_kbd_bindings = get_labelling_video_kbd_bindings()
self.__create_frm_key_presses_lbl()
self.__create_video_key_presses_lbl()
self.video_meta_data = get_video_meta_data(video_path=self.video_path)
self.frame_lst = list(range(0, self.video_meta_data["frame_count"]))
self.max_frm_no = max(self.frame_lst)
Expand Down Expand Up @@ -188,29 +188,30 @@ def __init__(self,
self.video_player_frm.grid(row=0, column=2, sticky=N)
self.play_video_btn = Button(self.video_player_frm, text="Open Video", command=self.__play_video)
self.play_video_btn.grid(sticky=N, pady=10)
self.video_key_lbls = Label(
self.video_player_frm,
text="\n\n Keyboard shortcuts for video navigation: \n p = Pause/Play"
"\n\n After pressing pause:"
"\n o = +2 frames \n e = +10 frames \n w = +1 second"
"\n\n t = -2 frames \n s = -10 frames \n x = -1 second"
"\n\n q = Close video window \n\n",
)
self.video_key_lbls.grid(sticky=W)
Label(self.video_player_frm, text=self.video_presses_lbl).grid(sticky=W)
self.update_img_from_video = Button(self.video_player_frm, text="Show current video frame", command=self.__update_frame_from_video)
self.update_img_from_video.grid(sticky=N)
self.__bind_shortcut_keys()
self.__bind_frm_shortcut_keys()


Label(self.video_player_frm, text=self.key_presses_lbl).grid(sticky=S)
self.__advance_frame(new_frm_number=self.frm_no)
self.main_window.mainloop()

def __create_key_presses_lbl(self):
def __create_frm_key_presses_lbl(self):
self.key_presses_lbl = "\n\n Keyboard shortcuts for frame navigation: "
for k, v in self.img_kbd_bindings.items():
self.key_presses_lbl += '\n'
self.key_presses_lbl += v['label']

def __bind_shortcut_keys(self):
def __create_video_key_presses_lbl(self):
self.video_presses_lbl = "\n\n Keyboard shortcuts for video navigation: "
for k, v in self.video_kbd_bindings.items():
self.video_presses_lbl += '\n'
self.video_presses_lbl += v['label']


def __bind_frm_shortcut_keys(self):
self.main_window.bind(self.img_kbd_bindings['save']['kbd'], lambda x: self.__save_results())
self.main_window.bind(self.img_kbd_bindings['frame+1_keep_choices']['kbd'], lambda x: self.__advance_frame(new_frm_number=int(self.current_frm_n.get() + 1), save_and_keep_checks=True))
self.main_window.bind(self.img_kbd_bindings['print_annotation_statistic']['kbd'], lambda x: self.print_annotation_statistics())
Expand All @@ -219,6 +220,9 @@ def __bind_shortcut_keys(self):
self.main_window.bind(self.img_kbd_bindings['last_frame']['kbd'], lambda x: self.__advance_frame(new_frm_number=self.max_frm_no))
self.main_window.bind(self.img_kbd_bindings['first_frame']['kbd'], lambda x: self.__advance_frame(0))




def print_annotation_statistics(self):
table_view = [["Video name", self.video_name], ["Video frames", self.video_meta_data["frame_count"]]]
for target in self.target_lst:
Expand Down Expand Up @@ -263,17 +267,17 @@ def __advance_frame(self, new_frm_number: int, save_and_keep_checks=False):
print("FRAME {} CANNOT BE SHOWN - YOU ARE VIEWING THE FINAL FRAME OF THE VIDEO (FRAME NUMBER {})".format(str(new_frm_number), str(self.max_frm_no)))
self.current_frm_n = IntVar(value=self.max_frm_no)
self.change_frm_box.delete(0, END)
self.change_frm_box.insert(0, self.current_frm_n.get())
self.change_frm_box.insert(0, str(self.current_frm_n.get()))
elif new_frm_number < 0:
print("FRAME {} CANNOT BE SHOWN - YOU ARE VIEWING THE FIRST FRAME OF THE VIDEO (FRAME NUMBER {})".format(str(new_frm_number), str(self.max_frm_no)))
self.current_frm_n = IntVar(value=0)
self.change_frm_box.delete(0, END)
self.change_frm_box.insert(0, self.current_frm_n.get())
self.change_frm_box.insert(0, str(self.current_frm_n.get()))
else:
self.__create_print_statements()
self.current_frm_n = IntVar(value=new_frm_number)
self.change_frm_box.delete(0, END)
self.change_frm_box.insert(0, self.current_frm_n.get())
self.change_frm_box.insert(0, str(self.current_frm_n.get()))
if not save_and_keep_checks:
for target in self.target_lst:
self.checkboxes[target]["var"].set(bool(self.data_df_targets[target].loc[int(self.current_frm_n.get())]))
Expand Down Expand Up @@ -351,13 +355,12 @@ def __create_print_statements(
)


def select_labelling_video(
config_path: Union[str, os.PathLike],
threshold_dict: Optional[Dict[str, float]] = None,
setting: str = None,
continuing: bool = None,
video_file_path=Union[str, os.PathLike],
):
def select_labelling_video(config_path: Union[str, os.PathLike],
threshold_dict: Optional[Dict[str, float]] = None,
setting: str = None,
continuing: bool = None,
video_file_path=Union[str, os.PathLike]):

if setting is not "pseudo":
video_file_path = filedialog.askopenfilename(
filetypes=[("Video files", Options.ALL_VIDEO_FORMAT_STR_OPTIONS.value)]
Expand All @@ -383,7 +386,7 @@ def select_labelling_video(
)


# test = select_labelling_video(config_path='/Users/simon/Desktop/envs/simba/troubleshooting/two_black_animals_14bp/project_folder/project_config.ini',
# test = select_labelling_video(config_path=r"C:\troubleshooting\two_black_animals_14bp\project_folder\project_config.ini",
# threshold_dict={'Attack': 0.4},
# setting='from_scratch',
# continuing=False)
Loading

0 comments on commit 6a9f886

Please sign in to comment.