-
Notifications
You must be signed in to change notification settings - Fork 31
/
capture.py
51 lines (40 loc) · 1.12 KB
/
capture.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
import cv
import os
import sys
import math
import curses
import signal
def signal_handler(signal, frame):
print 'You pressed Ctrl + C!'
curses.endwin()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
stdscr = curses.initscr()
palette = [' ', '.', '.', '/', 'c', '(', '@', '#', '8']
capture = cv.CaptureFromCAM(0)
# Get the width and height from the terminal (console)
(rows, columns) = os.popen('stty size', 'r').read().split()
rows = int(rows)
columns = int(columns)
while True:
# Capture the image
img = cv.QueryFrame(capture)
thumbnail = cv.CreateImage(
(columns, rows),
img.depth,
img.nChannels
)
cv.Resize(img, thumbnail)
img = thumbnail
# Print the output
for x in xrange(img.height):
for y in xrange(img.width):
b, g, r = img[x, y]
value = b * 0.1145 + g * 0.5866 + r * 0.2989
index = int(math.floor(value / (256.0 / (len(palette)))))
try:
stdscr.move(x, y)
stdscr.addch(palette[index])
except:
pass
stdscr.refresh()