-
Notifications
You must be signed in to change notification settings - Fork 1
/
ascii_video.py
executable file
·166 lines (137 loc) · 4.43 KB
/
ascii_video.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import curses
import cv2
import threading
from queue import Queue
import random
import numpy
import sys
import getopt
char_pixel_map = {
0: ' ',
2: '`',
4: '\'.',
6: '-',
7: '^,',
8: '\\/:"',
9: '~_!<>',
11: ';',
12: '=',
13: '()+|?',
14: 'i',
15: 'j',
16: 'l17{}',
17: 'tv[]',
18: 'crI',
19: 'fuJ2',
20: 'oxLO3%',
21: 'ansyzC*',
22: 'V',
23: 'Y4569',
24: 'ehNTZ0&',
25: 'bdkwAFP',
26: 'DX8$',
27: 'pqGU',
28: 'gKQS',
29: 'BE',
30: 'mRW#',
31: 'H',
36: 'M@'
}
show_height = 44
show_width = 160
board = [[]]
file_name = ''
speed = 10
def update_view(gray, y, x, block_height, block_width):
avg_gray = numpy.mean([row[x:x + block_width] for row in gray[y:y + block_height]])
keys = list(char_pixel_map.keys())
candidates = char_pixel_map[keys[int(numpy.power(avg_gray / 256, 1.25) * len(keys))]]
ch = candidates[int(random.uniform(0, len(candidates)))]
# print('{0}, {1}'.format(int(i / block_height) + 1, int(j / block_width) + 1))
return ch
def update_view_callback(r):
result = r.result()
if result[0] < show_height - 2 and result[1] < show_width - 2:
board[result[0]][result[1]] = result[2]
def print_image(stdscr, filename, shared_queue):
pause = False
cap = cv2.VideoCapture(filename)
frame_cnt = 0
while cap.isOpened():
if not pause:
ret, frame = cap.read()
if ret and frame_cnt % speed == 0:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# cv2.imshow('frame', gray)
frame_height = len(gray)
frame_width = len(gray[0])
stdscr.addstr(0, 1, "[{2}] height: {0}, width: {1}".format(frame_height, frame_width, frame_cnt))
frame_cnt += 1
# Convert image data to characters
block_height = int(frame_height / show_height)
block_width = int(frame_width / show_width)
for i in range(0, frame_height, block_height):
for j in range(0, frame_width, block_width):
ch = update_view(gray, i, j, block_height, block_width)
y, x = int(i / block_height), int(j / block_width)
if y < show_height - 2 and x < show_width - 2:
stdscr.addch(y + 1, x + 1, ch)
stdscr.refresh()
else:
frame_cnt += 1
# Check data sent from parent thread
if shared_queue.qsize() > 0:
command = shared_queue.get();
if command == 'stop':
break
elif command == 'pause':
pause = not pause
cap.release()
def paint_ui(stdscr):
stdscr.resize(show_height, show_width)
stdscr.border()
def init_scr(stdscr):
stdscr.clear()
global show_height, show_width, board
show_width = stdscr.getmaxyx()[1]
show_height = int(show_width / 32 * 9)
if show_height > stdscr.getmaxyx()[0]:
show_height = stdscr.getmaxyx()[0]
show_width = int(show_height / 9 * 32)
board = [[0 for col in range(show_width - 2)] for row in range(show_height - 2)]
paint_ui(stdscr)
stdscr.refresh()
shared_queue = Queue()
thread = threading.Thread(target=print_image, args=(stdscr, file_name, shared_queue), daemon=True)
thread.start()
while True:
ch = stdscr.getch()
if ch == ord('q'):
# Ask child to terminate
shared_queue.put('stop')
thread.join(2)
break
elif ch == ord('p'):
shared_queue.put('pause')
if __name__ == '__main__':
argv = sys.argv[1:]
if len(argv) < 1:
print('ascii_video.py -i <inputfile> [-s <speed>]')
sys.exit(1)
try:
opts, args = getopt.getopt(argv, "hi:s:")
except getopt.GetoptError:
print('ascii_video.py -i <inputfile> [-s <speed>]')
sys.exit(1)
for opt, arg in opts:
if opt == '-h':
print('ascii_video.py -i <inputfile> [-s <speed>]\n-i: 指定输入视频文件\n' +
'-s: 播放速度, 与硬件性能有关, 默认为10, 数值越大越快\n运行中按q退出, 按p暂停/恢复')
sys.exit()
elif opt == '-i':
file_name = arg
elif opt == '-s':
speed = int(arg)
curses.wrapper(init_scr)