Last active
April 22, 2020 18:17
-
-
Save botcs/5ab998d5e54347a3b251bc9e897e2229 to your computer and use it in GitHub Desktop.
a delayed mirror with minimal rewind functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
import time | |
cap = cv2.VideoCapture(0) | |
FRAME_DELAY = 120 | |
cv2.namedWindow('frame', cv2.WINDOW_FREERATIO) | |
# cv2.setWindowProperty('frame', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) | |
unseen_buffer = [] | |
history_buffer = [] | |
for i in range(FRAME_DELAY): | |
ret, frame = cap.read() | |
unseen_buffer.append(frame) | |
def mirror(pressed_key): | |
# Capture frame-by-frame | |
t = time.time() | |
ret, frame = cap.read() | |
unseen_buffer.append(frame) | |
frame = unseen_buffer.pop(0) | |
history_buffer.append(frame) | |
if len(history_buffer) > 30 * 600: # translates to 10 min @ 30 FPS | |
history_buffer.pop(0) | |
cv2.imshow('frame',frame) | |
print(f"FPS:{1/(time.time()-t)}") | |
def replay(pressed_key): | |
global frame_idx | |
if pressed_key == ord(','): | |
frame_idx = frame_idx-1 | |
if frame_idx < 0: | |
frame_idx = 0 | |
print("Oldest frame, can't go back further") | |
if pressed_key == ord('.'): | |
frame_idx = frame_idx+1 | |
if frame_idx > len(history_buffer)-1: | |
frame_idx = len(history_buffer)-1 | |
print("Newest frame, throw a flip already... bitch!") | |
frame = history_buffer[frame_idx] | |
cv2.imshow('frame',frame) | |
PAUSE = False | |
while(True): | |
pressed_key = cv2.waitKey(30) & 0xFF | |
if pressed_key == ord('q'): | |
breakm | |
elif pressed_key == ord(' '): | |
PAUSE = not PAUSE | |
if PAUSE: | |
frame_idx = len(history_buffer) - 1 | |
print("Rewind time!") | |
else: | |
print("Resume mirroring") | |
if not PAUSE: | |
mirror(pressed_key) | |
else: | |
replay(pressed_key) | |
# When everything done, release the capture | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment