Exemplo n.º 1
0
def calc_bpm(signal, name, visualize):
    normed = zero_centered_normalize(signal)
    bpms = []
    for i in range(3):
        conv_size = 1 + (i * 2)
        conv = np.convolve(normed, np.ones((conv_size, )) / conv_size, 'valid')

        # processing
        peaks = find_peaks(conv, height=0, distance=5)[0]

        if visualize and i == 0:
            frame, scale = signal_to_frame(conv,
                                           300,
                                           sel_points=peaks,
                                           ret_scale=True,
                                           background=(255, 255, 255),
                                           foreground=(0, 0, 0),
                                           padding=5)
            cv2.imshow(name, frame)

        if peaks.shape[0] < 2:
            return []

        peak1 = peaks[1:]
        peak2 = peaks[:-1]
        ppi = (peak1 - peak2).mean()
        bpm = 60 / (ppi / 5)
        bpms.append(bpm)

    return bpms
Exemplo n.º 2
0
def peaks(s):
    normed = zero_centered_normalize(s)
    peaks = find_peaks(normed, height=0, distance=5)[0]
    frame, scale = signal_to_frame(normed,
                                   300,
                                   sel_points=peaks,
                                   ret_scale=True,
                                   background=(255, 255, 255),
                                   foreground=(0, 0, 0),
                                   padding=5)
    return peaks, frame
Exemplo n.º 3
0
def show_plot(s1, s2, peak1, peak2):
    line_size = 4
    point_size = 150
    font_size = 15

    s1 = zero_centered_normalize(s1)
    s2 = zero_centered_normalize(s2)

    plt.figure(figsize=(5, 3))
    plt.plot(s1, linewidth=line_size, zorder=1, label='Reference')
    plt.plot(s2, linewidth=line_size, zorder=3, label='Proposed')

    # plt.scatter(peak1, s1[peak1], c='b', s=point_size, zorder=2, label='Reference peak')
    # plt.scatter(peak2, s2[peak2], c='r', s=point_size, zorder=4, label='Proposed peak')

    plt.legend(loc='lower right', prop={'size': 13})

    plt.xlabel('Frame #', fontsize=font_size)
    plt.ylabel('Normalized amplitude', fontsize=font_size)

    plt.tight_layout()
    plt.show()
Exemplo n.º 4
0
    def get_respiration(self, frame):
        # Save as member variable
        self.frame = frame

        # Load ROI detection model
        if self.roi_model is None:
            self.roi_model = load_model(self.model_path, compile=False)

        # Calculate ROI
        frames = self._get_frames(frame)
        if frames is None:
            return self.return_signal
        self.score, self.roi = self._get_roi(frames)

        # Extract ROI signals (# of roi pixels (or maximum_point), window_size)
        signals = self._get_signals_with_roi(frames)

        # Zero-centered normalization
        signals = zero_centered_normalize(signals, axis=-1)

        # Clustering with symmetry data
        n_cluster = 4
        if signals.shape[0] < n_cluster:
            return self.return_signal
        origin_cluster, reverse_cluster = self._get_cluster(signals, n_cluster)

        # Post process
        extracted_signals = self._get_extracted_signals(
            signals, n_cluster, origin_cluster, reverse_cluster)
        if extracted_signals is None:
            return self.return_signal
        if self.signal is None:
            self.signal = extracted_signals[0] - extracted_signals[1]
        else:
            coef1 = np.corrcoef(self.signal[1:],
                                extracted_signals[0][:-1])[0][1]
            coef2 = np.corrcoef(self.signal[1:],
                                extracted_signals[0][:-1])[0][1]
            self.signal = extracted_signals[0] - extracted_signals[
                1] if coef1 > coef2 else extracted_signals[
                    1] - extracted_signals[0]

        # Visualize
        if self.show_cluster:
            self._show_cluster_plane(signals, origin_cluster)
        if self.show_sym_cluster:
            self._show_symmetry_cluster_plane(
                signals, np.append(origin_cluster, reverse_cluster, axis=0))
        return -self.signal if self.flip else self.signal
Exemplo n.º 5
0
    for i in range(0, frames.shape[0] - SKIP_RATIO * WINDOW_SIZE, 10):
        # Set frame data
        xs = frames[i:i + WINDOW_SIZE * SKIP_RATIO]
        xs = xs[index]
        x_data = []
        for frame in xs:
            x_data.append(frame_process(frame, COLOR, DATA_SIZE))
        x_data = np.array(x_data)

        # Set signal data
        ys = signal[i:i + WINDOW_SIZE * SKIP_RATIO]
        y_data = ys[index]

        # Normalize
        normed_xs = zero_centered_normalize(x_data, axis=0)
        normed_ys = zero_centered_normalize(y_data)

        # Get label
        label = get_pearson_correlation_score(normed_xs, normed_ys)
        thres = 0.6
        label[label >= thres] = 1
        label[label < thres] = 0
        h, w = label.shape
        label = label.reshape((h, w, 1))

        # Set data to save
        save_xs.append(x_data)
        save_signal.append(normed_ys)
        save_label.append(label)
Exemplo n.º 6
0
def process(input_data, roi):
    # Extract signal from input data
    input_data = input_data[0]
    signals = input_data.transpose((3, 0, 1, 2))

    signals = signals[..., roi == 1.0].transpose(1, 2, 0)[..., 0]
    signals = signals.T

    # Zero-centered normalization
    signals = zero_centered_normalize(signals, axis=-1)

    # Set all elements 0 signal to 1
    signal_sum = np.fabs(signals).sum(axis=-1)
    signals[signal_sum == 0.0] += 1.0

    # Clustering
    n_cluster = 4
    if signals.shape[0] < n_cluster:
        print('ERROR!!!')
        return None

    reverse_signals = -signals
    input_signals = np.append(signals, reverse_signals, axis=0)
    cluster = clustering_hierarchy(input_signals,
                                   n_cluster,
                                   affinity='cosine',
                                   linkage='complete')

    # Divide cluster
    origin_cluster = cluster[:signals.shape[0]]
    reverse_cluster = cluster[signals.shape[0]:]

    # Count number of crossed cluster elements
    cluster_counter = []
    counter = []

    for i in range(n_cluster):
        cluster_counter.append(
            max((origin_cluster == i).astype(np.int).sum(), 1))
        arr = []
        for j in range(n_cluster):
            mask = (origin_cluster == i) & (reverse_cluster == j)
            arr.append(mask.astype(np.int).sum())
        counter.append(arr)
    cluster_counter = np.array(cluster_counter)
    counter = np.array(counter)

    cluster_ratio = (counter.T / cluster_counter).T
    cluster_max_index = cluster_ratio.argmax(axis=-1)
    cluster_empty_index = cluster_ratio.max(axis=-1) == 0
    cluster_max_index[cluster_empty_index] = -1

    pairs = []
    for i in range(n_cluster):
        if not cluster_empty_index[i]:
            if cluster_max_index[cluster_max_index[i]] == i:
                pairs.append((i, cluster_max_index[i]))
                cluster_empty_index[cluster_max_index[i]] = True

    if pairs:
        cluster_size = []
        for pair in pairs:
            cluster_size.append(cluster_counter[pair[0]] +
                                cluster_counter[pair[1]])
        idx = np.argmax(cluster_size)

        signal1 = signals[origin_cluster == pairs[idx][0]].mean(axis=0)
        signal2 = signals[origin_cluster == pairs[idx][1]].mean(axis=0)

        ret_signal = signal1 - signal2
        return ret_signal

    print('end point')
    return None
Exemplo n.º 7
0
def peaks(s):
    normed = zero_centered_normalize(s)
    peaks = find_peaks(normed, height=0, distance=5)[0]
    return peaks
Exemplo n.º 8
0
        # Set data
        frames.append(frame_process(frame, COLOR, DATA_SIZE))
        signal.append(signal_value)
        if len(frames) > WINDOW_SIZE:
            del frames[0]
            del signal[0]

        # Data to numpy array
        xs = np.array(frames)
        ys = np.array(signal)

        # Main process
        if len(frames) == WINDOW_SIZE:
            # Calculate similarity
            xs = zero_centered_normalize(xs, axis=0)
            ys = zero_centered_normalize(ys)

            # Calculate score for labeling
            # score = old_label_method(xs, ys)   # Old method
            score = get_pearson_correlation_score(xs, ys)
            thres = 0.7
            label = score.copy()
            label[label >= thres] = 1
            label[label < thres] = 0

            cv2.imshow('Score', cv2.resize(score, dsize=None, fx=DATA_SIZE, fy=DATA_SIZE, interpolation=cv2.INTER_AREA))
            cv2.imshow(LABEL_NAME, cv2.resize(label, dsize=None, fx=DATA_SIZE, fy=DATA_SIZE, interpolation=cv2.INTER_AREA))

        # Visualize results
        frame = draw_fps(frame, fps=round(video_stream.get_fps()))