Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Fix #16

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 50 additions & 50 deletions cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()