commit 7976f1a1c59be07b82d28d66cc1389337e23da09 Author: RobViren Date: Fri Nov 12 13:12:40 2021 -0600 Init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a858f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.tsv \ No newline at end of file diff --git a/data/curl.mp4 b/data/curl.mp4 new file mode 100644 index 0000000..7aec890 Binary files /dev/null and b/data/curl.mp4 differ diff --git a/data/press.mp4 b/data/press.mp4 new file mode 100644 index 0000000..33cc20c Binary files /dev/null and b/data/press.mp4 differ diff --git a/data/situp.mp4 b/data/situp.mp4 new file mode 100644 index 0000000..2f88177 Binary files /dev/null and b/data/situp.mp4 differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..96ee7b8 --- /dev/null +++ b/main.py @@ -0,0 +1,110 @@ +import numpy as np +import cv2 +import datetime +import matplotlib.pyplot as plt +import matplotlib + +matplotlib.use('WebAgg') +scale = 2 +max_count = 1088*1920*255 + +def processVideo(path): + prev_gray = None + data = [] + cap = cv2.VideoCapture(path) + alpha = 0.90 + posx = 0.0 + posy = 0.0 + posxs = [] + posys = [] + counts = [] + while True: + ret, src = cap.read() + if not ret: + print(ret) + break + else: + smol = cv2.resize( + src, (int(src.shape[1]/scale), int(src.shape[0]/scale))) + gray = cv2.cvtColor(smol, cv2.COLOR_BGR2GRAY) + + if prev_gray is not None: + diff = cv2.absdiff(gray, prev_gray) + blur = cv2.GaussianBlur(diff, (5,5),0) + retval, thresh = cv2.threshold(blur, 50, 255, cv2.THRESH_TOZERO) + cv2.imshow("thresh", thresh) + count = np.sum(thresh)/max_count * 100.0 + if count > 0.001 and count < 0.1: + moments = cv2.moments(thresh) + cX = int(moments["m10"] / moments["m00"]) + cY = int(moments["m01"] / moments["m00"]) + posx = posx * alpha + cX * (1.0-alpha) + posy = posy * alpha + cY * (1.0-alpha) + posxs.append(posx) + posys.append(posy) + counts.append(count) + + cv2.putText(smol, "{:.2}".format(count), (20, 50), + cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2) + i = 0 + while i < len(posxs): + blue_color = 0 + if counts[i] > 0.01: + blue_color = 255 + else: + blue_color = counts[i]/0.01 * 255 + + cv2.circle(smol, (int(posxs[i]), int(posys[i])), 10, (blue_color,0,0),6) + i+= 1 + + + + if count > 0.1: + count = 0.1 + data.append(count) + + cv2.imshow("src", smol) + prev_gray = np.copy(gray) + + key = cv2.waitKey(10) + if key == 27: + break + cap.release() + cv2.destroyAllWindows() + return data + + +out = open("data.tsv", "w+") +data1 = processVideo("data/curl.mp4") +data2 = processVideo("data/press.mp4") +data3 = processVideo("data/situp.mp4") + +i = 0 +max_length = 0 +if len(data1) > max_length: + max_length = len(data1) +if len(data2) > max_length: + max_length = len(data2) +if len(data3) > max_length: + max_length = len(data3) + +while i < max_length: + d1 = 0 + try: + d1 = data1[i] + except: + print("") + d2 = 0 + try: + d2 = data2[i] + except: + print("") + d3 = 0 + try: + d3 = data3[i] + except: + print("") + out.write("{}\t{}\t{}\n".format(d1, d2, d3)) + i += 1 + +out.close() diff --git a/plot.py b/plot.py new file mode 100644 index 0000000..4c713b0 --- /dev/null +++ b/plot.py @@ -0,0 +1,33 @@ +import matplotlib.pyplot as plt +import matplotlib +import pandas as pd +import numpy as np +from scipy.signal import savgol_filter +import scipy.signal as signal +import sys +df = pd.read_csv(sys.argv[1],delimiter="\t") +target = sys.argv[2] +time_size = 5 +window = time_size * 30 + 1 + +def reject_outliers(data,indicies, m = 2.): + d = np.abs(data - np.median(data)) + mdev = np.median(d) + s = d/mdev if mdev else 0. + return (data[s 0.0: + moments = cv2.moments(thresh) + cX = (moments["m10"] / moments["m00"])/smol.shape[1] + cY = (moments["m01"] / moments["m00"])/smol.shape[0] + prevx = posx + prevy = posy + posx = posx * pos_alpha + cX * (1.0-pos_alpha) + posy = posy * pos_alpha + cY * (1.0-pos_alpha) + velocity = math.sqrt((prevx-posx) * (prevx-posx) + (prevy - posy) * (prevy - posy)) + running_count = running_count * pos_alpha + count * (1.0 - pos_alpha) + out.write("{}\t{}\t{}\t{}\t{}\n".format(frame_number/30.0,posx,posy,running_count,velocity)) + + # Calculate new BG + bg_float = bg_float * bg_alpha + gray * (1.0 - bg_alpha) + bg = bg_float.astype(np.uint8) + + # Draw + #cv2.circle(smol, (int(posx*smol.shape[1]), int(posy*smol.shape[0])), 3, (255,0,0),6) + + # Show Results + # cv2.imshow("thresh", cv2.resize(thresh, (int(src.shape[1]/2.0), int(src.shape[0]/2.0)))) + # cv2.imshow("smol", cv2.resize(smol, (int(src.shape[1]/2.0), int(src.shape[0]/2.0)))) + # key = cv2.waitKey(1) + # if key == 27: + # break + + frame_number += 1 + print(frame_number) + +cap.release() +cv2.destroyAllWindows() \ No newline at end of file