-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
74 lines (52 loc) · 1.85 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from multiprocessing.pool import ThreadPool
from multiprocessing import cpu_count
from typing import Tuple
import time
import cv2
import tqdm
import playsound
def draw_frame(frame_information: Tuple[int, cv2.VideoCapture]):
order, frame = frame_information
y, x, _ = frame.shape
frame_str = ''
pixel_row = 0
for this_y in range(y):
for this_x in range(x):
pixel = '@' if frame[this_y, this_x].all() == 0 else ' '
frame_str += pixel
pixel_row += 1
if pixel_row == x:
frame_str += '\n'
pixel_row = 0
return order, frame_str
def generate_frames(video: cv2.VideoCapture):
success = True
order = 0
while success:
success, frame = video.read()
_, bw_frame = cv2.threshold(frame, 128, 255, cv2.THRESH_BINARY)
if bw_frame is None:
break
y, x, _ = bw_frame.shape
bw_frame = cv2.resize(bw_frame, (x // 4, y // 5))
yield order, bw_frame
order += 1
if __name__ == '__main__':
VIDEO_PATH = './assets/badapple.mp4'
VIDEO = cv2.VideoCapture(VIDEO_PATH)
VIDEO_FRAMES_COUNT = int(VIDEO.get(cv2.CAP_PROP_FRAME_COUNT))
with ThreadPool(processes=cpu_count()) as pool:
frames = list(tqdm.tqdm(pool.imap(draw_frame, generate_frames(VIDEO)), total=VIDEO_FRAMES_COUNT))
pool.close()
pool.join()
frames = sorted(frames, key=lambda x: x[0])
playsound.playsound('./assets/badapple.mp3', block=False)
FPS = 30
skip_ticks = 1/(FPS*1.0)
next_snap = time.time()
for _, frame in frames:
next_snap += skip_ticks
sleep_time = next_snap - time.time()
if sleep_time > 0:
print(frame)
time.sleep(sleep_time)