-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrifecta.py
More file actions
143 lines (112 loc) · 4.14 KB
/
Trifecta.py
File metadata and controls
143 lines (112 loc) · 4.14 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
from __future__ import division
from cv import *
import os
def capture_frame():
"""Capture frame from webcam, convert to grayscale, flip, and smooth."""
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."""
hybprinted = gradient_process(hybframe)
Threshold(hybframe, hybframe, th, clr, CV_THRESH_BINARY)
Laplace(hybframe, edges, ldeg)
Convert(edges, hybframe)
Resize(hybframe, scaled)
pixelnum = 0
for y in range(scaled.height):
pixelnum += int((terminalwidth - dw) / 2)
for x in range(scaled.width):
pixelnum += 1
if scaled[y, x] != 0:
hybprinted = (hybprinted[:pixelnum - 1] + asciiGroup[0] + (
hybprinted[pixelnum:] * (pixelnum != len(hybprinted) - 1)))
pixelnum += 1 * (y != (scaled.height - 1))
return hybprinted
def gradient_process(gradframe):
"""Process frame using only a gradient."""
Resize(gradframe, scaled)
printed = ''
for y in range(scaled.height):
printed = printed + ' ' * int((terminalwidth - dw) / 2)
for x in range(scaled.width):
printed = printed + asciiGroup[int(-1 + len(
asciiGroup) - ((scaled[y, x] * len(asciiGroup)) // 256))]
printed = printed + '\n' * (y != scaled.height - 1)
return printed
def laplacian_process(lapframe):
"""Process frame using only a Laplacian."""
Threshold(lapframe, lapframe, th, clr, CV_THRESH_BINARY)
Laplace(lapframe, edges, ldeg)
Convert(edges, lapframe)
Resize(lapframe, scaled)
printed = ''
for y in range(scaled.height):
printed = printed + ' ' * int((terminalwidth - dw) / 2)
for x in range(scaled.width):
printed = printed + asciiGroup[int(-1 + len(
asciiGroup) - ((scaled[y, x] * len(asciiGroup)) // 256))]
printed = printed + '\n' * (y != scaled.height - 1)
return printed
def all_process(rawframe):
"""Process frame all three ways."""
Copy(rawframe, gradframe)
Copy(rawframe, lapframe)
Copy(rawframe, hybframe)
gradientprinted = gradient_process(gradframe).split('\n')
laplacianprinted = laplacian_process(lapframe).split('\n')
hybridprinted = hybrid_process(hybframe).split('\n')
printed = ''
for y in range(len(gradientprinted)):
gradienttrimmed = gradientprinted[y][(int((
terminalwidth - dw) / 2) - int((quadwidth - dqw) / 2)):]
printed = printed + gradienttrimmed + laplacianprinted[y] + '\n'
for y in range(len(hybridprinted)):
printed = printed + ' ' * (int((
terminalwidth - dqw) / 2) - int((terminalwidth - dw) / 2))
printed = printed + hybridprinted[
y] + '\n' * (y != (len(hybridprinted) - 1))
return printed
def run():
"""Capture and process feed from webcam"""
while True:
rawframe = capture_frame()
printed = all_process(rawframe)
os.system('clear')
print(printed)
WaitKey(1)
if __name__ == '__main__':
# Settings for frame capture
wc = CaptureFromCAM(0)
asciiGroup = "#*oahbpqwmzcunxrt-+~<>:,^`. "
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
quadwidth = 152
desiredquadwidth = 110
dqw = desiredquadwidth
terminalwidth = 304
desiredwidth = 222
dw = desiredwidth
scaledsize = (dqw, int(dqw / 2 * hght // wdth))
# Initialize OpenCV images
inputframe = CreateImage(windowsize, 8, 1)
rawframe = CreateImage(windowsize, 8, 1)
gradframe = CreateImage(windowsize, 8, 1)
lapframe = CreateImage(windowsize, 8, 1)
hybframe = CreateImage(windowsize, 8, 1)
edges = CreateImage(windowsize, IPL_DEPTH_16S, 1)
scaled = CreateImage(scaledsize, 8, 1)
run()