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

83 lines
2.3 KiB

3 months ago
from datetime import datetime, date, time, timedelta
3 months ago
import cv2
3 months ago
def five_minute_segment_from(the_time):
return the_time.replace(second=(the_time.second // 5)*5, microsecond=0)
#minute = the_time.replace(second=0, microsecond=0)
#return minute.replace(minute=(minute.minute // 5)*5)
def video_address_for(time_stamp):
return 'videos/' + str(time_stamp) + '.mp4'
3 months ago
# define a video capture object
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FPS, 20.0)
if not camera.isOpened():
print("Cannot open camera")
exit()
queue = [(False, None)] * 100
3 months ago
last_time_stamp = five_minute_segment_from(datetime.now())
old_time = cv2.VideoCapture(video_address_for(last_time_stamp))
3 months ago
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
3 months ago
out = cv2.VideoWriter("videos/" + str(last_time_stamp) + ".mp4", fourcc, camera.get(cv2.CAP_PROP_FPS), (int(camera.get(3)),int(camera.get(4))))
3 months ago
idx = 0
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()):
out.release()
out = cv2.VideoWriter(video_address_for(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(seconds=30))))
last_time_stamp = five_minute_segment_from(datetime.now())
3 months ago
# Capture the video frame
# by frame
ret, frame = camera.read()
frame = cv2.flip(frame, 1)
#recod the frame
3 months ago
out.write(frame)
3 months ago
queue[idx] = ret, frame
3 months ago
3 months ago
showo, fr = queue[(idx - offset) % len(queue)]
3 months ago
if longterm:
ret2, frame2 = old_time.read()
if ret2:
cv2.imshow('vid', frame2)
elif showo:
3 months ago
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'):
break
if key == ord('a'):
offset += 1
if key == ord('d'):
offset -= 1
3 months ago
if key == ord('z'):
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()