Created
April 23, 2019 14:15
-
-
Save pondahai/75a79d9e27c04861e74af5f6fee25f0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import cv2 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from scipy.signal import detrend | |
helpText = "'Esc' to Quit" | |
def parse_args(): | |
parser = argparse.ArgumentParser(description="CV2 Video Capture PhotoPlethysmoGram Test") | |
parser.add_argument("--vid", dest="video_dev", | |
help="video device # of USB webcam (/dev/video?) [1]", | |
default=1, type=int) | |
args_ret = parser.parse_args() | |
return args_ret | |
def line(x0, x1, y0, y1, func): | |
isSwap = abs(y1 - y0) > abs(x1 - x0) | |
if isSwap: | |
x0, y0 = y0, x0 | |
x1, y1 = y1, x1 | |
if x0 > x1: | |
x0, x1 = x1, x0 | |
y0, y1 = y1, y0 | |
deltax = x1 - x0 | |
deltay = abs(y1 - y0) | |
error = deltax / 2 | |
y = y0 | |
if y0 < y1: | |
ystep = 1 | |
else: | |
ystep = -1 | |
for x in range(x0, x1): | |
if isSwap: | |
func(y, x) | |
else: | |
func(x, y) | |
error = error - deltay | |
if error < 0: | |
y = y + ystep | |
error = error + deltax | |
def plot_r(x, y): | |
frame[int(y), int(x)] = [255,0,0] | |
def plot_g(x, y): | |
frame[int(y), int(x)] = [0,255,0] | |
def plot_b(x, y): | |
frame[int(y), int(x)] = [0,0,255] | |
def plot_w(x, y): | |
frame[int(y), int(x)] = [255,255,255] | |
DATA_LENGTH = 64 | |
if __name__ == "__main__": | |
# plot init | |
xx = np.arange(64) | |
yy = np.random.randn(64) | |
fig, ax = plt.subplots(3,figsize=(8,8),linewidth=0.1) | |
fig.subplots_adjust(hspace=0.5) | |
# arg parsing | |
args = parse_args() | |
print("Called with args:") | |
print(args) | |
# open video device | |
cap = cv2.VideoCapture(args.video_dev) | |
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 32) | |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 32) | |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
px = int(width /2 ) # float | |
py = int(height /2 ) # float | |
print(py,px) | |
redHistoryDataList = np.random.randn(64) | |
redOutputDataList = np.random.randn(64) | |
greenHistoryDataList = np.random.randn(64) | |
greenOutputDataList = np.random.randn(64) | |
blueHistoryDataList = np.random.randn(64) | |
blueOutputDataList = np.random.randn(64) | |
# plots setup | |
li1, = ax[0].plot(xx, yy, color='r') | |
ax[0].set_xlim(0,DATA_LENGTH) | |
ax[0].set_ylim(-255,255) | |
li2, = ax[1].plot(xx, yy, color='g') | |
ax[1].set_xlim(0,DATA_LENGTH) | |
ax[1].set_ylim(-255,255) | |
li3, = ax[2].plot(xx, yy, color='b') | |
ax[2].set_xlim(0,DATA_LENGTH) | |
ax[2].set_ylim(-255,255) | |
while(True): | |
ret, frame = cap.read() | |
if ret: | |
cv2.putText(frame, helpText, (11,20), cv2.FONT_HERSHEY_PLAIN, 1.0, (32,32,32), 4, cv2.LINE_AA) | |
cv2.putText(frame, helpText, (10,20), cv2.FONT_HERSHEY_PLAIN, 1.0, (240,240,240), 1, cv2.LINE_AA) | |
bb, gg, rr = cv2.split(frame) | |
one_r = (255-np.mean(rr)) | |
one_g = (255-np.mean(gg)) | |
one_b = (255-np.mean(bb)) | |
redHistoryDataList = np.append(redHistoryDataList,one_r ) | |
if len(redHistoryDataList) > DATA_LENGTH : | |
redHistoryDataList = np.delete(redHistoryDataList,0) | |
greenHistoryDataList = np.append(greenHistoryDataList,one_g ) | |
if len(greenHistoryDataList) > DATA_LENGTH : | |
greenHistoryDataList = np.delete(greenHistoryDataList,0) | |
blueHistoryDataList = np.append(blueHistoryDataList,one_b ) | |
if len(blueHistoryDataList) > DATA_LENGTH : | |
blueHistoryDataList = np.delete(blueHistoryDataList,0) | |
redOutputDataList = np.append(redOutputDataList,(one_r - np.mean(redHistoryDataList[-32:]))) | |
if len(redOutputDataList) > DATA_LENGTH : | |
redOutputDataList = np.delete(redOutputDataList,0) | |
greenOutputDataList = np.append(greenOutputDataList,(one_g - np.mean(greenHistoryDataList[-32:]))) | |
if len(greenOutputDataList) > DATA_LENGTH : | |
greenOutputDataList = np.delete(greenOutputDataList,0) | |
blueOutputDataList = np.append(blueOutputDataList,(one_b - np.mean(blueHistoryDataList[-32:]))) | |
if len(blueOutputDataList) > DATA_LENGTH : | |
blueOutputDataList = np.delete(blueOutputDataList,0) | |
li1.set_xdata(np.arange(len(redOutputDataList))) | |
li1.set_ydata((redOutputDataList)*50) | |
li2.set_xdata(np.arange(len(greenOutputDataList))) | |
li2.set_ydata((greenOutputDataList)*50) | |
li3.set_xdata(np.arange(len(blueOutputDataList))) | |
li3.set_ydata((blueOutputDataList)*50) | |
# | |
now_x = 0 | |
old_x = 0 | |
old_ry = 0 | |
old_gy = 0 | |
old_by = 0 | |
# for p in drawDataList: | |
# by = int((255-p[0]) * 1.0) | |
# gy = int((255-p[1]) * 1.0) | |
# ry = int((255-p[2]) * 1.0) | |
# | |
# if now_x == 0: | |
# frame[old_by, now_x] = [255,0,0] | |
# frame[old_gy, now_x] = [0,255,0] | |
# frame[old_ry, now_x] = [0,0,255] | |
# else: | |
# line(old_x, now_x, old_by, by, plot_r) | |
# line(old_x, now_x, old_gy, gy, plot_g) | |
# line(old_x, now_x, old_ry, ry, plot_b) | |
# line(old_x+1, now_x+1, old_ry+1, ry+1, plot_b) | |
# line(old_x+2, now_x+2, old_ry+2, ry+2, plot_w) | |
# | |
# old_x = now_x | |
# old_by = by | |
# old_gy = gy | |
# old_ry = ry | |
# now_x=now_x+2 | |
cv2.imshow('frame', frame) | |
if cv2.waitKey(1) & 0xFF == 27: | |
break | |
plt.pause(0.001) | |
cap.release() | |
cv2.destroyAllWindows() | |
plt.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment