def computeHomography(xy: list, is_random: bool) -> (np.array): x = 0 # xx coordinate y = 1 # yy coordinate frame = 0 # video frame template = 1 # template frame # Compute homography matrix -> A@H = B A = [] B = [] # Uses 4 points because the homography has 8 degrees of freedom loop = 4 if is_random else len(xy[0]) for i in range(loop): if is_random: # Get random points random_point = rdr(0, len(xy[frame])) random_point = (xy[frame][random_point], xy[template][random_point]) else: # iterates for all points random_point = [xy[0][i], xy[1][i]] x_frame = random_point[frame][x] y_frame = random_point[frame][y] x_template = random_point[template][x] y_template = random_point[template][y] A.append([ x_frame, y_frame, 1, 0, 0, 0, -x_frame * x_template, -y_frame * x_template ]) A.append([ 0, 0, 0, x_frame, y_frame, 1, -x_frame * y_template, -y_frame * y_template ]) # coordinates at template frame B.append(x_template) B.append(y_template) A = np.array(A) B = np.array(B) H = None try: # in case of singular matrix # Sum of Least Squares H = inv(A.T @ A) @ (A.T @ B) H = np.append(H, 1) # appends H9 which was the normalization value H = H.reshape(3, 3) except: pass return H
def pendu(): game = True global life word = liste[rdr(len(liste))] seek_word = "".join(["*" for x in word]) while life > 0 and game is True: print("word: {}".format(seek_word)) letter = get_letter().upper() if letter in word: seek_word = put_letter(word, seek_word, letter) if seek_word == word: game = False else: life -= 1 print("life left: {}.".format(life)) if life == 0: print("you loose ! The word was {}.".format(word))
def multiple_main_value(data: list) -> str: direction = rdr(len(dictionary['Correlation_3'])) # random word oposite_direction = 0 if direction == 1 else 1 # random word oposite tmp = rnd(dictionary['Correlation_4']).format( hyperlink(data[0]), dictionary['Correlation_3'][direction], rnd(dictionary['Correlation_5'])) aux = increase_or_decrease(data[1]) # separete regarding the direction # at least one flag is True flag_0 = True if aux[0] != [] else False flag_1 = True if aux[1] != [] else False if not (flag_0 and flag_1): # only one direction dir_opcional = oposite_direction if len( aux[0]) > 0 else direction # relate with main variable col = 0 if len(aux[0]) > 0 else 1 tmp += rnd(dictionary['Correlation_6']).format( array_names=hyperlink([x[0] for x in aux[col]]), dir=dictionary['Correlation_3'][dir_opcional], singular_='s' if len(aux[col]) == 1 else '', array_values= f"({'; '.join([str(round(x[1],2)) for x in aux[col]])})") else: # both directions were found tmp += dictionary['Correlation_6'][1].format( array_names=hyperlink([x[0] for x in aux[1]]), dir=dictionary['Correlation_3'][direction], singular_='s' if len(aux[1]) == 1 else '', array_values=f"({'; '.join([str(round(x[1],2)) for x in aux[1]])})" ) tmp += rnd(dictionary['preposition_contrast']) tmp += dictionary['Correlation_6'][0].format( array_names=hyperlink([x[0] for x in aux[0]]), dir=dictionary['Correlation_3'][oposite_direction], singular_='s' if len(aux[0]) == 1 else '', array_values=f"({'; '.join([str(round(x[1],2)) for x in aux[0]])})" ) return tmp
def calculate_homography_matrix(points): x = 0 # x coordinate y = 1 # y coordinate video_frame_index = 0 # The frame is the first in the points array (composed of two lists, one for the frame and the other for the template) template_index = 1 # The template is the second in the points array A = [] B = [] for point_index in range(4): random_point = rdr(0, len(points[video_frame_index])) point = (points[video_frame_index][random_point], points[template_index][random_point]) video_frame_x = point[video_frame_index][x] video_frame_y = point[video_frame_index][y] template_x = point[template_index][x] template_y = point[template_index][y] # Build A matrix A.append([ video_frame_x, video_frame_y, 1, 0, 0, 0, -video_frame_x * template_x, -video_frame_y * template_x ]) A.append([ 0, 0, 0, video_frame_x, video_frame_y, 1, -video_frame_x * template_y, -video_frame_y * template_y ]) # Build B matrix B.append(template_x) B.append(template_y) A = np.array(A) B = np.array(B) H = None try: # if it's a singular matrix it will give an error H = inv(A.T @ A) @ (A.T @ B) H = np.append(H, 1) # we use h9=1 H = H.reshape(3, 3) except: pass return H
def ransac(pc1: np.array, pc2: np.array, xy: list, dim: tuple, percentage_inliers_threshold=0.5) -> (tuple): num_itr = len(xy[0]) // 4 # percentage_inliers_threshold = 0.5 # percentage dist_inliers_threshold = 0.2 # meters # best values r_best = np.zeros((3, 3), dtype=float) t_best = np.zeros((3, 1), dtype=float) dist_best = np.Inf pc_try_best = None max_inliers = 0 best_inliers = [[], []] already_nice_rt = False max_inliers_tuned = 0 pos_vect = lambda x, y: (round(x) * dim[0] + round(y)) if dim[0] > dim[ 1] else (round(x) + round(y) * dim[1]) pos_vect_inv = lambda idx: (idx // dim[0], idx % dim[0]) if dim[0] > dim[ 1] else (idx % dim[1], idx // dim[1]) #rigid_transformation = lambda r, t, pc: np.concatenate((r, t), axis=1)@np.concatenate( (pc, np.ones((1, pc.shape[1]), dtype=float)), axis=0 ) rigid_transformation = lambda r, t, pc: r @ pc + t for i in range(num_itr): points = 4 points_vect = [[], []] for i in range(points): random_point = rdr(0, len(xy[0])) # bicates 'aleatori' point idx_1 = pos_vect(*xy[0][random_point]) idx_2 = pos_vect(*xy[1][random_point]) points_vect[0].append(idx_1) points_vect[1].append(idx_2) r, t = procrustes(pc1[:, points_vect[0]], pc2[:, points_vect[1]]) # pc_rgb1, pc_rgb2 pc_1_try = rigid_transformation(r, t, pc2) num_inliers = 0 inliers = [[], []] for pt in range(len(xy[0])): # count number of inliers dist = np.linalg.norm(pc1[:, pos_vect(*xy[0][pt])] - pc_1_try[:, pos_vect(*xy[0][pt])], axis=0) if dist < dist_inliers_threshold: # check if it is an inlier inliers[0].append(pos_vect(*xy[0][pt])) inliers[1].append(pos_vect(*xy[1][pt])) num_inliers += 1 if (num_inliers == 0): continue # tuning model - adjust RT based on point cloud already found r_tuning, t_tuning = procrustes( pc1[:, [inliers[0], inliers[1]][0]], pc_1_try[:, [inliers[0], inliers[1]][0]]) r_tuned = r_tuning @ r t_tuned = t_tuning + t pc_tuned = rigid_transformation(r_tuned, t_tuned, pc2) num_inliers_tuned = check_num_inlers(pc1, pc_tuned, xy, dist_inliers_threshold, pos_vect) if (num_inliers_tuned > num_inliers): # checks if tuning improves the transformation r = r_tuned t = t_tuned pc_1_try = pc_tuned num_inliers = num_inliers_tuned if (num_inliers > percentage_inliers_threshold * len(xy[0])): # found a nice rigid transform r_best, t_best = (r, t) pc_try_best = pc_1_try max_inliers = num_inliers best_inliers = [inliers[0], inliers[1]] best_inliers = best_inliers.copy() already_nice_rt = True max_inliers_tuned = max(num_inliers_tuned, max_inliers_tuned) break elif (max_inliers < num_inliers): # updates the best rigid transform best_inliers = [inliers[0], inliers[1]] best_inliers = best_inliers.copy() max_inliers = num_inliers r_best, t_best = (r, t) pc_try_best = pc_1_try max_inliers_tuned = max(num_inliers_tuned, max_inliers_tuned) print(f'Number of inliers ransac {max_inliers}', end='\t >> \t') has_tuned = '\t:)' if max_inliers_tuned == max_inliers else ':(\t' print( f'Tuning: {max_inliers_tuned}\t{has_tuned}\t{"[ *** Found a nice [RT] !! :) *** ]" if already_nice_rt else ""}\t', flush=True) ratio_inliers = max_inliers / len(xy[0]) return (r_best, t_best, ratio_inliers, pc_try_best)
def keep_alive(self): """Randomizes mouse movement""" while True: self.move_relative(rdr(8), rdr(8)) slp(rdr(10, 30))