refactoring and splitting the demo laptop ver into its own file

This commit is contained in:
Shoofle 2024-08-07 12:58:29 -04:00
parent 86be5bf621
commit 9265070f9b
2 changed files with 152 additions and 18 deletions

View File

@ -6,7 +6,7 @@ import math
from gpiod.line import Direction, Value from gpiod.line import Direction, Value
#from wakepy import keep #from wakepy import keep
def five_minute_segment_from(the_time): def minute_segment_from(the_time):
# this is mocked out too be a one minute segment instead # this is mocked out too be a one minute segment instead
return the_time.replace(second=0, microsecond=0) return the_time.replace(second=0, microsecond=0)
#return the_time.replace(minute=(the_time.minute // 1)*1, second=0, microsecond=0) #return the_time.replace(minute=(the_time.minute // 1)*1, second=0, microsecond=0)
@ -18,7 +18,7 @@ def path_to_video_at(time_stamp):
#delay for long delay #delay for long delay
long_delay = timedelta(minutes=1) long_delay = timedelta(minutes=1)
last_time_stamp = five_minute_segment_from(datetime.now()) last_time_stamp = minute_segment_from(datetime.now())
# live webcam feed # live webcam feed
camera = cv2.VideoCapture("/dev/video0") camera = cv2.VideoCapture("/dev/video0")
@ -78,15 +78,15 @@ with gpiod.request_lines(
} }
) as io_lines: ) as io_lines:
while(True): while(True):
if last_time_stamp != five_minute_segment_from(datetime.now()): if last_time_stamp != minute_segment_from(datetime.now()):
# find file older than our long delay # find file older than our long delay
oldest_file_path = path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(minutes=5) - timedelta(minutes=10))) oldest_file_path = path_to_video_at(minute_segment_from(datetime.now() - timedelta(hours=24) - timedelta(minutes=10)))
if os.path.isfile(oldest_file_path): if os.path.isfile(oldest_file_path):
print(f"deleting old file {oldest_file_path}") print(f"deleting old file {oldest_file_path}")
os.remove(oldest_file_path) os.remove(oldest_file_path)
video_writer.release() video_writer.release()
newly_recording_file_path = path_to_video_at(five_minute_segment_from(datetime.now())) newly_recording_file_path = path_to_video_at(minute_segment_from(datetime.now()))
print(f"opening {newly_recording_file_path} too record this upcoming ssegment to") print(f"opening {newly_recording_file_path} too record this upcoming ssegment to")
video_writer = cv2.VideoWriter(newly_recording_file_path, video_writer = cv2.VideoWriter(newly_recording_file_path,
@ -98,18 +98,16 @@ with gpiod.request_lines(
if twenty_four_hours_ago.isOpened(): if twenty_four_hours_ago.isOpened():
twenty_four_hours_ago.release() twenty_four_hours_ago.release()
#twenty_four_hours_ago = cv2.VideoCapture(path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(hours=24)))) twenty_four_hours_ago = cv2.VideoCapture(path_to_video_at(minute_segment_from(datetime.now() - timedelta(hours=24))))
twenty_four_hours_ago = cv2.VideoCapture(path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(minutes=3))))
if twelve_hours_ago.isOpened(): if twelve_hours_ago.isOpened():
twelve_hours_ago.release() twelve_hours_ago.release()
#twelve_hours_ago = cv2.VideoCapture(path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(hours=12)))) twelve_hours_ago = cv2.VideoCapture(path_to_video_at(minute_segment_from(datetime.now() - timedelta(hours=12))))
twelve_hours_ago = cv2.VideoCapture(path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(minutes=2))))
if five_minutes_ago.isOpened(): if five_minutes_ago.isOpened():
five_minutes_ago.release() five_minutes_ago.release()
five_minutes_ago = cv2.VideoCapture(path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(minutes=1)))) five_minutes_ago = cv2.VideoCapture(path_to_video_at(minute_segment_from(datetime.now() - timedelta(minutes=5))))
last_time_stamp = five_minute_segment_from(datetime.now()) last_time_stamp = minute_segment_from(datetime.now())
ret, frame = camera.read() ret, frame = camera.read()
#frame = cv2.flip(frame, 1) #frame = cv2.flip(frame, 1)
@ -145,12 +143,7 @@ with gpiod.request_lines(
key = cv2.waitKey(16) # listen for keyprress for 16 ms key = cv2.waitKey(16) # listen for keyprress for 16 ms
if key & 0xFF == ord('q') or key == 27: # q or esc if key & 0xFF == ord('q') or key == 27: # q or esc
break break
"""if key & 0xFF == ord('a'):
short_term_delay_frames += 1
if key & 0xFF == ord('d'):
short_term_delay_frames -= 1
if key & 0xFF == ord('z'):
longterm = not longterm"""
# After the loop release the cap object # After the loop release the cap object
camera.release() camera.release()

141
webcam_demo.py Normal file
View File

@ -0,0 +1,141 @@
from datetime import datetime, date, time, timedelta
import os
import cv2
import math
from wakepy import keep
def five_minute_segment_from(the_time):
# this is mocked out too be a one minute segment instead
return the_time.replace(second=0, microsecond=0)
#return the_time.replace(minute=(the_time.minute // 1)*1, second=0, microsecond=0)
def path_to_video_at(time_stamp):
return 'videos/' + str(time_stamp) + '.mp4' # mkv required for h264 encoding
last_time_stamp = five_minute_segment_from(datetime.now())
# live webcam feed
camera = cv2.VideoCapture(0)
if not camera.isOpened():
print("Cannot open camera")
exit()
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
camera.set(cv2.CAP_PROP_FPS, 60.0)
camera_fps = camera.get(cv2.CAP_PROP_FPS)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
video_writer = cv2.VideoWriter(path_to_video_at(last_time_stamp),
fourcc,
camera_fps,
(int(camera.get(3)),int(camera.get(4))), #resolution
(cv2.VIDEOWRITER_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY)
)
# video feed from previously captured video
twenty_four_hours_ago = cv2.VideoCapture(path_to_video_at(last_time_stamp - timedelta(hours=24)))
twelve_hours_ago = cv2.VideoCapture(path_to_video_at(last_time_stamp - timedelta(hours=12)))
five_minutes_ago = cv2.VideoCapture(path_to_video_at(last_time_stamp - timedelta(minutes=5)))
# fullscreen window yess good
cv2.namedWindow("mirror", cv2.WINDOW_NORMAL)
cv2.setWindowProperty("mirror", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
queue = [(False, None)] * math.floor(camera_fps * 32) #queue of 32 seconds of frames
idx = 0 # current index into the frame queue
lines = {
"five_second": 2,
"thirty_second": 3,
"five_minute": 4,
"twelve_hour": 5,
"twenty_four_hour": 6
}
mode = 4
#with keep.presenting():
with keep.presenting():
while(True):
if last_time_stamp != five_minute_segment_from(datetime.now()):
oldest_file_path = path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(minutes=5) - timedelta(minutes=10)))
if os.path.isfile(oldest_file_path):
print(f"deleting old file {oldest_file_path}")
os.remove(oldest_file_path)
video_writer.release()
newly_recording_file_path = path_to_video_at(five_minute_segment_from(datetime.now()))
print(f"opening {newly_recording_file_path} too record this upcoming ssegment to")
video_writer = cv2.VideoWriter(newly_recording_file_path,
fourcc,
camera_fps,
(int(camera.get(3)), int(camera.get(4))),
(cv2.VIDEOWRITER_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY)
)
if twenty_four_hours_ago.isOpened():
twenty_four_hours_ago.release()
#twenty_four_hours_ago = cv2.VideoCapture(path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(hours=24))))
twenty_four_hours_ago = cv2.VideoCapture(path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(minutes=3))))
if twelve_hours_ago.isOpened():
twelve_hours_ago.release()
#twelve_hours_ago = cv2.VideoCapture(path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(hours=12))))
twelve_hours_ago = cv2.VideoCapture(path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(minutes=2))))
if five_minutes_ago.isOpened():
five_minutes_ago.release()
five_minutes_ago = cv2.VideoCapture(path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(minutes=1))))
last_time_stamp = five_minute_segment_from(datetime.now())
ret, frame = camera.read()
#frame = cv2.flip(frame, 1)
# record the frame to the appropriate file, and also to the short term buffer
video_writer.write(frame)
queue[idx] = ret, frame
frames = {
"five_second": queue[(idx - math.floor(5*camera_fps)) % len(queue)],
"thirty_second": queue[(idx - math.floor(30*camera_fps)) % len(queue)],
"five_minute": five_minutes_ago.read(),
"twelve_hour": twelve_hours_ago.read(),
"twenty_four_hour": twenty_four_hours_ago.read(),
}
#valid, the_frame = frames.five_second
if mode == 0:
valid, the_frame = frames["twenty_four_hour"]
elif mode == 1:
valid, the_frame = frames["twelve_hour"]
elif mode == 2:
valid, the_frame = frames["five_minute"]
elif mode == 3:
valid, the_frame = frames["thirty_second"]
else: #if not io_lines.get_value(lines.five_second)
valid, the_frame = frames["five_second"]
if valid:
cv2.imshow("mirror", the_frame)
idx = (idx + 1) % len(queue)
key = cv2.waitKey(16) # listen for keyprress for 16 ms
if key & 0xFF == ord('q') or key == 27: # q or esc
break
if key & 0xFF == ord('a'):
mode = (mode - 1) % 5
if key & 0xFF == ord('d'):
mode = (mode + 1) % 5
# After the loop release the cap object
camera.release()
video_writer.release()
twenty_four_hours_ago.release()
twelve_hours_ago.release()
five_minutes_ago.release()
# Destroy all the windows
cv2.destroyAllWindows()