In [2]:
import helpers as h
h.create_small_videos()
In [1]:
%matplotlib inline
import cv2
from matplotlib import pyplot as plt
import time
import os, json, sys
sys.path.append("../../../../../resources/pyimgsaliency/")
import pyimgsaliency as psal
import scipy.misc, os
import subprocess
from PIL import Image
VIDEO_FRAMES_ROOT = "video-frames-medium/"
ANALYSIS_ROOT = "analysis/image-saliency/"
VIDEO_ROOT = "new-videos/"
time_ref = {
"snowboarding": [151.868109 + 29, 241],
"hpo-preview": [0, 60.5],
"surfing": [45, 105],
"dining-at-the-met": [30, 96],
"knives": [0,35],
"trees": [82,150],
"invasion": [120, 180],
"ice-art": [4,70],
"volcano": [93,160]
}
already_done = [f.split(".json")[0] for f in os.listdir("analysis/image-saliency/") if f[0] != "."]
bases = [f for f in os.listdir("video-frames/") if "." not in f and f not in already_done][1:]
print bases
def get_immediate_subdirectories(a_dir):
return [name for name in os.listdir(a_dir)
if os.path.isdir(os.path.join(a_dir, name))]
def any_base_in_fn(f):
for base in bases:
if base in f:
return True
return False
all_videos = [v for v in os.listdir(VIDEO_ROOT) if "small" in v and v[0] !='.' if "arctic" not in v and "congo" not in v]
all_videos = [v for v in all_videos if any_base_in_fn(v)]
print all_videos
for video in all_videos:
base = video.lower().split(".mp4")[0]
print "Processing video:", base
video_path = VIDEO_ROOT + video
video_frames_path = VIDEO_FRAMES_ROOT + base + "/"
# delete any existing video frames
sub_directory_paths = [VIDEO_FRAMES_ROOT + p + "/" for p in get_immediate_subdirectories(VIDEO_FRAMES_ROOT)]
for d in sub_directory_paths:
for frame in os.listdir(d):
os.remove(d + frame)
# make a new directory
if not os.path.exists(video_frames_path):
os.mkdir(video_frames_path)
start_time = time.time()
# split the video into smaller video frames
subprocess.call(["ffmpeg", "-i", video_path, "-vf", "fps=2", video_frames_path + "%05d.png"])
analysis_path = ANALYSIS_ROOT + base + "/"
# make a new directory
if not os.path.exists(analysis_path):
os.mkdir(analysis_path)
# ok now we actually want to just take 2 frames per second
fns = [video_frames_path + fn for fn in os.listdir(video_frames_path) if ".png" in fn]
# new_fns = []
# for i in range(len(fns)):
# fn = fns[i]
# if i % 15 == 0:
# new_fns.append(fn)
# fns = new_fns
for frame_fn in fns:
out_fn = analysis_path + frame_fn.split("/")[-1]
try:
sal_map = psal.get_saliency_mbd(frame_fn).astype('uint8')
im = Image.fromarray(sal_map)
im.save(out_fn)
except:
print frame_fn
print "Time to process:", time.time() - start_time
In [2]:
3 + 4
Out[2]:
In [ ]: