Last active
January 6, 2020 06:24
-
-
Save Sam-Belliveau/a60021a782e2ed241879bc2ab7dce636 to your computer and use it in GitHub Desktop.
this generates huge amounts of code for any mp4 you give it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import random | |
| VIDEO_FILE="test.mp4" | |
| WIDTH=40 | |
| HEIGHT=30 | |
| FRAMERATE=4 | |
| COMPRESSION=2 # [0, 1, 2] | |
| code = open("test_code.txt", "w") | |
| def do_patch(image, last_image, key_frame=True): | |
| w = image.shape[0] | |
| h = image.shape[1] | |
| for y in range(0, h): | |
| for x in range(0, w): | |
| dif0 = abs(int(image[x, y, 0]) - int(last_image[x, y, 0])) | |
| dif1 = abs(int(image[x, y, 1]) - int(last_image[x, y, 1])) | |
| dif2 = abs(int(image[x, y, 2]) - int(last_image[x, y, 2])) | |
| dif = max(dif0, dif1, dif2) | |
| if((dif >= (26 * COMPRESSION) or key_frame)): | |
| line = "p {} {} {} {} {} ".format(y, x, image[x, y, 2] // 26, image[x, y, 1] // 26, image[x, y, 0] // 26) | |
| code.write(line) | |
| last_image[x,y] = image[x,y] | |
| code.write("d ") | |
| def do_resize(image): | |
| return cv2.resize(image, (WIDTH, HEIGHT)) | |
| video = cv2.VideoCapture(VIDEO_FILE) | |
| success, image = video.read() | |
| resized = do_resize(image) | |
| last_image = resized | |
| code.write("to p [x y r g b] ask patch x (0 - y) [ set pcolor (list (r * 25) (g * 25) (b * 25)) ]end ") | |
| code.write("to d display end ") | |
| code.write("to go ") | |
| frame = 0 | |
| while (success): | |
| resized = do_resize(image) | |
| frame += 1 | |
| if(frame % (30/FRAMERATE) == 0): | |
| do_patch(resized, last_image, False) | |
| success, image = video.read() | |
| code.write("\nend\n") | |
| code.flush() | |
| code.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment