Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/target
assets/boxes.json
/apple_frames
*.mp4
.cargo/config
69 changes: 49 additions & 20 deletions bad apple.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,63 @@
import cv2
from tqdm import tqdm
import numpy as np
import argparse

parser = argparse.ArgumentParser(
prog="Boxes utility")

parser.add_argument('-i', '--input', action='store')
args = parser.parse_args()

user_option = 0

print("Boxes conversion utility")
if not args.input:
print("Select an option:")
print("(1) Convert video to boxes.json")
print("(2) Convert boxes.json to boxes.bin")
user_option = int(input("Option: "))
else:
user_option = 1

# invalid options
if user_option > 2 or user_option < 1:
print("Invalid option")
exit()

if user_option == 2:
# checks and such
with open("assets/boxes.json") as f:
j = json.load(f)
print(f"Most visible windows: {max(len(b) for b in j)}")
print(f"Total frames: {len(j)}")
print(f"Total window changes: {sum(len(b) for b in j)}")
print(
f"Base width: {max(max((coords[0]+coords[2] for coords in b), default=0) for b in j)} Base height: {max(max((coords[1]+coords[3] for coords in b), default=0) for b in j)}"
)

# checks and such
with open("assets/boxes.json") as f:
j = json.load(f)
print(f"Most visible windows: {max(len(b) for b in j)}")
print(f"Total frames: {len(j)}")
print(f"Total window changes: {sum(len(b) for b in j)}")
print(
f"Base width: {max(max((coords[0]+coords[2] for coords in b), default=0) for b in j)} Base height: {max(max((coords[1]+coords[3] for coords in b), default=0) for b in j)}"
)

print("Serialising box-o'-bytes to boxes.bin")
with open("assets/boxes.bin", "wb") as f:
for frame in j:
for window in frame:
f.write(bytes(window))
# null window signifies new frame
f.write(bytes([0, 0, 0, 0]))
exit()
print("Serialising box-o'-bytes to boxes.bin")
with open("assets/boxes.bin", "wb") as f:
for frame in j:
for window in frame:
f.write(bytes(window))
# null window signifies new frame
f.write(bytes([0, 0, 0, 0]))
exit()

# whole arrays printed, debug
# np.set_printoptions(threshold=np.inf)

inp = "【東方】Bad Apple!! PV【影絵】 [FtutLA63Cp8].webm"
# try to use the arg inputs, otherwise fallback to user input
inp = args.input or input("Input video: ")
out = "apple_frames"
max_width = 64
threshold = 255 * 0.4

if not os.path.isdir(out):
print("Cannot find output folder, creating one...")
os.makedirs(out)


def frame_to_boxes(im: Image, name):
w, h = im.size
Expand Down Expand Up @@ -174,7 +203,7 @@ def frame_to_boxes(im: Image, name):

cap.release()
finally:
with open("boxes.json", "w") as f:
with open("assets/boxes.json", "w") as f:
json.dump(all_boxes, f)

# im = Image.open('bad apple.jpg')
Expand Down