public X Get(X_POS xp) { switch (xp) { case X_POS.XP_FIRST: return(m_xFirst); case X_POS.XP_THIS: return(m_xThis); case X_POS.XP_LAST: return(m_xLast); } return(null); }
public static float dtwDist(X_POS p1, X_POS p2) { return(handsDist(p1, p2)); /*float sum = 0; * for (int i = 7; i < p1.N; i += 7) * { * float s1 = Mathf.Sqrt(p1.vec[i + 0] * p1.vec[i + 0] + p1.vec[i + 1] * p1.vec[i + 1] + p1.vec[i + 2] * p1.vec[i + 2]); * float s2 = Mathf.Sqrt(p2.vec[i + 0] * p2.vec[i + 0] + p2.vec[i + 1] * p2.vec[i + 1] + p2.vec[i + 2] * p2.vec[i + 2]); * sum += (s1 - s2) * (s1 - s2); * } * return sum;*/ }
private int calnFrame(ControlledHuman.Record record, int back = 0) { if (record.getIndex() < 1) { return(0); } X_POS currSpeed = record.getXSpeedSmooth(back); if (dtw[1] == 0f) { dtw[1] = X_POS.dtwDist(currSpeed, xSpeedSmooth[1]); return(1); } float[] nDtw = new float[timestamp.Count]; if (circularDtw) { dtw[0] = dtw[timestamp.Count - 1]; } for (int t = 1; t < timestamp.Count; t++) { if (nDtw[t - 1] != 0f && (nDtw[t] == 0f || nDtw[t - 1] < nDtw[t])) { nDtw[t] = nDtw[t - 1]; } if (dtw[t - 1] != 0f && (nDtw[t] == 0f || dtw[t - 1] < nDtw[t])) { nDtw[t] = dtw[t - 1]; } if (dtw[t] != 0f && (nDtw[t] == 0f || dtw[t] < nDtw[t])) { nDtw[t] = dtw[t]; } nDtw[t] += X_POS.dtwDist(currSpeed, xSpeedSmooth[t]); } dtw = nDtw; int frame = 1; for (int t = 2; t < timestamp.Count; t++) { if (dtw[t] != 0 && dtw[t] < dtw[frame]) { frame = t; } } return(frame); }
/*public static float handsDistToHead(X_POS p1, X_POS p2) * { * float sum = 0; * int cnt = 0; * for (int i = 7; i < p1.N; i += 7) * { * float d2 = 0; * d2 += Mathf.Pow((p1.vec[i + 0] - p1.vec[0]) - (p2.vec[i + 0] - p2.vec[0]), 2f); * d2 += Mathf.Pow((p1.vec[i + 1] - p1.vec[1]) - (p2.vec[i + 1] - p2.vec[1]), 2f); * d2 += Mathf.Pow((p1.vec[i + 2] - p1.vec[2]) - (p2.vec[i + 2] - p2.vec[2]), 2f); * sum += Mathf.Sqrt(d2); * cnt++; * } * sum /= cnt; * return sum; * }*/ public static float handsDist(X_POS p1, X_POS p2) { float sum = 0; int cnt = 0; for (int i = 7; i < p1.N; i += 7) { float d2 = 0; d2 += Mathf.Pow(p1.vec[i + 0] - p2.vec[i + 0], 2f); d2 += Mathf.Pow(p1.vec[i + 1] - p2.vec[i + 1], 2f); d2 += Mathf.Pow(p1.vec[i + 2] - p2.vec[i + 2], 2f); sum += Mathf.Sqrt(d2); cnt++; } sum /= cnt; return(sum); }
public void readTags(string[] tags, int baseId = 0) { timestamp.Add(float.Parse(tags[baseId + 0])); X_POS x = new X_POS(); for (int i = 0; i < x.N; i++) { x.vec[i] = float.Parse(tags[baseId + 1 + i]); } Y_POS y = new Y_POS(); for (int i = 0; i < y.N; i++) { y.vec[i] = float.Parse(tags[baseId + 1 + x.N + i]); } xPos.Add(x); yPos.Add(y); xPosSmooth.Add(xRollingMean(xPos)); yPosSmooth.Add(yRollingMean(yPos)); }
private void segment() { int startIndex = -1; int endIndex = -1; segmentCheck(out startIndex, out endIndex); timestamp = timestamp.GetRange(startIndex, endIndex - startIndex + 1); xPos = xPos.GetRange(startIndex, endIndex - startIndex + 1); yPos = yPos.GetRange(startIndex, endIndex - startIndex + 1); xStart = xPos[0]; yStart = yPos[0]; int T = timestamp.Count; float startTime = timestamp[0]; for (int t = 0; t < T; t++) { xPos[t] = new X_POS(xPos[t] - xStart); yPos[t] = new Y_POS(yPos[t] - yStart); timestamp[t] -= startTime; } }
public float predictMotionFrame(ControlledHuman.Record record, out float score) { if (circularDtw) { circularCnt++; if (circularCnt >= timestamp.Count) { int back = 50; circularCnt = back; dtw = new float[timestamp.Count]; X_POS currSpeed = record.getXSpeedSmooth(back); for (int t = 0; t < timestamp.Count; t++) { dtw[t] = X_POS.dtwDist(currSpeed, xSpeedSmooth[t]); } for (int i = back - 1; i >= 1; i--) { calnFrame(record, i); } } } int dtwFrame = calnFrame(record); score = dtw[dtwFrame]; predictFrame += 1f; if (circularDtw) { float timeDist = dtwFrame - predictFrame; if (timeDist <= 0f) { timeDist += timestamp.Count - 1f; } if (timeDist < (timestamp.Count - 1f) / 2f) { predictFrame += 1.0f; } else { predictFrame -= 0.5f; } if (predictFrame > timestamp.Count - 1f) { predictFrame -= timestamp.Count - 1f; } } else { if (dtwFrame > predictFrame + 1.0f) { predictFrame += 1.0f; } else if (dtwFrame < predictFrame - 0.5f) { predictFrame -= 0.5f; } predictFrame = Mathf.Min(predictFrame, timestamp.Count - 1f); } return(predictFrame); }