Skip to content

Instantly share code, notes, and snippets.

@korayal
Last active August 23, 2022 16:03
Show Gist options
  • Save korayal/fcf235b1307e2461d1c1f8d8b8c2730f to your computer and use it in GitHub Desktop.
Save korayal/fcf235b1307e2461d1c1f8d8b8c2730f to your computer and use it in GitHub Desktop.
Check whether a video file is 3d
#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python39Packages.pillow -p python39Packages.numpy
from PIL import Image
import numpy as np
import subprocess
import argparse
from decimal import *
import time
import glob
import os
def longest_line(boolean_array):
counter = 0
biggest = 0
for i in boolean_array:
if i == True:
counter += 1
if counter > biggest:
biggest = counter
if i == False:
counter = 0
return biggest
def mse(imageA, imageB):
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
return err
def difference(imageA, imageB):
pairs = zip(imageA.getdata(), imageB.getdata())
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = imageA.size[0] * imageB.size[1] * 3
return ((dif / 255.0 * 100) / ncomponents)
def calc_difference(filename):
image = Image.open(filename).convert('RGB')
width, height = image.size
small = image.resize((int(width/2), int(height/2)))
width, height = small.size
width, height = image.size
# # Over/Under. Split into top and bottom halves. The right eye sees the top image.
top = image.crop( (0, 0, width, height/2))
bottom = image.crop( (0, height/2, width, height))
# # Calculate the difference of the top/bottom
tb_diff = difference(top,bottom)
# # Split into left and right halves. The left eye sees the right image.
right = image.crop( (0, 0, width/2, height))
left = image.crop( (width/2, 0, width, height))
# # Calculate the difference of the left/right.
lr_diff = difference(right,left)
return (tb_diff, lr_diff)
def diff_samples(video_name):
now = time.time()
nowstr = str(int(now))
file_prefix = f'/tmp/test-{nowstr}-'
subprocess.run(['ffmpeg', '-loglevel', 'quiet', '-y', '-skip_frame', 'nokey', '-i', video_name, '-vf', 'fps=1/60,scale=720:-1', '-ss', '00:10:00', '-t', '360', f'{file_prefix}%d.png'])
now_after = time.time()
print(f'[**][{now_after - now}] images extracted done')
filenames = glob.glob(f'{file_prefix}*')
diff_tb = []
diff_lr = []
for i,f in enumerate(sorted(filenames)):
try:
tb, lr = calc_difference(f)
diff_tb.append(tb)
diff_lr.append(lr)
except Exception as _:
continue
now_before = time.time()
print(f'[#{i}][{now_before - now_after}] {f}]')
now_after = now_before
if len(diff_tb) > 0:
tb = Decimal(sum(diff_tb) / len(diff_tb)).quantize(Decimal('.001'), rounding=ROUND_DOWN)
lr = Decimal(sum(diff_lr) / len(diff_lr)).quantize(Decimal('.001'), rounding=ROUND_DOWN)
if tb < lr:
print(f'[{video_name}] TB: {tb}')
else:
print(f'[{video_name}] LR: {lr}')
for f in filenames:
os.remove(f)
parser = argparse.ArgumentParser()
parser.add_argument('file', metavar='N', type=str)
args = parser.parse_args()
diff_samples(args.file)
err /= float(imageA.shape[0] * imageA.shape[1])
return err
def difference(imageA, imageB):
pairs = zip(imageA.getdata(), imageB.getdata())
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = imageA.size[0] * imageB.size[1] * 3
return ((dif / 255.0 * 100) / ncomponents)
def calc_difference(filename):
image = Image.open(filename).convert('RGB')
width, height = image.size
small = image.resize((int(width/2), int(height/2)))
width, height = small.size
width, height = image.size
# # Over/Under. Split into top and bottom halves. The right eye sees the top image.
top = image.crop( (0, 0, width, height/2))
bottom = image.crop( (0, height/2, width, height))
# # Calculate the difference of the top/bottom
tb_diff = difference(top,bottom)
# # Split into left and right halves. The left eye sees the right image.
right = image.crop( (0, 0, width/2, height))
left = image.crop( (width/2, 0, width, height))
# # Calculate the difference of the left/right.
lr_diff = difference(right,left)
return (tb_diff, lr_diff)
def diff_samples(video_name):
now = str(int(time.time()))
file_prefix = f'/tmp/test-{now}-'
subprocess.run(['ffmpeg', '-loglevel', 'quiet', '-y', '-skip_frame', 'nokey', '-i', video_name, '-vf', 'fps=6', '-ss', '00:10:00', '-t', '1', f'{file_prefix}%d.png'])
filenames = glob.glob(f'{file_prefix}*')
diff_tb = []
diff_lr = []
for f in filenames:
try:
tb, lr = calc_difference(f)
diff_tb.append(tb)
diff_lr.append(lr)
except Exception as _:
continue
if len(diff_tb) > 0:
tb = Decimal(sum(diff_tb) / len(diff_tb)).quantize(Decimal('.001'), rounding=ROUND_DOWN)
lr = Decimal(sum(diff_lr) / len(diff_lr)).quantize(Decimal('.001'), rounding=ROUND_DOWN)
if tb < lr:
print(f'[{video_name}] TB: {tb}')
else:
print(f'[{video_name}] LR: {lr}')
for f in filenames:
os.remove(f)
parser = argparse.ArgumentParser()
parser.add_argument('file', metavar='N', type=str)
args = parser.parse_args()
diff_samples(args.file)
return err
def difference(imageA, imageB):
pairs = zip(imageA.getdata(), imageB.getdata())
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = imageA.size[0] * imageB.size[1] * 3
return ((dif / 255.0 * 100) / ncomponents)
def calc_difference(filename):
image = Image.open(filename).convert('RGB')
width, height = image.size
small = image.resize((int(width/2), int(height/2)))
width, height = small.size
width, height = image.size
# # Over/Under. Split into top and bottom halves. The right eye sees the top image.
top = image.crop( (0, 0, width, height/2))
bottom = image.crop( (0, height/2, width, height))
# # Calculate the difference of the top/bottom
tb_diff = difference(top,bottom)
# # Split into left and right halves. The left eye sees the right image.
right = image.crop( (0, 0, width/2, height))
left = image.crop( (width/2, 0, width, height))
# # Calculate the difference of the left/right.
lr_diff = difference(right,left)
return (tb_diff, lr_diff)
def diff_samples(video_name):
now = str(int(time.time()))
file_prefix = f'/tmp/test-{now}-'
subprocess.run(['ffmpeg', '-loglevel', 'quiet', '-y', '-skip_frame', 'nokey', '-i', video_name, '-vf', 'fps=6', '-ss', '00:10:00', '-t', '1', f'{file_prefix}%d.png'])
filenames = glob.glob(f'{file_prefix}*')
diff_tb = []
diff_lr = []
for f in filenames:
try:
tb, lr = calc_difference(f)
diff_tb.append(tb)
diff_lr.append(lr)
except Exception as _:
continue
subprocess.run(['rm', f'{file_prefix}*'])
if len(diff_tb) > 0:
tb = Decimal(sum(diff_tb) / len(diff_tb)).quantize(Decimal('.001'), rounding=ROUND_DOWN)
lr = Decimal(sum(diff_lr) / len(diff_lr)).quantize(Decimal('.001'), rounding=ROUND_DOWN)
if tb < lr:
print(f'[{video_name}] TB: {tb}')
else:
print(f'[{video_name}] LR: {lr}')
parser = argparse.ArgumentParser()
parser.add_argument('file', metavar='N', type=str)
args = parser.parse_args()
diff_samples(args.file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment