Skip to content

Commit

Permalink
update functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheong0428 committed Nov 2, 2017
1 parent 2b20d4f commit 22ed25c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
18 changes: 11 additions & 7 deletions facesync/facesync.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,18 @@ def find_offset_cross(self,length = 10,search_start=0,verbose=True):
------------
length: seconds to use for the cross correlation matching, default is 10 seconds
verbose: if True, prints the currently processing audio filename
Output
------------
allrs : list of cross correlation results using fftconvolve. to retrieve the offset time need to zero index and subtract argmax.
'''
import numpy as np
from numpy.fft import fft, ifft, fftshift
from scipy.signal import fftconvolve
assert(self.target_audio is not None), 'Target audio not specified'
assert(self.audio_files is not None), 'Audio files not specified'
self.offsets = []
rate0,data0 = wav.read(self.target_audio)
allrs = []
for i, afile in enumerate(self.audio_files):
if verbose:
print(afile)
Expand All @@ -164,15 +169,14 @@ def find_offset_cross(self,length = 10,search_start=0,verbose=True):
xnew[:len(x)] = x
x = xnew
assert(len(x)==len(y)), "Length of two samples must be the same"
f1 = fft(x)
f2 = fft(np.flipud(y))
crosscorr = fftshift(np.real(ifft(f1*f2)))
assert(len(crosscorr)==len(x))
zero_index = int(len(x) / 2 ) -1
crosscorr = fftconvolve(x,y[::-1],'full')
zero_index = int(len(crosscorr) / 2 ) -1
offset_x = search_start+(zero_index - np.argmax(crosscorr))/float(rate0)
# assert(len(crosscorr)==len(x))
self.offsets.append(offset_x)
write_offset_to_file(afile, offset_x,header='xcorr_len'+str(length))

allrs.append(crosscorr)
return allrs

def find_offset_corr(self,length=5,search_start=0,search_end=20,fps=44100,verbose=True):
'''
Expand Down
32 changes: 19 additions & 13 deletions facesync/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
import numpy as np
import matplotlib.pyplot as plt

def VideoViewer(path_to_video, data_df, ylabel='',legend=False,xlim=None,ylim=None,plot_rows=True):
def rec_to_time(vals,fps):
times = np.array(vals)/60./fps
times = [str(int(np.floor(t))).zfill(2)+':'+str(int((t-np.floor(t))*60)).zfill(2) for t in times]
return times

def VideoViewer(path_to_video, data_df,xlabel='', ylabel='',title='',figsize=(6.5,3),legend=False,xlim=None,ylim=None,plot_rows=False):
"""
This function plays a video and plots the data underneath the video and moves a cursor as the video plays.
Plays videos using Jupyter_Video_Widget by https://github.com/Who8MyLunch/Jupyter_Video_Widget
Expand All @@ -25,17 +30,15 @@ def VideoViewer(path_to_video, data_df, ylabel='',legend=False,xlim=None,ylim=No
Args:
path_to_video : file path or url to a video. tested with mov and mp4 formats.
data_df : pandas dataframe with columns to be plotted. (plotting too many column can slowdown update)
data_df : pandas dataframe with columns to be plotted in 30hz. (plotting too many column can slowdown update)
ylabel(str): add ylabel
legend(bool): toggle whether to plot legend
xlim(list): pass xlimits [min,max]
ylim(list): pass ylimits [min,max]
plot_rows(bool): Draws individual plots for each column of data_df. (Default: True)
"""
from jpy_video import Video

from IPython.display import display, HTML

display(HTML(data="""
<style>
div#notebook-container { width: 95%; }
Expand All @@ -48,31 +51,34 @@ def VideoViewer(path_to_video, data_df, ylabel='',legend=False,xlim=None,ylim=No
wid = Video(f)
wid.layout.width='640px'
wid.display()
lnwidth = 3

fps = wid.timebase**-1 # time base is play rate hard coded at 30fps
print(fps)
if plot_rows:
fig,axs = plt.subplots(data_df.shape[1],1,figsize=(6.5,3)) # hardcode figure size for now..
fig,axs = plt.subplots(data_df.shape[1],1,figsize=figsize) # hardcode figure size for now..
else:
fig,axs = plt.subplots(1,1,figsize=(6.5,3))
fig,axs = plt.subplots(1,1,figsize=figsize)
t=wid.current_time
if plot_rows:
if plot_rows and data_df.shape[1]>1:
for ixs, ax in enumerate(axs):
ax.axvline(fps*t,color='k',linestyle='--') # cursor is always first of ax
ax.axvline(fps*t,color='k',linestyle='--',linewidth=lnwidth) # cursor is always first of ax
# plot each column
data_df.iloc[:,ixs].plot(ax=ax,legend=legend,xlim=xlim,ylim=ylim)
ax.set(ylabel = ylabel,title=data_df.columns[ixs])
ax.set_xlabel('Frames')
ax.set_xticks = np.arange(0,data_df.shape[0],5)
ax.set(ylabel =data_df.columns[ixs], xlabel=xlabel, xticklabels = rec_to_time(ax.get_xticks(),fps))
else:
axs.axvline(fps*t,color='k',linestyle='--') # cursor is always first of ax
axs.axvline(fps*t,color='k',linestyle='--',linewidth=lnwidth) # cursor is always first of ax
# plot each column
data_df.plot(ax=axs,legend=legend,xlim=xlim,ylim=ylim)
axs.set(ylabel = ylabel)
axs.set_xticks = np.arange(0,data_df.shape[0],5)
axs.set(ylabel = data_df.columns[0],xlabel=xlabel, title=title, xticklabels = rec_to_time(axs.get_xticks(),fps))
if legend:
plt.legend(loc=1)
plt.tight_layout()

def plot_dat(axs,t,fps=fps):
if plot_rows:
if plot_rows and data_df.shape[1]>1:
for ax in axs:
if ax.lines:
ax.lines[0].set_xdata([np.round(fps*t),np.round(fps*t)])
Expand Down

0 comments on commit 22ed25c

Please sign in to comment.