set up to work with the raspi gpio and such

This commit is contained in:
Shoofle 2024-08-05 13:40:03 -04:00
parent 996f417185
commit a93434251c

View File

@ -2,7 +2,8 @@ from datetime import datetime, date, time, timedelta
import os import os
import cv2 import cv2
import gpiod import gpiod
from gpiod import Direction, Value import math
from gpiod.line import Direction, Value
#from wakepy import keep #from wakepy import keep
def five_minute_segment_from(the_time): def five_minute_segment_from(the_time):
@ -20,19 +21,21 @@ long_delay = timedelta(minutes=1)
last_time_stamp = five_minute_segment_from(datetime.now()) last_time_stamp = five_minute_segment_from(datetime.now())
# live webcam feed # live webcam feed
camera = cv2.VideoCapture(0) camera = cv2.VideoCapture("/dev/video0")
if not camera.isOpened(): if not camera.isOpened():
print("Cannot open camera") print("Cannot open camera")
exit() exit()
# video writer # video writer
fourcc = cv2.VideoWriter_fourcc(*'H264') fourcc = cv2.VideoWriter_fourcc(*'x264')
camera_fps = camera.get(cv2.CAP_PROP_FPS) 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), video_writer = cv2.VideoWriter(path_to_video_at(last_time_stamp),
fourcc, fourcc,
camera_fps, camera_fps,
(int(camera.get(3)),int(camera.get(4))), #resolution (int(camera.get(3)),int(camera.get(4))), #resolution
(cv2.VIDEOWRITER_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY)
) )
# video feed from previously captured video # video feed from previously captured video
@ -50,30 +53,31 @@ queue = [(False, None)] * math.floor(camera_fps * 32) #queue of 32 seconds of fr
idx = 0 # current index into the frame queue idx = 0 # current index into the frame queue
lines = { lines = {
"five_second": 1, "five_second": 2,
"thirty_second": 2, "thirty_second": 3,
"five_minute": 3, "five_minute": 4,
"twelve_hour": 4, "twelve_hour": 5,
"twenty_four_hour": 5 "twenty_four_hour": 6
} }
input_line = gpiod.LineSettings(direction=Direction.INPUT)
#with keep.presenting(): #with keep.presenting():
with gpiod.request_lines( with gpiod.request_lines(
"/dev/gpiochip0", "/dev/gpiochip4",
consumer="mirror-to-yesterday", consumer="mirror-to-yesterday",
config={ config={
lines.five_second: gpiod.LineSettings(direction=Direction.INPUT), lines["five_second"]: input_line,
lines.thirty_second: gpiod.LineSettings(direction=Direction.INPUT), lines["thirty_second"]: input_line,
lines.five_minute: gpiod.LineSettings(direction=Direction.INPUT), lines["five_minute"]: input_line,
lines.twelve_hour: gpiod.LineSettings(direction=Direction.INPUT), lines["twelve_hour"]: input_line,
lines.twenty_four_hour: gpiod.LineSettings(direction=Direction.INPUT) lines["twenty_four_hour"]: input_line,
} }
) as io_lines: ) as io_lines:
while(True): while(True):
if last_time_stamp != five_minute_segment_from(datetime.now()): if last_time_stamp != five_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(hours=24) - timedelta(minutes=10))) 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): 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)
@ -91,13 +95,15 @@ 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(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(): 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(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(): 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=5)))) 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()) last_time_stamp = five_minute_segment_from(datetime.now())
@ -117,16 +123,16 @@ with gpiod.request_lines(
} }
#valid, the_frame = frames.five_second #valid, the_frame = frames.five_second
if io_lines.get_value(lines.twenty_four_hour): if not io_lines.get_value(lines["twenty_four_hour"]):
valid, the_frame = frames.twenty_four_hour valid, the_frame = frames["twenty_four_hour"]
elif io_lines.get_value(lines.twelve_hour): elif not io_lines.get_value(lines["twelve_hour"]):
valid, the_frame = frames.twelve_hour valid, the_frame = frames["twelve_hour"]
elif io_lines.get_value(lines.five_minute): elif not io_lines.get_value(lines["five_minute"]):
valid, the_frame = frames.five_minute valid, the_frame = frames["five_minute"]
elif io_lines.get_value(lines.thirty_second): elif not io_lines.get_value(lines["thirty_second"]):
valid, the_frame = frames.thirty_second valid, the_frame = frames["thirty_second"]
else: #if io_lines.get_value(lines.five_second) else: #if not io_lines.get_value(lines.five_second)
valid, the_frame = frames.five_second valid, the_frame = frames["five_second"]
if valid: if valid:
cv2.imshow("mirror", the_frame) cv2.imshow("mirror", the_frame)