-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFullscreen
More file actions
executable file
·207 lines (166 loc) · 5.07 KB
/
Fullscreen
File metadata and controls
executable file
·207 lines (166 loc) · 5.07 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
from __future__ import division
from cv import *
from sys import argv, exit
import os
def capture_frame():
"""Capture frame from webcam, convert to grayscale, flip, and smooth."""
global wc
global fm
global inputframe
CvtColor(QueryFrame(wc), inputframe, CV_RGB2GRAY)
Flip(inputframe, flipMode=fm)
Smooth(inputframe, inputframe, CV_MEDIAN)
return inputframe
def hybrid_process(hybframe):
"""Process frame using a gradient-Laplacian hybrid."""
global summation
hybgrad = gradient_process(hybframe)
hyblap = laplacian_process(hybframe)
Add(hybgrad, hyblap, summation)
return summation
def gradient_process(gradframe):
"""Process frame using only a gradient."""
global gradscaled
Resize(gradframe, gradscaled)
return GetMat(gradscaled)
def laplacian_process(lapframe):
"""Process frame using only a Laplacian."""
global th
global clr
global edges
global ldeg
global lapscaled
Threshold(lapframe, lapframe, th, clr, CV_THRESH_BINARY)
Laplace(lapframe, edges, ldeg)
Convert(edges, lapframe)
Resize(lapframe, lapscaled)
return GetMat(lapscaled)
def print_output(printmat):
"""Print the image as a string of ASCII characters"""
printed = ''
for y in range(printmat.height):
printed = printed + ' ' * int((terminalwidth - dw) / 2)
for x in range(printmat.width):
printed = printed + asciiGroup[int(-1 + len(
asciiGroup) - ((printmat[y, x] * len(asciiGroup)) // 256))]
printed = printed + '\n' * (y != (printmat.height - 1))
os.system('clear')
print(printed)
def run(mode, tw, fw, invert):
"""Capture and process feed from webcam"""
initialize(tw, fw, invert)
while True:
rawframe = capture_frame()
if mode == '-c':
printed = hybrid_process(rawframe)
elif mode == '-g':
printed = gradient_process(rawframe)
elif mode == '-l':
printed = laplacian_process(rawframe)
print_output(printed)
WaitKey(1)
def initialize(tw, fw, invert):
"""Initialize settings and images"""
global wc
global asciiGroup
global fm
global th
global clr
global ldeg
global quadwidth
global dqw
global terminalwidth
global dw
global scaledsize
global inputframe
global gradscaled
global edges
global lapscaled
global gradscaled
global summation
# Settings for frame capture
wc = CaptureFromCAM(0)
# Old ASCII Group
# asciiGroup = "####**oahbpqwmzcunxrt-+~<>:,^`... "
asciiGroup = "#########**********oahkbdpqwmzcvunxrjft|?-+~<>!:,^`'.... "
if invert:
asciiGroup = asciiGroup[::-1]
windowsize = GetSize(QueryFrame(wc))
hght = windowsize[1]
wdth = windowsize[0]
flipMode = 1
fm = flipMode
# Settings for Threshold()
threshold = 100
th = threshold
color = 255
clr = color
# Setting for Laplace()
laplaceDegree = 7
ldeg = laplaceDegree
# Settings for sizing (for fullscreened terminal on 1600x900 screen)
terminalwidth = tw
desiredwidth = fw
dw = desiredwidth
scaledsize = (dw, int(dw / 2 * hght // wdth))
# Initialize OpenCV images
inputframe = CreateImage(windowsize, 8, 1)
edges = CreateImage(windowsize, IPL_DEPTH_16S, 1)
hybscaled = CreateImage(scaledsize, 8, 1)
gradscaled = CreateImage(scaledsize, 8, 1)
lapscaled = CreateImage(scaledsize, 8, 1)
summation = CreateMat(scaledsize[1], scaledsize[0], CV_8UC1)
def help():
"""Print help"""
print('-g : Gradient mode')
print('-l : Laplacian mode')
print('-c : Combined mode')
print('-t : Trifecta')
print('-tw <int> : Set width of terminal')
print('-fw <int> : Set width of frame')
print('-i : Invert the output color')
exit(0)
def arg_processing():
mode = False
tw = False
fw = False
invert = False
for i in range(1, len(argv)):
try:
inp = argv[i]
except IndexError:
inp = False
if inp == '-g' or inp == '-l' or inp == '-c' or inp == '-t' and not mode:
mode = inp
elif inp == '-h' or inp == '--help':
help()
elif inp == '-tw':
try:
tw = int(argv[i+1])
except IndexError:
pass
except ValueError:
pass
elif inp == '-fw':
try:
fw = int(argv[i+1])
except IndexError:
pass
except ValueError:
pass
elif inp == '-i':
invert = True
if not mode:
mode = '-c'
if not tw:
tw = 304
if not fw:
fw = 222
if mode == '-t':
os.system('./Trifecta -tw ' + str(tw) +' -fw ' + str(fw) + (' -i'*invert))
exit(0)
return [mode, tw, fw, invert]
if __name__ == '__main__':
[mode, tw, fw, invert] = arg_processing()
run(mode, tw, fw, invert)