|
|
|
@ -1,72 +1,83 @@ |
|
|
|
|
from datetime import datetime, date, time, timedelta |
|
|
|
|
import os |
|
|
|
|
import cv2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def five_minute_segment_from(the_time): |
|
|
|
|
return the_time.replace(minute=(the_time.minute // 1)*1, second=0, microsecond=0) |
|
|
|
|
|
|
|
|
|
def video_address_for(time_stamp): |
|
|
|
|
def path_to_video_at(time_stamp): |
|
|
|
|
return 'videos/' + str(time_stamp) + '.mkv' |
|
|
|
|
|
|
|
|
|
#delay for long delay |
|
|
|
|
long_delay = timedelta(hours=1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
queue = [(False, None)] * 100 #queue of recent frames |
|
|
|
|
last_time_stamp = five_minute_segment_from(datetime.now()) |
|
|
|
|
|
|
|
|
|
# live webcam feed |
|
|
|
|
camera = cv2.VideoCapture(0) |
|
|
|
|
camera.set(cv2.CAP_PROP_FPS, 30.0) |
|
|
|
|
if not camera.isOpened(): |
|
|
|
|
print("Cannot open camera") |
|
|
|
|
exit() |
|
|
|
|
|
|
|
|
|
# video writer |
|
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'H264') |
|
|
|
|
out = cv2.VideoWriter(video_address_for(last_time_stamp), fourcc, camera.get(cv2.CAP_PROP_FPS), (int(camera.get(3)),int(camera.get(4)))) |
|
|
|
|
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)))) |
|
|
|
|
|
|
|
|
|
# video feed from previously captured video |
|
|
|
|
old_time = cv2.VideoCapture(video_address_for(last_time_stamp)) |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
idx = 0 |
|
|
|
|
offset = 0 |
|
|
|
|
longterm = False |
|
|
|
|
while(True): |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
out.release() |
|
|
|
|
out = cv2.VideoWriter(video_address_for(last_time_stamp), |
|
|
|
|
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))) |
|
|
|
|
) |
|
|
|
|
if old_time.isOpened(): |
|
|
|
|
old_time.release() |
|
|
|
|
old_time = cv2.VideoCapture(video_address_for(five_minute_segment_from(datetime.now() - timedelta(minutes=10)))) |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
last_time_stamp = five_minute_segment_from(datetime.now()) |
|
|
|
|
|
|
|
|
|
ret, frame = camera.read() |
|
|
|
|
frame = cv2.flip(frame, 1) |
|
|
|
|
|
|
|
|
|
#recod the frame |
|
|
|
|
# record the frame to the appropriate file, and also to the short term buffer |
|
|
|
|
out.write(frame) |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
showo, fr = queue[(idx - offset) % len(queue)] |
|
|
|
|
if longterm: |
|
|
|
|
ret2, frame2 = old_time.read() |
|
|
|
|
if ret2: |
|
|
|
|
cv2.imshow('vid', frame2) |
|
|
|
|
elif showo: |
|
|
|
|
cv2.imshow('vid', fr) |
|
|
|
|
|
|
|
|
|
# the 'q' button is set as the quitting button you may use any desired button of your choice |
|
|
|
|
key = cv2.waitKey(1) & 0xFF |
|
|
|
|
if key == ord('q'): |
|
|
|
|
key = cv2.waitKey(16) # listen for keyprress for some number of ms |
|
|
|
|
if key & 0xFF == ord('q') or key == 27: # q or esc |
|
|
|
|
break |
|
|
|
|
if key == ord('a'): |
|
|
|
|
if key & 0xFF == ord('a'): |
|
|
|
|
offset += 1 |
|
|
|
|
if key == ord('d'): |
|
|
|
|
if key & 0xFF == ord('d'): |
|
|
|
|
offset -= 1 |
|
|
|
|
if key == ord('z'): |
|
|
|
|
if key & 0xFF == ord('z'): |
|
|
|
|
longterm = not longterm |
|
|
|
|
|
|
|
|
|
idx = (idx + 1) % len(queue) |
|
|
|
|