<jupyter_start><jupyter_text><NAME>15 November 2016Update: 10 Feb 2017 - Read in frames from raw .avi video, and stack them all up to visualize pollen release over time- This is used to make a figure and a supplemental video for manuscript<jupyter_code>import cv2
import numpy as np
import matplotlib
matplotlib.use("TkAgg") # have to use this for tkinter to  work below
from matplotlib import pyplot as plt
%matplotlib tk

# scikit image
import skimage
from skimage import io

import os
import pandas as pd
import time
%qtconsole
import Tkinter as tk
import tkFileDialog

def file_choose():
    '''
    Opens tkfileDialog and returns full path name.
    It's the equivalent of file.choose in R.
    Make sure the following modeules are loaded
    import Tkinter as tk
    import tkFileDialog
    Also, won't work if %matplotlib qt4 has been used
    '''
    root = tk.Tk()
    root.update()
    file1 = tkFileDialog.askopenfilename(parent=root)
    root.destroy() # closes the window
    return file1
# get this video: 20160615_153850.avi
aa = '/Users/callinswitzer/Dropbox/ExperSummer2016/Kalmia/Manuscript/DatasetsSupplemental/20160615_153850.avi'
print aa
cap = cv2.VideoCapture(aa)
print 'numFrames = ' + str(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
# read in frames and stack them up as a np array

# read in one frame
ret, frame = cap.read()

# convert to grayscale
frame = np.array(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY))
frame.shape


numFr = int(np.round(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)))

# preallocate array
imStack = np.ones(np.hstack([list(frame.shape), numFr])) * 0.0

cap = cv2.VideoCapture(aa)

for ii in range(numFr):
    ret, frame = cap.read()
    frame = 255 - np.array(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY))
    imStack[:,:,ii] = frame
    print ii
# get median value for each pixel over time
aa = np.median(imStack, axis = 2)
# subtract background
im2 = np.array([imStack[:,:,ii] - aa for ii in range(imStack.shape[2])])
# threshold
bb = np.array([im2[ii,:,:] > 20 for ii in range(imStack.shape[2])])
# show another bw image
ii = 500
plt.clf()
io.imshow(imStack[:,:,ii] > 60, cmap = 'gray')
# make a normalized image stack -- moving things will be black
imst2 = 1 - (imStack / np.max(imStack))
# make new video to combine two image stacks
AnnotStack = np.array([imst2[:, :, ii] * (im2[ii,:,:] < 20) for ii in range(imStack.shape[2])])
# save video
vidDir = '/Users/callinswitzer/Desktop/testStack2/'

if not os.path.isdir(vidDir):
    os.mkdir(vidDir)

os.chdir(vidDir)
# save images -- no tail
for ii in range(AnnotStack.shape[0]):
    cv2.imwrite(str(ii).zfill(4) + '.png', AnnotStack[ii, :,:] * 255.0)
# make images into a movie
stta = time.time()
!ffmpeg -start_number 0 -i %04d.png -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -pix_fmt yuv420p -y FlyingPollenOverlay.mp4
print time.time() - stta # ~ 1.4 seconds
#annotate with tail by stacking images
im3 = np.array([(im2[ii,:,:] < 20) + (im2[ii-1,:,:] < 20) for ii in range(1, imStack.shape[2])])
im3 = im2.copy()
im3[0, :,:] = 0

for ii in range(1, im2.shape[0]):
    im3[ii,:,: ] = (im3[ii,:,: ] > 10) + (im3[ii-1, :, :] > 0)
plt.clf()
io.imshow(im3[0, :,:] > 0)
plt.clf()
io.imshow(im3[500, :,:])
# combine stacks
AnnotStack2 = np.array([imst2[:, :, ii] * (im3[ii,:,:] < 1) for ii in range(imStack.shape[2])])
# save images
vidDir = '/Users/callinswitzer/Desktop/testStack3/'

if not os.path.isdir(vidDir):
    os.mkdir(vidDir)

os.chdir(vidDir)

# save images
for ii in range(AnnotStack.shape[0]):
    cv2.imwrite(str(ii).zfill(4) + '.png', AnnotStack2[ii, :,:] * 255.0)
# convert images to video
stta = time.time()
!ffmpeg -start_number 0 -i %04d.png -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -pix_fmt yuv420p -y FlyingPollenOverlay_tail.mp4
print time.time() - stta # ~ 1.4 seconds
# show installed packages and versions
!pip freeze 
# print system info
import IPython
print IPython.sys_info()<jupyter_output>{'commit_hash': u'5c9c918',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': '/Users/callinswitzer/anaconda/lib/python2.7/site-packages/IPython',
 'ipython_version': '5.1.0',
 'os_name': 'posix',
 'platform': 'Darwin-15.6.0-x86_64-i386-64bit',
 'sys_executable': '/Users/callinswitzer/anaconda/bin/python',
 'sys_platform': 'darwin',
 'sys_version': '2.7.9 |Anaconda custom (x86_64)| (default, Dec 15 2014, 10:37:34) \n[GCC 4.2.1 (Apple Inc. build 5577)]'}
{'fraction_non_alphanumeric': 0.15231187669990934, 'fraction_numerical': 0.04941069809610154, 'mean_word_length': 4.362089914945322, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '531021', 'license': 'MIT', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1709, 'n_words': 450, 'path': '2_5_StackVideoFrames_Submission.ipynb', 'repo_name': 'callinSwitzer/KalmiaCatapult', 'size': '4420'}