-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASCII-Camera.py
More file actions
62 lines (38 loc) · 1.74 KB
/
ASCII-Camera.py
File metadata and controls
62 lines (38 loc) · 1.74 KB
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
import os
import cv2
import numpy as np
# Last bits of characters get the darkest coloured pixels while the first characters get the opposite.
# Usage of tuples as its more memory effecient and faster in proformance
ascii_chars = (' ', '.', "'", ',', ':', ';', 'c', 'l', 'x', 'o', '*', '?', 'k', 'X', '&', 'd', 'O', '0', 'K', 'i', 'N', 'G')
# To not show actual live webcam footage
SHOW_REAL_VIDEO = False
COLOURIZED = False # LATER FEATURE
####################### Gray Version ###########################
def rowToAscii(row):
scale = len(ascii_chars)
return tuple(ascii_chars[min(int(x / 256 * scale), scale - 1)] for x in row)
def pixelToAscii(grayVal):
return tuple(rowToAscii(row) for row in grayVal)
def showAscii(gray_frame):
os.system("clear")
print('\n'.join((''.join(row) for row in gray_frame)), end='')
################################################################
capture = cv2.VideoCapture(0)
while(cv2.waitKey(1) & 0xFF != ord('q')):
# Get adjusted size of the terminal
screen_width, screen_height = os.popen('stty size', 'r').read().split()
# read each frame from the capture feed
ret, frame = capture.read()
if not ret:
break
if not COLOURIZED:
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Gray scale the actual colorized frames
adjusted_frame = cv2.resize(gray_frame, (int(screen_height), int(screen_width))) # adjusted cv window to the terminal's dimensions
ascii_frame = pixelToAscii(adjusted_frame)
showAscii(ascii_frame)
if SHOW_REAL_VIDEO:
cv2.imshow('Grayscale Frame', gray_frame)
# else:
# Release capture data when everything is done
capture.release()
cv2.destroyAllWindows()