Skip to content

Commit

Permalink
Video comparison script
Browse files Browse the repository at this point in the history
  • Loading branch information
hyades committed Aug 15, 2013
1 parent f45c872 commit 77b7a08
Show file tree
Hide file tree
Showing 23 changed files with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions python-api/tests/scripts/cv.py
@@ -0,0 +1,83 @@
import os
import cv2
import sys

from scipy.misc import imread
from scipy.linalg import norm
from scipy import sum, average

import subprocess

def check_video(video, cmp_folder):


generate_keyframes(video, cmp_folder)

result = {}
for x in os.listdir('reference_frames'):
try:
res = comp_image('reference_frames/'+x, 'test_frames/'+x)
# if res:
result[x] = res
except Exception as e:
pass
for x in result:
print x, result[x]


def generate_keyframes(video, cmp_folder):
cmd = "/usr/local/bin/ffmpeg -i {0} -f image2 -vf fps=fps=1 {1}/foo-%d.png".format(video, cmp_folder)
# print cmd
with open(os.devnull, 'w') as tempf:
proc = subprocess.Popen(
cmd.split(),
stdout=tempf,
stderr=tempf,
bufsize=-1,
shell=False)
proc.wait()


def to_grayscale(arr):
"If arr is a color image (3D array), convert it to grayscale (2D array)."
if len(arr.shape) == 3:
return average(arr, -1) # average over the last axis (color channels)
else:
return arr

def comp_image(image1, image2):
file1, file2 = (image1, image2)
# read images as 2D arrays (convert to grayscale for simplicity)
img1 = imread(file1).astype(float)
img2 = imread(file2).astype(float)
# compare
n_0 = compare_images(img1, img2)
return n_0*1.0/img1.size
# print "Manhattan norm:", n_m, "/ per pixel:", n_m/img1.size
# print "Zero norm:", n_0, "/ per pixel:", n_0*1.0/img1.size

def normalize(arr):
rng = arr.max()-arr.min() + 1e-10
amin = arr.min()
return (arr-amin)*255/rng


def compare_images(img1, img2):
# normalize to compensate for exposure difference, this may be unnecessary
# consider disabling it
img1 = normalize(img1)
img2 = normalize(img2)
# calculate the difference and its norms
diff = img1 - img2 # elementwise for scipy arrays
# m_norm = sum(abs(diff)) # Manhattan norm
z_norm = norm(diff.ravel(), 0) # Zero norm
return z_norm

def main():
video1 = 'rec1.data'
video = 'rec2.data'
generate_keyframes(video1, 'reference_frames')
print check_video(video, 'test_frames')

main()

Binary file added python-api/tests/scripts/reference_frames/foo-1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python-api/tests/scripts/test_frames/foo-1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python-api/tests/scripts/test_frames/foo-10.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python-api/tests/scripts/test_frames/foo-11.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python-api/tests/scripts/test_frames/foo-2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python-api/tests/scripts/test_frames/foo-3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python-api/tests/scripts/test_frames/foo-4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python-api/tests/scripts/test_frames/foo-5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python-api/tests/scripts/test_frames/foo-6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python-api/tests/scripts/test_frames/foo-7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python-api/tests/scripts/test_frames/foo-8.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python-api/tests/scripts/test_frames/foo-9.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 77b7a08

Please sign in to comment.