def test_viterbi(observations, smooth): """ Method that compute the accuracy of Viterbi algorithm on the Test Set :param observations: words of test set :param smooth: Type of smoothing to apply :return: Accuracy on test set """ emission_matrix_path = './data/' + config_data.dataset + '/emission_matrix/emission_matrix_test_' + \ config_data.dataset + smooth + '.json' sentences = test_manager.get_sentences() test_emission_matrix = em_matrix.get_emission_matrix( emission_matrix_path, observations) progr_bar_length = len(sentences) print_progress_bar(0, progr_bar_length, prefix='Progress:', suffix='Complete', length=50) viterbi_result = [] for pb_index, sent in enumerate(sentences): print_progress_bar(pb_index + 1, progr_bar_length, prefix='Progress:', suffix='Complete', length=50) # viterbi_result += refine_result(viterbi.viterbi(sent, test_emission_matrix)) viterbi_result += viterbi.viterbi(sent, test_emission_matrix) print("Viterbi accuracy: {}".format( evaluator.compute_accuracy(viterbi_result)))
def start(self) -> None: """ Start file download :return: None """ try: sock = self.__connect(self.host_ip4, self.host_ip6, self.host_port, self.packet) except socket.error as e: shell_colors.print_red( f'\nImpossible to send data to {self.host_ip4}|{self.host_ip6} [{self.host_port}]: {e}\n' ) return ack = sock.recv(4).decode() if ack != "ARET": shell_colors.print_red( f'\nInvalid command received: {ack}. Expected: ARET\n') sock.close() return total_chunks = int(sock.recv(6).decode()) shell_colors.print_blue(f'\n#chunk: {total_chunks}') try: f_obj = open('shared/' + self.file_name, 'wb') except OSError as e: shell_colors.print_red(f'\nSomething went wrong: {e}\n') raise e progress_bar.print_progress_bar(0, total_chunks, prefix='Downloading:', suffix='Complete', length=50) for i in range(total_chunks): chunk_size = sock.recv(5) # if not all the 5 expected bytes has been received while len(chunk_size) < 5: chunk_size += sock.recv(1) chunk_size = int(chunk_size) data = sock.recv(chunk_size) # if not all the expected bytes has been received while len(data) < chunk_size: data += sock.recv(1) f_obj.write(data) progress_bar.print_progress_bar(i + 1, total_chunks, prefix='Downloading:', suffix='Complete', length=50) f_obj.close()
def test_baseline(): """ Method that compute the accuracy of the Baseline Tagging on the test set :return: Baseline accuracy """ sentences = test_manager.get_sentences() progr_bar_length = len(sentences) print_progress_bar(0, progr_bar_length, prefix='Progress:', suffix='Complete', length=50) baseline_result = [] for pb_index, sent in enumerate(sentences): print_progress_bar(pb_index + 1, progr_bar_length, prefix='Progress:', suffix='Complete', length=50) baseline_result += baseline.compute_baseline(sent) print("Baseline accuracy mean: {}".format( evaluator.compute_accuracy(baseline_result)))
def find_center_of_rotation(video: str, max_dist: float = 5, min_consecutive: int = 2) -> tuple: cap = cv2.VideoCapture(video) num_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) cnt = 0 pointCollector = StablePointsCollector(max_dist, min_consecutive) while True: frame, cnt = grab_next_frame_with_led_on(cap, cnt) if frame is not None: undist = cvt.undo_distortion(frame) undist = cv2.resize( undist, (int(frame.shape[1] / 3), int(frame.shape[0] / 3))) pt, vis, centroid = cvt.get_rod_reference_point(undist) cv2.circle(vis, (int(pt[0]), int(pt[1])), 2, (0, 250, 255), 3) pt[1] = undist.shape[0] - pt[1] pointCollector.append(pt) scat = plt.scatter(pointCollector.pt_x, pointCollector.pt_y, c="r") plt.xlim([0, undist.shape[1]]) plt.ylim([0, undist.shape[0]]) plt.draw() plt.pause(0.001) print_progress_bar(cnt + 1, num_frames, prefix='Progress:', suffix='Complete, numPoints: ' + str(pointCollector.num_points), length=50) cv2.imshow("Frame", undist) if cnt >= num_frames: break print_progress_bar(num_frames, num_frames, prefix='Progress:', suffix='Complete, numPoints: ' + str(pointCollector.num_points), length=50) a = fe.fitEllipse(np.asarray(pointCollector.pt_x), np.asarray(pointCollector.pt_y)) center = fe.ellipse_center(a) fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'}) ax.set_xlim(0, undist.shape[1]) ax.set_ylim(0, undist.shape[0]) scat = plt.scatter(pointCollector.pt_x, pointCollector.pt_y, c="r") plt.plot(center[0], center[1], 'o') plt.show() plt.show() return (center[0] / undist.shape[1], (undist.shape[0] - center[1]) / undist.shape[0])
cv2.putText(vis, header, (10, 20), cv2.FONT_HERSHEY_DUPLEX, .45, (125, 125, 0)) elif led_triggered: # save answer and pause logging log.write(log_line) header = "trial #" + str(trial_cnt) + " gt_angle: " + str(alpha) cv2.putText(vis, header, (10, 20), cv2.FONT_HERSHEY_DUPLEX, .45, (125, 125, 0)) ans = "ans: " + str(gamma) cv2.putText(vis, ans, (10, 50), cv2.FONT_HERSHEY_DUPLEX, .45, (125, 125, 0)) led_triggered = False trial_started = False ready_to_start = False elif trial_started is False: cv2.putText(vis, "waiting for trial start...", (10, 20), cv2.FONT_HERSHEY_DUPLEX, .45, (125, 125, 0)) print_progress_bar(frame_cnt + 1, num_frames, prefix='Progress:', suffix='Complete', length=50) cv2.imshow("Frame", vis) cv2.waitKey(1) if frame_cnt == num_frames: print("\n All done, bye.") break