diff --git a/cam.py b/cam.py index eb8818c..a8135ba 100755 --- a/cam.py +++ b/cam.py @@ -3,73 +3,73 @@ import os import sys import platform -from pynput.keyboard import * +from pynput.keyboard import Key, Listener def on_press(key): - finish(key) + finish(key) def on_release(key): - pass + pass listener = Listener(on_press=on_press, on_release=on_release) def finish(key): - if key == Key.esc: - listener.stop() - os._exit(0) + if key == Key.esc: + listener.stop() + os._exit(0) def main(): + vc = None - vc = None + if platform.system() == 'Windows': + vc = cv.VideoCapture(0, cv.CAP_DSHOW) + elif platform.system() == 'Linux': + vc = cv.VideoCapture(0) - if platform.system() == 'Windows': - vc = cv.VideoCapture(0,cv.CAP_DSHOW) + if vc.isOpened(): + rval, frame = vc.read() + else: + rval = False - elif platform.system() == 'Linux': - vc = cv.VideoCapture(0) + if rval: + listener.start() - if vc.isOpened(): - rval, frame = vc.read() - else: - rval = False + while rval: + rval, frame = vc.read() + print(toASCII(frame)) - if rval: - listener.start() + vc.release() # Release the video capture when done + cv.destroyAllWindows() # Close any OpenCV windows - while rval: - rval, frame = vc.read() - print(toASCII(frame)) - + sys.exit() - sys.exit() - -def toASCII(frame, cols = 120, rows = 35): - - frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) - height, width = frame.shape - cell_width = width / cols - cell_height = height / rows - if cols > width or rows > height: - raise ValueError('Too many cols or rows.') - result = "" - for i in range(rows): - for j in range(cols): - gray = np.mean( - frame[int(i * cell_height):min(int((i + 1) * cell_height), height), int(j * cell_width):min(int((j + 1) * cell_width), width)] - ) - result += grayToChar(gray) - result += '\n' - return result +def toASCII(frame, cols=120, rows=35): + frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) + height, width = frame.shape + cell_width = width / cols + cell_height = height / rows + if cols > width or rows > height: + raise ValueError('Too many cols or rows.') + result = "" + for i in range(rows): + for j in range(cols): + gray = np.mean( + frame[int(i * cell_height):min(int((i + 1) * cell_height), height), + int(j * cell_width):min(int((j + 1) * cell_width), width)] + ) + result += grayToChar(gray) + result += '\n' + return result def grayToChar(gray): - CHAR_LIST = ' .:-=+*#%@' # Replace by " .',;:clodxkO0KXNWM" if you want more precision. - num_chars = len(CHAR_LIST) - return CHAR_LIST[ - min( - int(gray * num_chars / 255), - num_chars - 1 - ) - ] - + CHAR_LIST = ' .:-=+*#%@' # Replace by " .',;:clodxkO0KXNWM" for more precision. + num_chars = len(CHAR_LIST) + return CHAR_LIST[ + min( + int(gray * num_chars / 255), + num_chars - 1 + ) + ] + if __name__ == '__main__': - main() + main()