def get_bpms(target_path, visualize=False): paths = glob.glob(target_path + '*.npy') bpms = [] names = [] corrs = [] for path in paths: name = os.path.split(path)[-1][:-4] names.append(name) bpms.append([]) corrs.append([]) data = np.load(path) estimated = data[0] reference = data[1] for x, y in zip(estimated, reference): # correlation coef = np.corrcoef(x, y)[0][1] corrs[-1].append(coef) # calc bpm x_bpms = calc_bpm(x, 'x', visualize) y_bpms = calc_bpm(y, 'y', visualize) diff_min = 9999 bpm = None for x_bpm, y_bpm in zip(x_bpms, y_bpms): if x_bpm != 0 and y_bpm != 0: diff = np.fabs(x_bpm - y_bpm) if diff_min > diff: diff_min = diff bpm = (x_bpm, y_bpm) if bpm is not None: bpms[-1].append(bpm) # show if visualize: if len(corrs[-1]) == 1: print(path) show_signal('estimated', x, 300, background=(255, 255, 255), foreground=(0, 0, 0), padding=5) show_signal('reference', y, 300, background=(255, 255, 255), foreground=(0, 0, 0), padding=5) key = cv2.waitKey(0) if key == 27: break elif key == 's': np.save('sample.npy', [x, y]) return bpms, names, corrs
def experiment_4(self, frame, signal_value): # Append self.frame_buffer.append(frame) self.reference_buffer.append(signal_value) data_len = len(self.frame_buffer) if data_len > 100: reference_signal = self.reference_buffer[-100:] else: reference_signal = self.reference_buffer # Visualize show_signal('Reference', reference_signal, 500) if data_len >= self.fps * 50: print('Saving experiment 4..') np.save(self.path + '4_frame_' + self.name, self.frame_buffer) np.save(self.path + '4_signal_' + self.name, self.reference_buffer) print('Done.') self.reset_experiment() cv2.destroyWindow('Reference') return False else: return True
def experiment_2(self, frame, signal_value): # Append self.frame_buffer.append(frame) self.reference_buffer.append(signal_value) data_len = len(self.frame_buffer) # Calc sin signal radian = self.idx * (2 * np.pi) / ((60.0 / self.bpm) * self.fps) self.sin_signal.append(np.sin(radian)) if data_len > 100: del self.sin_signal[0] reference_signal = self.reference_buffer[-100:] else: reference_signal = self.reference_buffer # Update data self.idx += 1 if self.stop_state == 0: pass elif self.stop_state == 1: self.idx -= 1 elif self.stop_state == 2: pass elif self.stop_state == 3: self.idx -= 1 if data_len > (20 * self.fps) and data_len % (5 * self.fps) == 0: if self.stop_state == 0: self.stop_state = 1 elif self.stop_state == 1: self.stop_state = 2 elif self.stop_state == 2: self.stop_state = 3 elif self.stop_state == 3: self.stop_state = 0 prev_bpm = self.bpm self.bpm += 10 self.idx = (self.idx * prev_bpm) / self.bpm # Visualize show_signal('Sin', self.sin_signal, 500) show_signal('Reference', reference_signal, 500) if data_len >= self.fps * 80: print('Saving experiment 2..') np.save(self.path + '2_frame_' + self.name, self.frame_buffer) np.save(self.path + '2_signal_' + self.name, self.reference_buffer) print('Done.') self.reset_experiment() cv2.destroyWindow('Sin') cv2.destroyWindow('Reference') return False else: return True
def show_selected_signal(): global xs, px, py selected = xs[:, py, px, ...] if len(selected.shape) == 1: show_signal('Channel', selected, 500) else: show_signal('Channel 1', selected[:, 0], 500) show_signal('Channel 2', selected[:, 1], 500) show_signal('Channel 3', selected[:, 2], 500)
def experiment_1(self, frame, signal_value): # Append self.frame_buffer.append(frame) self.reference_buffer.append(signal_value) data_len = len(self.frame_buffer) # Calc sin signal radian = self.idx * (2 * np.pi) / ((60.0 / self.bpm) * self.fps) self.sin_signal.append(np.sin(radian)) if data_len > 100: del self.sin_signal[0] reference_signal = self.reference_buffer[-100:] else: reference_signal = self.reference_buffer # Update data self.idx += 1 if data_len > (20 * self.fps) and data_len % (10 * self.fps) == 0: prev_bpm = self.bpm self.bpm += (10 if data_len < ((20 + 40) * self.fps) else -10) self.idx = (self.idx * prev_bpm) / (self.bpm if self.bpm > 0 else 1) # Visualize show_signal('Sin', self.sin_signal, 500) show_signal('Reference', reference_signal, 500) if data_len >= self.fps * 90: print('Saving experiment 1..') np.save(self.path + '1_frame_' + self.name, self.frame_buffer) np.save(self.path + '1_signal_' + self.name, self.reference_buffer) print('Done.') self.reset_experiment() cv2.destroyWindow('Sin') cv2.destroyWindow('Reference') return False else: return True
is_saving = False save_index = 0 frames = [] signal = [] while True: # Show reference show_sin_signals(fps=FPS) # Get frame and signal data ret, frame = video_stream.read() signal_value = signal_stream.get_signal_value() if is_saving: frames.append(frame) signal.append(signal_value) show_signal('Signal', signal, 500) # Visualize frame = draw_fps(frame, fps=round(video_stream.get_fps())) cv2.imshow('Frame', frame) # Wait and key event key = cv2.waitKey(video_stream.delay()) if key == 27: break elif key == ord('s'): if is_saving: print('> End saving.') xs = np.array(frames) ys = np.array(signal)