Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file added code/__pycache__/model.cpython-39.pyc
Binary file not shown.
53 changes: 32 additions & 21 deletions code/inference_preprocess.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,71 @@
import numpy as np
import cv2
from skimage.util import img_as_float
import tensorflow as tf
import matplotlib.pyplot as plt
import time
import scipy.io
from scipy.sparse import spdiags


def preprocess_raw_video(videoFilePath, dim=36):

#########################################################################
# set up
t = []
i = 0
vidObj = cv2.VideoCapture(videoFilePath);
totalFrames = int(vidObj.get(cv2.CAP_PROP_FRAME_COUNT)) # get total frame size
Xsub = np.zeros((totalFrames, dim, dim, 3), dtype = np.float32)
vidObj = cv2.VideoCapture(videoFilePath)
totalFrames = int(vidObj.get(cv2.CAP_PROP_FRAME_COUNT)) # get total frame size
Xsub = np.zeros((totalFrames, dim, dim, 3), dtype=np.float32)
height = vidObj.get(cv2.CAP_PROP_FRAME_HEIGHT)
width = vidObj.get(cv2.CAP_PROP_FRAME_WIDTH)
success, img = vidObj.read()
dims = img.shape
print("Orignal Height", height)
print("Original width", width)
#########################################################################
# Crop each frame size into dim x dim
while success:
t.append(vidObj.get(cv2.CAP_PROP_POS_MSEC))# current timestamp in milisecond
vidLxL = cv2.resize(img_as_float(img[:, int(width/2)-int(height/2 + 1):int(height/2)+int(width/2), :]), (dim, dim), interpolation = cv2.INTER_AREA)
vidLxL = cv2.rotate(vidLxL, cv2.ROTATE_90_CLOCKWISE) # rotate 90 degree
vidLxL = cv2.cvtColor(vidLxL.astype('float32'), cv2.COLOR_BGR2RGB)
t.append(vidObj.get(cv2.CAP_PROP_POS_MSEC)) # current timestamp in milisecond
vidLxL = cv2.resize(
img_as_float(
img[
:,
int(width / 2)
- int(height / 2 + 1) : int(height / 2)
+ int(width / 2),
:,
]
),
(dim, dim),
interpolation=cv2.INTER_AREA,
)
# vidLxL = cv2.rotate(vidLxL, cv2.ROTATE_90_CLOCKWISE) # rotate 90 degree
vidLxL = cv2.cvtColor(vidLxL.astype("float32"), cv2.COLOR_BGR2RGB)
vidLxL[vidLxL > 1] = 1
vidLxL[vidLxL < (1/255)] = 1/255
vidLxL[vidLxL < (1 / 255)] = 1 / 255
Xsub[i, :, :, :] = vidLxL
success, img = vidObj.read() # read the next one
success, img = vidObj.read() # read the next one
i = i + 1
plt.imshow(Xsub[0])
plt.title('Sample Preprocessed Frame')
plt.title("Sample Preprocessed Frame")
plt.show()
#########################################################################
# Normalized Frames in the motion branch
normalized_len = len(t) - 1
dXsub = np.zeros((normalized_len, dim, dim, 3), dtype = np.float32)
dXsub = np.zeros((normalized_len, dim, dim, 3), dtype=np.float32)
for j in range(normalized_len - 1):
dXsub[j, :, :, :] = (Xsub[j+1, :, :, :] - Xsub[j, :, :, :]) / (Xsub[j+1, :, :, :] + Xsub[j, :, :, :])
dXsub[j, :, :, :] = (Xsub[j + 1, :, :, :] - Xsub[j, :, :, :]) / (
Xsub[j + 1, :, :, :] + Xsub[j, :, :, :]
)
dXsub = dXsub / np.std(dXsub)
#########################################################################
# Normalize raw frames in the apperance branch
Xsub = Xsub - np.mean(Xsub)
Xsub = Xsub / np.std(Xsub)
Xsub = Xsub[:totalFrames-1, :, :, :]
Xsub = Xsub / np.std(Xsub)
Xsub = Xsub[: totalFrames - 1, :, :, :]
#########################################################################
# Plot an example of data after preprocess
dXsub = np.concatenate((dXsub, Xsub), axis = 3);
dXsub = np.concatenate((dXsub, Xsub), axis=3)
return dXsub


def detrend(signal, Lambda):
"""detrend(signal, Lambda) -> filtered_signal
This function applies a detrending filter.
Expand All @@ -80,5 +92,4 @@ def detrend(signal, Lambda):
diags_data = np.array([ones, minus_twos, ones])
diags_index = np.array([0, 1, 2])
D = spdiags(diags_data, diags_index, (signal_length - 2), signal_length).toarray()
filtered_signal = np.dot((H - np.linalg.inv(H + (Lambda ** 2) * np.dot(D.T, D))), signal)
return filtered_signal
return np.dot((H - np.linalg.inv(H + (Lambda**2) * np.dot(D.T, D))), signal)
Loading