Skip to content

Instantly share code, notes, and snippets.

@kabachuha
Created September 23, 2022 21:55
Show Gist options
  • Save kabachuha/5e165322382848114a59d2a855f49330 to your computer and use it in GitHub Desktop.
Save kabachuha/5e165322382848114a59d2a855f49330 to your computer and use it in GitHub Desktop.
numexpr parser test for deforum args
0:(1.5), 20:(3), 40:(15), 60:(0), 80:(-15), 100:(-3), 120:(0), 240(3), 260(-3), 300(0)
import os
import numpy as np
import numexpr
import pandas as pd
import re
def get_inbetweens(key_frames, max_frames, evaluate_only_keys = True, integer=False, interp_method='Linear'):
key_frame_series = pd.Series([np.nan for a in range(max_frames)])
for i in range(0, max_frames):
if i in key_frames:
value = key_frames[i]
# if not all, leave the rest for the default interpolation
if evaluate_only_keys:
t = i
key_frame_series[i] = numexpr.evaluate(value)
if not evaluate_only_keys:
t = i
key_frame_series[i] = numexpr.evaluate(value)
key_frame_series = key_frame_series.astype(float)
if interp_method == 'Cubic' and len(key_frames.items()) <= 3:
interp_method = 'Quadratic'
if interp_method == 'Quadratic' and len(key_frames.items()) <= 2:
interp_method = 'Linear'
key_frame_series[0] = key_frame_series[key_frame_series.first_valid_index()]
key_frame_series[max_frames-1] = key_frame_series[key_frame_series.last_valid_index()]
key_frame_series = key_frame_series.interpolate(method=interp_method.lower(), limit_direction='both')
if integer:
return key_frame_series.astype(int)
return key_frame_series
def parse_key_frames(string, prompt_parser=None):
import re
pattern = r'((?P<frame>[0-9]+):[\s]*(?P<param>[\S\s]*?)([,][\s]|[\s]?$))' #totally works!!!
frames = dict()
for match_object in re.finditer(pattern, string):
frame = int(match_object.groupdict()['frame'])
param = match_object.groupdict()['param']
if prompt_parser:
frames[frame] = prompt_parser(param)
else:
frames[frame] = param
if frames == {} and len(string) != 0:
raise RuntimeError('Key Frame string not correctly formatted')
return frames
max_frames = 200
evaluate_only_keys = True
with open('more.txt', 'r', encoding='utf-8') as f:
data = f.read()
print(data)
translation_x_series = get_inbetweens(parse_key_frames(data), max_frames, evaluate_only_keys)
print(translation_x_series)
translation_x_series.to_csv('kek.csv')
0:(1.0+sin(t**2)), 20:(3), 40:(1.0+sin(t**2))
0:(1.0+sin(t**2)), 20:(3), 40:(1.0+cos(t**2)), 60:(0), 80:(-15), 100:(-3), 120:(0), 240(3), 260(-3), 300(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment