def check_skeleton(self, skeleton: dict): """ Checking skeleton for trigger :param skeleton: a skeleton dictionary, returned by calculate_skeletons() from poser file :return: response, a tuple of result (bool) and response body Response body is used for plotting and outputting results to trials dataframes """ direction_x, direction_y = self._point head_dir, angle = angle_between_vectors(direction_x, direction_y, *skeleton[self._bodyparts[0]], *skeleton[self._bodyparts[1]]) true_angle = 180 - abs(angle) result = true_angle <= self._angle color = (0, 255, 0) if result else (0, 0, 255) if self._debug: response_body = { 'plot': { 'line': dict(pt1=skeleton[self._bodyparts[0]], pt2=self._point, color=color), 'text': dict(text=str(true_angle), org=skeleton[self._bodyparts[0]], color=(255, 255, 255)) } } else: response_body = {'angle': true_angle} response = (result, response_body) return response
def check_skeleton(self, skeleton: dict): """ Checking skeleton for trigger :param skeleton: a skeleton dictionary, returned by calculate_skeletons() from poser file :return: response, a tuple of result (bool) and response body Response body is used for plotting and outputting results to trials dataframes ['tailroot', 'neck', 'nose'] you need to pass this to angle between vectors to get headdirection """ tailroot_x, tailroot_y = skeleton["tailroot"] neck_x, neck_y = skeleton["neck"] nose_x, nose_y = skeleton["nose"] ret_head_dir, angle = angle_between_vectors(tailroot_x, tailroot_y, neck_x, neck_y, nose_x, nose_y) true_angle = 180 - abs(angle) if true_angle <= self._angle: if self._head_dir == ret_head_dir: result = True elif self._head_dir == "both": result = True else: result = False color = (0, 255, 0) if result else (0, 0, 255) if self._debug: center = (nose_x, nose_y) response_body = { "plot": { "text": dict( text=str(true_angle), org=skeleton[self._end_point], color=(255, 255, 255), ), "circle": dict(center=center, radius=5, color=color), } } else: response_body = {"angle": true_angle} response = (result, response_body) return response
def check_skeleton(self, skeleton: dict): """ Checking skeleton for trigger :param skeleton: a skeleton dictionary, returned by calculate_skeletons() from poser file :return: response, a tuple of result (bool) and response body Response body is used for plotting and outputting results to trials dataframes [point , 'neck', 'nose'] is used for headdirection """ _, angle = angle_between_vectors(*skeleton["neck"], *skeleton["nose"], *self._center) true_angle = abs(angle) result_head, _ = self._headdirection_trigger.check_skeleton(skeleton) result_roi, _ = self._region_trigger.check_skeleton(skeleton) if result_head is True and result_roi is False: result = True else: result = False color = (0, 255, 0) if result else (0, 0, 255) if self._debug: point = skeleton["nose"] response_body = { "plot": { "text": dict(text=str(true_angle), org=point, color=(255, 255, 255)), "circle": dict(center=self._center, radius=self._radius, color=color), } } else: response_body = {"angle": true_angle} response = (result, response_body) return response
def check_skeleton(self, skeleton: dict): """ Checking skeleton for trigger :param skeleton: a skeleton dictionary, returned by calculate_skeletons() from poser file :return: response, a tuple of result (bool) and response body Response body is used for plotting and outputting results to trials dataframes ['neck', 'nose', 'tailroot'] you need to pass this to angle between vectors to get headdirection """ ret_head_dir, angle = angle_between_vectors( *skeleton[self._bodyparts[0]], *skeleton[self._bodyparts[1]], *skeleton[self._bodyparts[2]]) true_angle = abs(angle) if true_angle <= self._angle: if self._head_dir == ret_head_dir: result = True elif self._head_dir == 'both': result = True else: result = False color = (0, 255, 0) if result else (0, 0, 255) if self._debug: center = skeleton[self._bodyparts[1]] response_body = { 'plot': { 'text': dict(text=str(true_angle), org=skeleton[self._bodyparts[1]], color=(255, 255, 255)), 'circle': dict(center=center, radius=5, color=color) } } else: response_body = {'angle': true_angle} response = (result, response_body) return response
def check_skeleton(self, skeleton: dict): """ Checking skeleton for trigger :param skeleton: a skeleton dictionary, returned by calculate_skeletons() from poser file :return: response, a tuple of result (bool) and response body Response body is used for plotting and outputting results to trials dataframes """ start_x, start_y = skeleton[self._start_point] end_x, end_y = skeleton[self._end_point] direction_x, direction_y = self._point head_dir, angle = angle_between_vectors(direction_x, direction_y, start_x, start_y, end_x, end_y) true_angle = 180 - abs(angle) result = true_angle <= self._angle color = (0, 255, 0) if result else (0, 0, 255) if self._debug: response_body = { "plot": { "line": dict(pt1=skeleton[self._end_point], pt2=self._point, color=color), "text": dict( text=str(true_angle), org=skeleton[self._end_point], color=(255, 255, 255), ), } } else: response_body = {"angle": true_angle} response = (result, response_body) return response
def check_skeleton(self, frame, skeleton): if self._experiment_timer.check_timer(): if self._total_time >= self._max_total_time: # check if total time to stimulate per experiment is reached print("Ending experiment, total event time ran out") self.stop_experiment() else: # if not continue if not self._intertrial_timer.check_timer(): # check if there is an intertrial time running right now, if not continue # check if the headdirection angle is within limits _, angle_point = angle_between_vectors( *skeleton["neck"], *skeleton["nose"], *self._point) if self._start_angle <= angle_point <= self._end_angle: if not self._event: # if a stimulation event wasn't started already, start one print("Starting Stimulation") self._event = True # and activate the laser, start the timer and reset the intertrial timer laser_switch(True) self._event_start = time.time() self._intertrial_timer.reset() else: if time.time( ) - self._event_start <= self._max_trial_time: # if the total event time has not reached the maximum time per event # self._trial_time = time.time() - self._event_start pass else: # if the maximum event time was reached, reset the event, # turn off the laser and start intertrial time print( "Ending Stimulation, Stimulation time ran out" ) self._event = False # laser_toggle(False) laser_switch(False) # self._trial_time = time.time() - self._event_start trial_time = time.time() - self._event_start self._total_time += trial_time self._results.append(trial_time) print("Stimulation duration", trial_time) self._intertrial_timer.start() else: # if the headdirection is not within the parameters if self._event: # but the stimulation is still going if time.time( ) - self._event_start < self._min_trial_time: # check if the minimum event time was not reached, then pass pass else: # if minumum event time has been reached, reset the event, # turn of the laser and start intertrial time print("Ending Stimulation, angle not in range") self._event = False # laser_toggle(False) laser_switch(False) # self._trial_time = time.time() - self._event_start trial_time = time.time() - self._event_start self._total_time += trial_time self._results.append(trial_time) print("Stimulation duration", trial_time) self._intertrial_timer.start() else: # if maximum experiment time was reached, stop experiment print("Ending experiment, timer ran out") self.stop_experiment() return self._event
def check_skeleton(self, skeleton: dict): """ Checking skeleton for trigger :param skeleton: a skeleton dictionary, returned by calculate_skeletons() from poser file :return: response, a tuple of result (bool) and response body Response body is used for plotting and outputting results to trials dataframes """ if isinstance(self._roi_bodyparts, list): roi_result = [] for part in self._roi_bodyparts: bp_x, bp_y = skeleton[part] roi_result.append( self._region_of_interest.check_point(bp_x, bp_y)) roi_result = any(roi_result) else: bp_x, bp_y = skeleton[self._roi_bodyparts] roi_result = self._region_of_interest.check_point(bp_x, bp_y) ret_head_dir, angle = angle_between_vectors( *skeleton[self._angle_bodyparts[0]], *skeleton[self._angle_bodyparts[1]], *self._point) true_angle = abs(angle) if true_angle <= self._angle: angle_result = True else: angle_result = False if angle_result and roi_result: result = True else: result = False color = (0, 255, 0) if result else (0, 0, 255) if self._roi_type == 'circle': response_body = { 'plot': { 'circle': dict(center=self._region_of_interest.get_center(), radius=int(self._region_of_interest.get_x_radius()), color=color), 'text': dict(text=str(true_angle), org=skeleton[self._angle_bodyparts[1]], color=(255, 255, 255)) } } elif self._roi_type == 'square': box = self._region_of_interest.get_box() x1, y1, x2, y2 = box pt1 = (x1, y2) pt2 = (x2, y1) response_body = { 'plot': { 'square': dict(pt1=pt1, pt2=pt2, color=color), 'text': dict(text=str(true_angle), org=skeleton[self._angle_bodyparts[1]], color=(255, 255, 255)) } } response = (result, response_body) return response