From 287a4f75f74ddba0f90e20f0642c6d0b43487d74 Mon Sep 17 00:00:00 2001 From: Vishesh-dd4723 Date: Sun, 9 Oct 2022 16:46:09 +0530 Subject: [PATCH] feat: Keyboard controls --- generate.py | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/generate.py b/generate.py index 114b293..a8fe03f 100644 --- a/generate.py +++ b/generate.py @@ -7,7 +7,7 @@ import numpy as np class AMP(): - def __init__(self, chars_id=3, rLH=159, rUH=14, gLH=36, gUH=80, bLH=104, bUH=138): + def __init__(self, chars_id=0, rLH=159, rUH=14, gLH=36, gUH=80, bLH=104, bUH=138): ASCII_CHAR_ARRAY = (" .:-=+*#%@", " .,:ilwW", " ▏▁░▂▖▃▍▐▒▀▞▚▌▅▆▊▓▇▉█", " `^|1aUBN", " .`!?xyWN") self.ASCII_CHARS = ASCII_CHAR_ARRAY[chars_id] @@ -29,6 +29,9 @@ def __init__(self, chars_id=3, rLH=159, rUH=14, gLH=36, gUH=80, bLH=104, bUH=138 self.bLowerHue = bLH self.bUpperHue = bUH + self.SPACEBAR_KEY = (32,) + self.QUIT_KEYS = (113,81) + def vid_render(self, pixels, coloring = True): """Function to merge and render the ASCII output to the terminal. @@ -53,9 +56,11 @@ def vid_render(self, pixels, coloring = True): def subtitle_show(self, subs, tstamp_ms): """Function to get subtitles of current frame and display them""" - self.captions.clear() - self.captions.border(' ', ' ', 0, 0, ' ', ' ', ' ', ' ') parts = subs.slice(starts_before={'milliseconds': int(tstamp_ms)}, ends_after={'milliseconds': int(tstamp_ms)}) + if parts: + self.captions.clear() + self.captions.border(' ', ' ', 0, 0, ' ', ' ', ' ', ' ') + self.captions.addstr(0, 1, "Captions") self.captions.move(1, 0) for part in parts: @@ -78,8 +83,6 @@ def get_pixel_matrix(self, image): # set image to determined d1 and column size im = image.resize((d1, d2)) pixels = np.reshape(im.getdata(), (d2, d1, 3)) - with open('error.txt', 'a') as f: - print(pixels.shape, file=f) return pixels @@ -138,10 +141,27 @@ def read_media_sub(self, vidfile, coloring, subfile=''): vidcap = cv2.VideoCapture(vidfile) if subfile: subs = pysrt.open(subfile,encoding='latin-1') fps = vidcap.get(cv2.CAP_PROP_FPS) + + self.media.nodelay(True) + + user_input = curses.ERR + while vidcap.isOpened(): - # read frames from the image + # Read frames from the image success, image = vidcap.read() if not success: break + + # Check for user input + user_input = self.media.getch() + + if user_input in self.SPACEBAR_KEY: + user_input = self.media.getch() + while user_input == curses.ERR: + user_input = self.media.getch() + + if user_input in self.QUIT_KEYS: break + + # Process a frame dur = time.process_time() cv2.imwrite("./data/frame.jpg", image) self.print_from_image("./data/frame.jpg", coloring) @@ -151,6 +171,9 @@ def read_media_sub(self, vidfile, coloring, subfile=''): vidcap.release() cv2.destroyAllWindows() + self.media.nodelay(False) + return user_input + def main(self, argv): beginX = 0; beginY = 0 @@ -171,9 +194,10 @@ def main(self, argv): self.captions.border(0, 0, 0, 0, 0, 0, 0, 0) subfile = argv[2] - self.read_media_sub(vidfile,colored_output,subfile) + return_val = self.read_media_sub(vidfile,colored_output,subfile) - self.media.getch() + if (return_val not in self.QUIT_KEYS): + self.media.getch() def run(stdscr): obj = AMP()