From e415eeb45b724949ea6e6e8f98be65ae5cb90a89 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 18 Jul 2013 10:20:00 +0000 Subject: [PATCH] get sequence has for one average frame per cut, fixes #1391 --- pandora/sequence/extract.py | 62 +++++++++++++++++++++++++++++++++++++ pandora/sequence/tasks.py | 3 +- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/pandora/sequence/extract.py b/pandora/sequence/extract.py index 85a73fcdf..a1747caed 100644 --- a/pandora/sequence/extract.py +++ b/pandora/sequence/extract.py @@ -3,6 +3,8 @@ from __future__ import division import Image import os +import numpy +import math ZONE_INDEX = [] for pixel_index in range(64): @@ -79,3 +81,63 @@ def get_sequences(path, position=0): sequences[mode][-1]['out'] = position return sequences, position +def get_cut_sequences(stream, position=0): + path = stream.timeline_prefix + cuts = list(stream.cuts) + [stream.duration] + modes = ['color', 'shape'] + sequences = {} + for mode in modes: + sequences[mode] = [] + position_start = position + fps = 25 + file_names = filter(lambda x: 'timelinedata8p' in x, os.listdir(path)) + file_names = sorted(file_names, key=lambda x: int(x[14:-4])) + file_names = map(lambda x: path + x, file_names) + + def add_hash(cut, cut_data): + if sequences['color']: + start = sequences['color'][-1]['out'] + else: + start = 0 + end = position + frames = int(math.ceil((end - start) * fps)) + #print 'add', start, end, frames + if frames: + cut_data /= frames + frame_image = Image.new('RGB', (8,8)) + frame_image.putdata(list(tuple(pixel) for pixel in cut_data)) + for mode in modes: + frame_hash = get_hash(frame_image, mode) + if sequences[mode] and sequences[mode][-1]['hash'] == frame_hash: + sequences[mode][-1]['out'] = end + else: + sequences[mode].append({ + 'hash': frame_hash, + 'in': start, + 'out': end, + }) + else: + print 'fixme', cut_data + + def next_cut(): + if cuts: + cut = cuts.pop(0) + else: + cut = 0 + return numpy.array(Image.new('RGB', (8,8)).getdata(), numpy.int64), cut + + cut_data, cut = next_cut() + + for file_name in file_names: + timeline_image = Image.open(file_name) + timeline_width = timeline_image.size[0] + for x in range(0, timeline_width, 8): + frame_image = timeline_image.crop((x, 0, x + 8, 8)) + cut_data += numpy.array(frame_image.getdata(), numpy.int64) + position += 1 / fps + if cut and position > cut: + add_hash(cut, cut_data) + cut_data, cut = next_cut() + position += 1 / fps + add_hash(cut, cut_data) + return sequences, position diff --git a/pandora/sequence/tasks.py b/pandora/sequence/tasks.py index 5916c4839..0353f7d77 100644 --- a/pandora/sequence/tasks.py +++ b/pandora/sequence/tasks.py @@ -13,7 +13,8 @@ def get_sequences(itemId): models.Sequence.objects.filter(sort=i.sort).delete() position = 0 for stream in i.streams(): - data, position = extract.get_sequences(stream.timeline_prefix, position) + #data, position = extract.get_sequences(stream.timeline_prefix, position) + data, position = extract.get_cut_sequences(stream, position) keys = None values = [] for mode in data: