a webcam with a 5 second, 30 minute, or 24 hour delay
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mirror-to-yesterday/webcam.py

90 lines
2.8 KiB

3 months ago
from datetime import datetime, date, time, timedelta
import os
3 months ago
import cv2
3 months ago
def five_minute_segment_from(the_time):
3 months ago
return the_time.replace(minute=(the_time.minute // 1)*1, second=0, microsecond=0)
3 months ago
def path_to_video_at(time_stamp):
3 months ago
return 'videos/' + str(time_stamp) + '.mkv'
#delay for long delay
long_delay = timedelta(hours=1)
3 months ago
queue = [(False, None)] * 100 #queue of recent frames
last_time_stamp = five_minute_segment_from(datetime.now())
3 months ago
3 months ago
# live webcam feed
3 months ago
camera = cv2.VideoCapture(0)
if not camera.isOpened():
print("Cannot open camera")
exit()
3 months ago
# video writer
fourcc = cv2.VideoWriter_fourcc(*'H264')
out = cv2.VideoWriter(path_to_video_at(last_time_stamp), fourcc, camera.get(cv2.CAP_PROP_FPS), (int(camera.get(3)),int(camera.get(4))))
3 months ago
3 months ago
# video feed from previously captured video
old_time = cv2.VideoCapture(path_to_video_at(last_time_stamp))
cv2.namedWindow("mirror", cv2.WINDOW_NORMAL)
cv2.setWindowProperty("mirror", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
3 months ago
3 months ago
idx = 0
3 months ago
offset = 0
3 months ago
longterm = False
3 months ago
while(True):
3 months ago
if last_time_stamp != five_minute_segment_from(datetime.now()):
# find file older than our long delay
old_file = path_to_video_at(five_minute_segment_from(datetime.now() - long_delay - timedelta(minutes=5)))
print(f"deleting old file {old_file}")
os.remove(old_file)
3 months ago
out.release()
out = cv2.VideoWriter(path_to_video_at(last_time_stamp),
3 months ago
fourcc,
camera.get(cv2.CAP_PROP_FPS),
(int(camera.get(3)), int(camera.get(4)))
)
if old_time.isOpened():
old_time.release()
new_file = path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(minutes=10)))
pint(f"opening the previously recorded file {new_file} to read from for long mirror")
old_time = cv2.VideoCapture(new_file)
3 months ago
last_time_stamp = five_minute_segment_from(datetime.now())
3 months ago
3 months ago
ret, frame = camera.read()
3 months ago
frame = cv2.flip(frame, 1)
# record the frame to the appropriate file, and also to the short term buffer
3 months ago
out.write(frame)
3 months ago
queue[idx] = ret, frame
prev_frame_valid, prev_frame = queue[(idx - offset) % len(queue)]
old_frame_valid, old_frame = old_time.read()
if not longterm and prev_frame_valid:
cv2.imshow('mirror', prev_frame)
elif longterm and old_frame_valid:
cv2.imshow('mirror', old_frame)
3 months ago
key = cv2.waitKey(16) # listen for keyprress for some number of ms
if key & 0xFF == ord('q') or key == 27: # q or esc
3 months ago
break
if key & 0xFF == ord('a'):
3 months ago
offset += 1
if key & 0xFF == ord('d'):
3 months ago
offset -= 1
if key & 0xFF == ord('z'):
3 months ago
longterm = not longterm
3 months ago
idx = (idx + 1) % len(queue)
# After the loop release the cap object
camera.release()
3 months ago
out.release()
3 months ago
# Destroy all the windows
cv2.destroyAllWindows()