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()