-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
56 lines (41 loc) · 1.42 KB
/
main.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
import sys
import numpy as np
import cv2 as cv
import time
import imutils
ascii_chars = '█$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,"^`\' '[::-1]
def image_to_text(image: np.ndarray) -> str:
text = ''
for i in range(image.shape[0]):
text += '\n'
for k in range(image.shape[1]):
greyscale_value = image[i][k]
char_index = int((greyscale_value / 256) * (len(ascii_chars)))
text += ascii_chars[char_index]
return text
def reset_cursor(height: int) -> None:
print(f'\033[{height}A', flush=True)
def main() -> None:
if len(sys.argv) < 2:
print('Please provide the video path.')
return
cap = cv.VideoCapture(sys.argv[1])
if not cap.isOpened():
print('Please provide a valid video path.')
return
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
cap.release()
return
image = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Set a correct size and aspect ration for terminal
image = imutils.resize(image, width=80)
image = cv.resize(image, (80, image.shape[0] // 2))
image_as_text = image_to_text(image)
print(image_as_text, end='', flush=True)
reset_cursor(height=image.shape[1])
time.sleep(0.04)
if __name__ == '__main__':
main()