-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcam.py
105 lines (88 loc) · 2.18 KB
/
webcam.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
import cv2
import curses
import configparser
import time
# read the config file
config = configparser.ConfigParser()
config.read('tcconf.ini')
zoom = int(config['DEFAULT']['Zoom'])
invert = bool(config['DEFAULT']['Invert'])
refresh = float(int(config['DEFAULT']['Refresh'])/100)
camsize = int(config["DEFAULT"]["CameraSize"])
c_input = int(config["DEFAULT"]["Input"])
# set up curses
stdscr = curses.initscr()
curses.cbreak()
curses.noecho()
stdscr.keypad(1)
stdscr.nodelay(1)
term_h, term_w = stdscr.getmaxyx()
# set up camera
c_size = round(camsize/zoom)
win = cv2.VideoCapture(c_input)
ret = win.set(3, c_size), win.set(4, c_size)
step_x = c_size / term_w
step_y = c_size / term_h
if invert:
ascii_values = {
50: '%',
75: '#',
100: '*',
125: '+',
150: '-',
175: ':',
210: '.',
255: ' ',
}
else:
ascii_values = {
50: ' ',
75: '.',
100: ':',
125: '-',
150: '+',
175: '*',
210: '#',
255: '%',
}
def xrange(n):
return range(int(n * step_x), int((n+1) * step_x))
def yrange(n):
return range(int(n * step_y), int((n+1) * step_y))
def to_ascii(n):
n = n
while 1:
try:
a = ascii_values[n]
return a
except KeyError:
n += 1
def main():
while 1:
ret, frame = win.read()
M = cv2.getRotationMatrix2D((c_size / 2, c_size / 2), 90, 1)
frame = cv2.warpAffine(frame, M, (c_size, c_size))
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
for x in range(term_w - 1):
for y in range(term_h):
avg = 0
pts = 0
for i in xrange(x):
for j in yrange(y):
avg += gray[i, j]
pts += 1
stdscr.addch(y, x, to_ascii(round(avg / pts)))
c = stdscr.getch()
if c == ord('q'):
break
stdscr.refresh()
time.sleep(refresh)
if __name__ == "__main__":
main()
cv2.destroyAllWindows()
win.release()
curses.nocbreak()
stdscr.keypad(0)
stdscr.nodelay(0)
curses.echo()
curses.endwin()