2019-01-01 21:50:01 +00:00
|
|
|
from PIL import Image
|
|
|
|
import numpy as np
|
|
|
|
|
2019-02-23 23:32:07 +00:00
|
|
|
import frame_sequence
|
2019-01-05 23:31:56 +00:00
|
|
|
import opcodes
|
2019-01-01 21:50:01 +00:00
|
|
|
import screen
|
2019-01-05 23:31:56 +00:00
|
|
|
import video
|
2019-01-01 21:50:01 +00:00
|
|
|
|
2019-02-27 14:10:39 +00:00
|
|
|
MAX_OUT = 10 * 1024 * 1024
|
2019-01-01 21:50:01 +00:00
|
|
|
VIDEO_FPS = 30
|
2019-02-27 14:10:39 +00:00
|
|
|
APPLE_FPS = 15
|
2019-01-02 22:16:54 +00:00
|
|
|
|
2019-01-02 23:10:03 +00:00
|
|
|
|
2019-01-01 21:50:01 +00:00
|
|
|
def main():
|
2019-02-23 23:32:07 +00:00
|
|
|
frames = frame_sequence.frame_sequence(
|
|
|
|
"Computer Chronicles - 06x05 - The Apple II.mp4")
|
2019-01-02 22:16:54 +00:00
|
|
|
|
2019-02-23 23:32:07 +00:00
|
|
|
bytes_out = 0
|
2019-02-27 14:10:39 +00:00
|
|
|
sims = []
|
|
|
|
out_frames = 0
|
2019-01-02 22:16:54 +00:00
|
|
|
|
2019-02-23 23:32:07 +00:00
|
|
|
s = video.Video(APPLE_FPS)
|
|
|
|
screen_cls = screen.HGR140Bitmap
|
2019-01-05 23:31:56 +00:00
|
|
|
|
2019-02-23 23:32:07 +00:00
|
|
|
# Assert that the opcode stream reconstructs the same screen
|
|
|
|
ds = video.Video()
|
|
|
|
decoder = opcodes.Decoder(s.state)
|
2019-01-05 23:31:56 +00:00
|
|
|
|
2019-02-23 23:32:07 +00:00
|
|
|
with open("out.bin", "wb") as out:
|
|
|
|
for idx, frame in enumerate(frames):
|
2019-01-01 21:50:01 +00:00
|
|
|
if idx % (VIDEO_FPS // APPLE_FPS):
|
|
|
|
continue
|
2019-01-05 23:31:56 +00:00
|
|
|
|
2019-01-01 21:50:01 +00:00
|
|
|
im = Image.fromarray(frame)
|
2019-01-05 23:31:56 +00:00
|
|
|
im = im.resize((screen_cls.XMAX, screen_cls.YMAX))
|
2019-01-01 21:50:01 +00:00
|
|
|
im = im.convert("1")
|
2019-01-02 00:24:25 +00:00
|
|
|
im = np.array(im)
|
2019-01-01 21:50:01 +00:00
|
|
|
# im.show()
|
|
|
|
|
2019-01-05 23:31:56 +00:00
|
|
|
f = screen_cls(im)
|
|
|
|
|
2019-02-23 23:32:07 +00:00
|
|
|
stream = bytes(s.emit_stream(s.encode_frame(f)))
|
2019-01-01 21:50:01 +00:00
|
|
|
|
|
|
|
# assert that the screen decodes to the original bitmap
|
2019-01-05 23:31:56 +00:00
|
|
|
bm = screen_cls.from_bytemap(s.screen).bitmap
|
2019-01-02 00:24:25 +00:00
|
|
|
|
|
|
|
# print("Comparing bitmaps")
|
|
|
|
# print(np.array(im))
|
|
|
|
# print(bm)
|
|
|
|
# print(s.screen)
|
2019-02-23 23:32:07 +00:00
|
|
|
#np.set_printoptions(threshold=100000000)
|
|
|
|
|
|
|
|
#assert np.array_equal(bm, im), np.ma.masked_array(
|
2019-01-02 00:03:21 +00:00
|
|
|
# bm, np.logical_not(np.logical_xor(bm, im)))
|
2019-01-01 21:50:01 +00:00
|
|
|
|
|
|
|
# d = Image.fromarray(s.screen)
|
|
|
|
# d.show()
|
|
|
|
|
|
|
|
bytes_out += len(stream)
|
2019-02-23 23:32:07 +00:00
|
|
|
bytes_left = MAX_OUT - bytes_out
|
2019-01-01 21:50:01 +00:00
|
|
|
|
2019-02-27 14:10:39 +00:00
|
|
|
sim = screen.bitmap_similarity(im, bm)
|
|
|
|
sims.append(sim)
|
|
|
|
out_frames += 1
|
2019-01-02 00:24:25 +00:00
|
|
|
print("Frame %d, %d bytes, similarity = %f" % (
|
2019-02-27 14:10:39 +00:00
|
|
|
idx, len(stream), sim))
|
2019-02-23 23:32:07 +00:00
|
|
|
out.write(stream[:bytes_left])
|
|
|
|
|
|
|
|
if bytes_left <= 0:
|
|
|
|
out.write(bytes(s.done()))
|
|
|
|
break
|
2019-01-01 21:50:01 +00:00
|
|
|
|
2019-02-27 14:10:39 +00:00
|
|
|
print("Median similarity: %f" % sorted(sims)[out_frames//2])
|
2019-01-01 21:50:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|