Skip to content

Commit

Permalink
absurd number of bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
CiaraStrawberry committed Apr 25, 2023
1 parent 8f1814a commit a1f61ee
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 65 deletions.
31 changes: 17 additions & 14 deletions scripts/Berry_Method.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def split_frames_into_big_batches(frames, batch_size, border,ebsynth,returnframe

end_idx = min(end_idx, num_frames)
batches.append(frames[start_idx:end_idx])
print (f"batch {i} has {len(batches[i])} frames")
frame_locations.append((start_idx,end_idx))

if returnframe_locations == False:
Expand Down Expand Up @@ -178,7 +179,7 @@ def generate_square_from_video(video_path, fps, batch_size,resolution,size_size)

return square_texture

def generate_squares_to_folder (video_path, fps, batch_size,resolution,size_size,max_frames,output_folder,border,ebsynth_mode):
def generate_squares_to_folder (video_path, fps, batch_size,resolution,size_size,max_frames,output_folder,border,ebsynth_mode,max_frames_to_save):

if ebsynth_mode == False:
if border >= (batch_size * size_size * size_size) / 2:
Expand Down Expand Up @@ -236,7 +237,7 @@ def generate_squares_to_folder (video_path, fps, batch_size,resolution,size_size
f.write(str(size_size) + "\n")
f.write(str(batch_size) + "\n")
f.write(str(video_path) + "\n")
f.write(str(max_frames) + "\n")
f.write(str(max_frames_to_save) + "\n")
f.write(str(border) + "\n")
#return list of urls

Expand Down Expand Up @@ -285,7 +286,7 @@ def process_video_batch (video_path_old, fps, per_side, batch_size, fillindenois
per_batch_limmit = (((per_side * per_side) * batch_size)) + border
video_data = convert_video_to_bytes(video_path)
frames = utilityb.extract_frames_movpie(video_data, fps, max_frames)
print(f"full frames num = {len(frames)}")
print(f"splitting into batches with per_batch_limmit = {per_batch_limmit} and border {border}" )
bigbatches = split_frames_into_big_batches(frames, per_batch_limmit,border,ebsynth=False)
bigprocessedbatches = []
for i , batch in enumerate(bigbatches):
Expand All @@ -296,6 +297,7 @@ def process_video_batch (video_path_old, fps, per_side, batch_size, fillindenois
Image.fromarray(image).save(os.path.join(output_folder, f"result/output{a + (len(new_batch) * i)}.png"))

just_frame_groups = []
print (f"bigprocessedbatches len = {len(bigprocessedbatches)}")
for i in range(len(bigprocessedbatches)):
newgroup = []
for b in range(len(bigprocessedbatches[i])):
Expand Down Expand Up @@ -604,13 +606,13 @@ def divideFrames(frame_groups, x, y):
if end + y <= len(group):
overlap_group = group[end:end+y]

# Resize the images in new_group and overlap_group to ensure they have the same shape
#reference_shape = new_group[0].shape
#new_group_resized = [cv2.resize(img, (reference_shape[1], reference_shape[0])) for img in new_group]
#overlap_group_resized = [cv2.resize(img, (reference_shape[1], reference_shape[0])) for img in overlap_group]

# Concatenate the images from new_group and overlap_group
combined_group = np.concatenate((new_group, overlap_group), axis=0)
if y > 0:
combined_group = np.concatenate((new_group, overlap_group), axis=0)
else:
combined_group = new_group
print (f"overlap group size {len(overlap_group)}")
transitions.append(len(result))

else:
Expand All @@ -622,7 +624,7 @@ def divideFrames(frame_groups, x, y):
return result,transitions


def trim_images(images_list_of_lists, max_images,border_indices):
def trim_images(images_list_of_lists, max_images, border_indices):
"""
Trims the given list of lists of image arrays so that the total number of image arrays is below the specified maximum.
Removes whole image arrays from the end of the list of lists if the max_images doesn't include them.
Expand All @@ -637,16 +639,17 @@ def trim_images(images_list_of_lists, max_images,border_indices):
total_images = sum([len(img_list) for img_list in images_list_of_lists])

while total_images > max_images:
print (f"total_images = {total_images}, max_images = {max_images}")
print(f"total_images = {total_images}, max_images = {max_images}")
last_list_idx = len(images_list_of_lists) - 1
last_img_idx = len(images_list_of_lists[last_list_idx]) - 1

if last_img_idx >= 0:
total_images -= 1
images_list_of_lists[last_list_idx].pop()
images_list_of_lists[last_list_idx] = images_list_of_lists[last_list_idx][:-1]

if len(images_list_of_lists[last_list_idx]) == 0:
images_list_of_lists.pop()
border_indices.pop()
if last_list_idx in border_indices:
border_indices.pop()

return images_list_of_lists,border_indices
return images_list_of_lists, border_indices
26 changes: 15 additions & 11 deletions scripts/Ebsynth_Processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def recombine (video_path, fps, per_side, batch_size, fillindenoise, edgedenoise



def crossfade_folder_of_folders(output_folder, fps):
def crossfade_folder_of_folders(output_folder, fps,return_generated_video_path=False):
"""Crossfade between images in a folder of folders and save the results."""
root_folder = output_folder
all_dirs = [d for d in os.listdir(root_folder) if os.path.isdir(os.path.join(root_folder, d))]
Expand Down Expand Up @@ -152,7 +152,7 @@ def crossfade_folder_of_folders(output_folder, fps):



for j in range(keynum, len(images_current)):
for j in range(keynum, len(images_current) - 1):
alpha = (j - keynum) / (len(images_current) - keynum)
image1_path = os.path.join(current_dir, images_current[j])
next_image_index = j - keynum if j - keynum < len(images_next) else len(images_next) - 1
Expand All @@ -166,20 +166,24 @@ def crossfade_folder_of_folders(output_folder, fps):
# blended_image.save(os.path.join(output_folder, f"{dirs[i]}_{dirs[i+1]}_crossfade_{j:04}.png"))

final_dir = os.path.join(root_folder, dirs[-1])
for c in range(allkeynums[-1], len(final_dir)):
images_final = sorted(os.listdir(final_dir))
if c >= len(images_final):
break
image1_path = os.path.join(final_dir, images_final[c])
final_dir_images = sorted(os.listdir(final_dir))
start_point = len(final_dir_images) // 2
print(f"going from dir {start_point} to end at {len(final_dir_images)}")

for c in range(start_point, len(final_dir_images)):
image1_path = os.path.join(final_dir, final_dir_images[c])
image1 = Image.open(image1_path)
output_images.append(np.array(image1))




print (f"outputting {len(output_images)} images")
output_save_location = os.path.join(output_folder, "crossfade.mp4")
generated_vid = extensions.TemporalKit.scripts.berry_utility.pil_images_to_video(output_images, output_save_location, fps)
return generated_vid

if return_generated_video_path == True:
return generated_vid
else:
return output_images

def getkeynums (folder_path):
filenames = os.listdir(folder_path)
Expand Down
38 changes: 26 additions & 12 deletions scripts/berry_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,13 +551,13 @@ def crossfade_videos(video_paths,fps, overlap_indexes, num_overlap_frames, outpu
output it to the output file location
"""


original_frames_arrays = []
for i in range(len(video_paths)):
data = convert_video_to_bytes(video_paths[i])
frames_list = extract_frames_movpie(data, fps)
original_frames_arrays.append(frames_list)
print (f"video {i} has {len(frames_list)} frames")
#not video paths any more frame arrays
original_frames_arrays = video_paths
#for i in range(len(video_paths)):
# data = convert_video_to_bytes(video_paths[i])
## frames_list = extract_frames_movpie(data, fps)
# original_frames_arrays.append(frames_list)
# print (f"video {i} has {len(frames_list)} frames")
new_frames_arrays = copy.deepcopy(original_frames_arrays)

for index, frames_array in enumerate(original_frames_arrays):
Expand All @@ -576,7 +576,7 @@ def crossfade_videos(video_paths,fps, overlap_indexes, num_overlap_frames, outpu
last_of_current.pop() # remove the last element from array2

crossfaded = []
for i in range(num_overlap_frames - 1):
for i in range(num_overlap_frames):
alpha = 1 - (i / num_overlap_frames) # set alpha value
if i > len(last_of_current) - 1 or i > len(first_of_next) - 1:

Expand All @@ -586,6 +586,8 @@ def crossfade_videos(video_paths,fps, overlap_indexes, num_overlap_frames, outpu
new_frame = crossfade_frames(last_of_current[i], first_of_next[i], alpha)
#print (new_frame.shape)
crossfaded.append(new_frame)
print (f"crossfaded {len(crossfaded)} frames with num overlap = {num_overlap_frames}, the last of current array is of length {len(last_of_current)} and the first of next is of length {len(first_of_next)}")
#saving first of next and last of current
new_frames_arrays[index][-num_overlap_frames:] = crossfaded

if index > 0 and index - 1 in overlap_indexes:
Expand All @@ -597,7 +599,9 @@ def crossfade_videos(video_paths,fps, overlap_indexes, num_overlap_frames, outpu
output_array = []
for arr in new_frames_arrays:
for frame in arr:
output_array.append(Image.fromarray(frame))
#frame = cv2.resize(frame, (new_frames_arrays, new_height), interpolation=cv2.INTER_LINEAR)
#print (frame.shape)
output_array.append(Image.fromarray(frame).convert("RGB"))
return pil_images_to_video(output_array, output_path, fps)


Expand Down Expand Up @@ -638,7 +642,8 @@ def interpolate_frames(frame1, frame2, ratio):
frame_time = 1 / target_fps
current_time = 0

while current_time < video_duration:

while current_time < video_duration and (max_frames is None or len(frames) < max_frames):
frame1_time = current_time * frame_ratio
frame2_time = min((current_time + frame_time) * frame_ratio, video_duration)
frame1 = video_clip.get_frame(frame1_time)
Expand All @@ -652,7 +657,10 @@ def interpolate_frames(frame1, frame2, ratio):

frames.append(frame)
current_time += frame_time


if max_frames is not None and len(frames) > max_frames:
frames = frames[:max_frames]

print(f"Extracted {len(frames)} frames at {target_fps} fps over a clip with a length of {len(frames) / target_fps} seconds with the old duration of {video_duration} seconds")
return frames

Expand All @@ -675,11 +683,17 @@ def split_video_into_numpy_arrays(video_path, target_fps=None, perform_interpola
video_manager.start()

scene_manager.detect_scenes(frame_source=video_manager)
scene_list = scene_manager.get_scene_list()
scene_list = scene_manager.get_scene_list(start_in_scene=True)

if target_fps is not None:
original_fps = video_manager.get(cv2.CAP_PROP_FPS)

if len(scene_list) == 0:
start_time = 0
end_time = video_manager.get(cv2.CAP_PROP_FRAME_COUNT) / video_manager.get(cv2.CAP_PROP_FPS)
scene_list.append((start_time, end_time))

print (f"Detected {len(scene_list)} scenes")
numpy_arrays = save_scenes_as_numpy_arrays(scene_list, video_path, target_fps, original_fps if target_fps else None, perform_interpolation)

print(f"Total scenes: {len(numpy_arrays)}")
Expand Down
Loading

0 comments on commit a1f61ee

Please sign in to comment.