|
1 | 1 | import numpy
|
2 | 2 | import sys
|
3 |
| -import itertools |
4 | 3 | import os
|
5 | 4 | import scipy
|
6 | 5 | from scipy.signal import find_peaks, find_peaks_cwt, peak_prominences
|
|
14 | 13 |
|
15 | 14 | import pyopenpose as op
|
16 | 15 |
|
17 |
| -marker = itertools.cycle((",", "+", ".", "o", "*")) |
18 |
| - |
19 | 16 |
|
20 | 17 | class OpenposeDetector:
|
21 |
| - # nose, neck, right eye, left eye, right ear, left ear |
22 |
| - face_neck_keypoints = [0, 1, 15, 16, 17, 18] |
| 18 | + # nose, right eye, left eye, right ear, left ear |
| 19 | + face_neck_keypoints = [0, 15, 16, 17, 18] |
23 | 20 | confidence_threshold = 0.5
|
24 |
| - peak_prominence = 0.2 |
| 21 | + peak_prominence = 0.05 |
| 22 | + obj_limit = 2 |
25 | 23 |
|
26 | 24 | def __init__(self, n_frames, custom_keypoints=None):
|
27 | 25 | config = {}
|
@@ -100,56 +98,52 @@ def process_frame(self, frame):
|
100 | 98 |
|
101 | 99 | def find_peaks(self):
|
102 | 100 | peaks = [
|
103 |
| - find_peaks( |
104 |
| - numpy.nan_to_num(y_coords), prominence=OpenposeDetector.peak_prominence |
105 |
| - ) |
| 101 | + find_peaks(y_coords, prominence=OpenposeDetector.peak_prominence) |
106 | 102 | for y_coords in self.all_y_coords
|
107 | 103 | ]
|
108 | 104 | peaks = [p[0] for p in peaks]
|
109 | 105 |
|
110 | 106 | prominences = [
|
111 |
| - peak_prominences(numpy.nan_to_num(y_coords), peaks[i]) |
| 107 | + peak_prominences(y_coords, peaks[i]) |
112 | 108 | for i, y_coords in enumerate(self.all_y_coords)
|
113 | 109 | ]
|
114 | 110 | prominences = [p[1] for p in prominences]
|
115 | 111 |
|
116 |
| - strongest_peaks_index = prominences.index(max(prominences, key=sum)) |
| 112 | + top_ycoords_and_peaks = [ |
| 113 | + (ycrds, pks) |
| 114 | + for _, pks, ycrds in sorted( |
| 115 | + zip(prominences, peaks, self.all_y_coords), |
| 116 | + key=lambda triplet: sum(triplet[0]), |
| 117 | + reverse=True, |
| 118 | + ) |
| 119 | + ] |
117 | 120 |
|
118 |
| - return peaks, strongest_peaks_index |
| 121 | + # only track up to obj_limit objects |
| 122 | + return top_ycoords_and_peaks[: OpenposeDetector.obj_limit] |
119 | 123 |
|
120 | 124 | def plot_ycoords(self):
|
121 |
| - peaks, strongest_peaks_index = self.find_peaks() |
122 |
| - |
123 | 125 | plt.figure(1)
|
124 | 126 | plt.title("normalized y coordinate motion")
|
125 | 127 |
|
126 | 128 | plt.xlabel("frame")
|
127 | 129 | plt.ylabel("y coord")
|
128 | 130 |
|
129 | 131 | frames = numpy.arange(self.n_frames)
|
| 132 | + best_coords_and_peaks = self.find_peaks() |
130 | 133 |
|
131 |
| - for i, y_coords in enumerate(self.all_y_coords): |
| 134 | + for i, coordspeaks in enumerate(best_coords_and_peaks): |
| 135 | + y_coords, peaks = coordspeaks |
132 | 136 | y_coords = numpy.asarray(y_coords)
|
133 |
| - plt.plot(frames, y_coords, label="obj {0}".format(i)) |
134 |
| - |
135 |
| - if i == strongest_peaks_index: |
136 |
| - # mark the strongest peaks in a thicker manner |
137 |
| - plt.plot( |
138 |
| - peaks[i], |
139 |
| - y_coords[peaks[i]], |
140 |
| - marker=next(marker), |
141 |
| - linestyle="None", |
142 |
| - markersize=8, |
143 |
| - color="black", |
144 |
| - ) |
145 |
| - else: |
146 |
| - plt.plot( |
147 |
| - peaks[i], |
148 |
| - y_coords[peaks[i]], |
149 |
| - marker=next(marker), |
150 |
| - linestyle="None", |
151 |
| - markersize=5, |
152 |
| - ) |
| 137 | + plt.plot( |
| 138 | + frames, |
| 139 | + y_coords, |
| 140 | + "-D", |
| 141 | + label="obj {0}".format(i), |
| 142 | + markevery=peaks, |
| 143 | + mec="black", |
| 144 | + mfc="black", |
| 145 | + ) |
| 146 | + |
153 | 147 | plt.legend()
|
154 | 148 | plt.show()
|
155 | 149 |
|
|
0 commit comments