Ejemplo n.º 1
0
    def check_skeleton(self, frame, skeleton):
        status, trial = self._process.get_status()
        if status:
            current_trial = self._trials[trial]
            condition, response = current_trial["trigger"].check_skeleton(
                skeleton)
            self._process.pass_condition(condition)
            result = self._process.get_result()
            if result is not None:
                self.process_result(result, trial)
                self._chosen_trial = None
                print(self._completion_counter)
                # check if all trials were successful until completion
                if all(self._completion_counter.values()):
                    print("Experiment is finished")
                    print("All trials reached required amount of successes")
                    self.stop_experiment()

                # if not continue
                self._iti_duration = next(self._iti_list, False)
                self._intertrial_timer = Timer(self._iti_duration)
                print(" Going into InterTrialTime for " +
                      str(self._iti_duration) + " sec.")

                self._intertrial_timer.start()
            result = None
            plot_triggers_response(frame, response)

        # elif not self._intertrial_timer.check_timer() and not self._penalty_timer.check_timer():
        elif not self._intertrial_timer.check_timer():
            # chosen_trial = random.choice(list(self._experiment['possible_trials'].keys()))
            if self._chosen_trial is None:
                self._chosen_trial = next(self._trials_list, False)
            elif not self._chosen_trial:
                print("Experiment is finished due to max. trial number.")
                print(self._result_list)
                self.stop_experiment()
            elif self._counter["result"][("Greenbar_whiteback", True)] >= 20:
                print("Reached max amount of CS+ trial successes!")
                print(self._result_list)
                self.stop_experiment()
            else:
                # if self._counter['trial']['Greenbar_whiteback'] >= 10:
                #     # check if 10 green trials have passed and change trigger stage
                #     self._stage = 2
                if self.check_triggers(skeleton):
                    # check trial start triggers
                    self._process.set_trial(self._chosen_trial)
                    self._print_check = False
                elif not self._print_check:
                    print("Next trial: #" + str(len(self._result_list) + 1) +
                          " " + self._chosen_trial)
                    print(
                        "Animal is not meeting trial start criteria, the start of trial is delayed."
                    )
                    self._print_check = True
                    # self._penalty_timer.reset()
                    # self._penalty_timer.start()
        # if experimental time ran out, finish experiments
        self.check_exp_timer()
Ejemplo n.º 2
0
    def check_skeleton(self, frame, skeleton):
        """
        Checking each passed animal skeleton for a pre-defined set of conditions
        Outputting the visual representation, if exist
        Advancing trials according to inherent logic of an experiment
        :param frame: frame, on which animal skeleton was found
        :param skeleton: skeleton, consisting of multiple joints of an animal
        """
        self.check_exp_timer()  # checking if experiment is still on

        if self._event_count >= self._settings_dict['EXP_LENGTH']:
            self.stop_experiment()

        elif not self.experiment_finished:
            if not self._intertrial_timer.check_timer():
                # check if condition is met
                result, response = self._trigger.check_skeleton(
                    skeleton=skeleton)
                if result:
                    self._event_count += 1
                    print('Stimulation #{self._event_count}'.format())
                    self._intertrial_timer.reset()
                    self._intertrial_timer.start()

                plot_triggers_response(frame, response)
                self._process.put(result)
Ejemplo n.º 3
0
    def check_skeleton(self, frame, skeleton):
        """
        Checking each passed animal skeleton for a pre-defined set of conditions
        Outputting the visual representation, if exist
        Advancing trials according to inherent logic of an experiment
        :param frame: frame, on which animal skeleton was found
        :param skeleton: skeleton, consisting of multiple joints of an animal
        """
        self.check_exp_timer()  # checking if experiment is still on
        for trial in self._trial_count:
            # checking if any trial hit a predefined cap
            if self._trial_count[trial] >= 10:
                self.stop_experiment()

        if not self.experiment_finished:
            result, response = False, None
            for trial in self._trials:
                # check for all trials if condition is met
                result, response = self._trials[trial]["trigger"](
                    skeleton=skeleton)
                plot_triggers_response(frame, response)
                if result:
                    if self._current_trial is None:
                        if not self._trial_timers[trial].check_timer():
                            self._current_trial = trial
                            self._trial_timers[trial].reset()
                            self._trial_count[trial] += 1
                            print(trial, self._trial_count[trial])
                else:
                    if self._current_trial == trial:
                        self._current_trial = None
                        self._trial_timers[trial].start()

            self._process.set_trial(self._current_trial)
            return result, response
def start_videoanalyser():
    print("Starting DeepLabCut")
    model = load_model(
        r"D:\DeepPoseKit-Data-master\datasets\fly\best_model_densenet.h5")

    experiment_enabled = False
    video_output = True

    if experiment_enabled:
        print("Initializing experiment")
        experiment = ExampleExperiment()
        experiment.start_experiment()

    # some variables initialization
    all_rows = []
    index = 0

    while video.isOpened():
        ret, frame = video.read()
        if ret:
            scmap, locref, pose = get_pose(frame, config, sess, inputs,
                                           outputs)
            peaks = find_local_peaks_new(scmap, locref, ANIMALS_NUMBER, config)
            skeletons = calculate_skeletons(peaks, ANIMALS_NUMBER)
            if skeletons:
                for skeleton in skeletons:
                    if experiment_enabled:
                        result, response = experiment.check_skeleton(
                            frame, skeleton)
                        plot_triggers_response(frame, response)
                out_frame = plot_bodyparts(frame, skeletons)
            else:
                out_frame = frame
            cv2.imshow('stream', out_frame)
            if video_output:
                video_file.write(out_frame)
            if experiment_enabled:
                all_rows.append(
                    create_row(index, skeletons, experiment_enabled,
                               experiment.get_trial()))
            else:
                all_rows.append(
                    create_row(index, skeletons, experiment_enabled, None))
            index += 1
        else:
            break

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    if experiment_enabled:
        experiment.stop_experiment()
    if video_output:
        print('Saving analyzed video for {}'.format(video_name))
        video_file.release()
    video.release()
    create_dataframes(all_rows)
Ejemplo n.º 5
0
    def check_skeleton(self, frame, skeleton):
        status, trial = self._process.get_status()
        if status:
            current_trial = self._trials[trial]
            condition, response = current_trial['trigger'].check_skeleton(
                skeleton)
            self._process.put(condition)
            result = self._process.get_result()
            if result is not None:
                self.process_result(result, trial)
                self._current_trial = None
                # check if all trials were successful until completion
                if self._success_count >= self._settings_dict['EXP_COMPLETION']:
                    print("Experiment is finished")
                    print("Trial reached required amount of successes")
                    self.stop_experiment()

                # if not continue
                print(' Going into Intertrial time.')
                self._intertrial_timer.reset()
                self._intertrial_timer.start()
            result = None
            plot_triggers_response(frame, response)

        elif not self._intertrial_timer.check_timer():
            if self._current_trial is None:
                self._current_trial = next(self._trials_list, False)
            elif not self._current_trial:
                print("Experiment is finished due to max. trial repetition.")
                print(self._result_list)
                self.stop_experiment()
            else:
                init_result, response_body = self._init_trigger.check_skeleton(
                    skeleton)
                if init_result:
                    # check trial start triggers
                    self._process.put_trial(self._trials[self._current_trial],
                                            self._current_trial)
                    self._print_check = False
                elif not self._print_check:
                    print('Next trial: #' + str(len(self._result_list) + 1) +
                          ' ' + self._current_trial)
                    print(
                        'Animal is not meeting trial start criteria, the start of trial is delayed.'
                    )
                    self._print_check = True
        # if experimental time ran out, finish experiments
        super().check_exp_timer()
Ejemplo n.º 6
0
    def check_skeleton(self, frame, skeletons):
        """
        Checking passed animal skeletons for a pre-defined set of conditions
        Outputting the visual representation, if exist
        Advancing trials according to inherent logic of an experiment
        :param frame: frame, on which animal skeleton was found
        :param skeletons: skeletons, consisting of multiple joints of an animal
        """
        self.check_exp_timer()  # checking if experiment is still on
        for trial in self._trial_count:
            # checking if any trial hit a predefined cap
            if self._trial_count[trial] >= self._max_reps:
                self.stop_experiment()

        if not self.experiment_finished:
            result, response = False, None
            #checking if enough animals were detected
            if len(skeletons) >= self._min_animals:
                for trial in self._trials:
                    # check if social interaction trigger is true
                    result, response = self._trials[trial]['trigger'](
                        skeletons=skeletons)
                    plot_triggers_response(frame, response)
                    if result:
                        if self._current_trial is None:
                            if not self._trial_timers[trial].check_timer():
                                self._current_trial = trial
                                self._trial_timers[trial].reset()
                                self._trial_count[trial] += 1
                                print(trial, self._trial_count[trial])
                    else:
                        if self._current_trial == trial:
                            self._current_trial = None
                            self._trial_timers[trial].start()

                self._process.set_trial(self._current_trial)
            else:
                pass
            return result, response
Ejemplo n.º 7
0
    def check_skeleton(self, frame, skeleton):
        """
        Checking each passed animal skeleton for a pre-defined set of conditions
        Outputting the visual representation, if exist
        Advancing trials according to inherent logic of an experiment
        :param frame: frame, on which animal skeleton was found
        :param skeleton: skeleton, consisting of multiple joints of an animal
        """
        self.check_exp_timer()  # checking if experiment is still on

        if not self.experiment_finished:
            result, response = self._trigger.check_skeleton(skeleton=skeleton)
            plot_triggers_response(frame, response)
            if result:
                laser_switch(True)
                self._event_count += 1
                print(self._event_count)
                print("Light on")

            else:
                laser_switch(False)
                print("Light off")

            return result, response