In [414]:
# interactive tool, put on top so we can find it easily
ks = "abcd"
kwargs = {}
for i in range(4):
    kwargs["p"+ks[i]]=widgets.IntSliderWidget(min=0, max=SW if i%2 else SH, step=1, value=p[i])
    kwargs["q"+ks[i]]=widgets.IntSliderWidget(min=0, max=slide_w if i%2 else slide_h, step=1, value=q[i])
kwargs['blur_factorx']=widgets.IntSliderWidget(min=0, max=40, step=1, value=blur_factor)
i = interact(iicompare, **kwargs)
(126, 1, 443, 631) , (6, 12, 1125, 2000) , 12 0.973290029063
In [3]:
%matplotlib inline
import numpy as np
import cv2
import matplotlib.pyplot as plt
import os
try:
    import cv
    CAP_FRAME_COUNT = cv.CV_CAP_PROP_FRAME_COUNT
    CAP_FRAME_WIDTH  = cv.CV_CAP_PROP_FRAME_WIDTH
    CAP_FRAME_HEIGHT = cv.CV_CAP_PROP_FRAME_HEIGHT
    CAP_FPS = cv.CV_CAP_PROP_FPS
    CAP_POS_FRAMES = cv.CV_CAP_PROP_POS_FRAMES
except:
    CAP_FRAME_COUNT = cv2.CAP_PROP_FRAME_COUNT
    CAP_FRAME_WIDTH  = cv2.CAP_PROP_FRAME_WIDTH
    CAP_FRAME_HEIGHT = cv2.CAP_PROP_FRAME_HEIGHT
    CAP_FPS = cv2.CAP_PROP_FPS
    CAP_POS_FRAMES = cv2.CAP_PROP_POS_FRAMES
    
    print "no cv"
from IPython.html.widgets import interact, interactive, fixed
from IPython.display import clear_output, display, HTML
from IPython.html import widgets
In [4]:
NAME = "openstack"
fn_base = "%s/%s"%(NAME,NAME)
if  os.path.isfile(fn_base+".mp4"):
    fn = fn_base+".mp4"
elif os.path.isfile(fn_base+".avi"):
    fn = fn_base+".avi"
else:
    print "original video file does not exist"    
v2 = cv2.VideoCapture(fn)
SW, SH = v2.get(CAP_FRAME_WIDTH), v2.get(CAP_FRAME_HEIGHT)
FRAMES = v2.get(CAP_FRAME_COUNT)
v2.get(CAP_FPS), v2.get(CAP_FRAME_WIDTH), v2.get(CAP_FRAME_HEIGHT), v2.get(CAP_FRAME_COUNT)
original video file does not exist
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-dcdde810701a> in <module>()
      7 else:
      8     print "original video file does not exist"
----> 9 v2 = cv2.VideoCapture(fn)
     10 SW, SH = v2.get(CAP_FRAME_WIDTH), v2.get(CAP_FRAME_HEIGHT)
     11 FRAMES = v2.get(CAP_FRAME_COUNT)

NameError: name 'fn' is not defined
In [403]:
from io import BytesIO
import PIL
from IPython.display import display, Image

def display_img_array(ima, cvt=None, **kwargs):
    if cvt:
        ima = cv2.cvtColor(ima, cvt)
    im = PIL.Image.fromarray(ima)
    bio = BytesIO()
    im.save(bio, format='png')
    display(Image(bio.getvalue(), format='png', **kwargs))
In [404]:
frame = None
frame_n = 0
slide_n = 0
slide = None
In [405]:
# select frame
def select_frame(n):
    global frame, frame_n
    frame_n = n
    v2.set(CAP_POS_FRAMES, n)
    ret, frame = v2.read()
    display_img_array(frame, width=480, cvt = cv2.COLOR_BGR2RGB)
interact(select_frame, n=widgets.IntSliderWidget(min=0, max=FRAMES-1, step=1, value=frame_n))
In [406]:
def load_original_slides(name):
    original_slides = []
    i = 0
    text = widgets.TextWidget()
    display(text)
    while True:    
        img = cv2.imread("%s/%s-%d.png"%(name, name, i))        
        if img is None:
            break
        text.value = "%s, %d, %d"%(img.shape, len(original_slides),i)
        original_slides.append(img)
        i+=1
    return original_slides
In [407]:
def normalize(im):    
    im=cv2.cvtColor(im, cv2.CV_32F)
    im=cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    #im=cv2.equalizeHist(im)
    return im
original_slides = load_original_slides(NAME)
#normalized_slides = [cv2.blur(normalize(s), (11,11))  for s in original_slides]
In [408]:
# select slide
def select_slide(n):
    global slide, slide_n
    slide_n = n
    slide = original_slides[n]
    display_img_array(slide, width=480, cvt=cv2.COLOR_BGR2RGB)
interact(select_slide, n=widgets.IntSliderWidget(min=0, max=len(original_slides)-1, step=1, value=slide_n))

slide_h, slide_w = slide.shape[:2]
print slide_w, slide_h