trying (and failing?) to get hw acceleration

This commit is contained in:
Shoofle 2024-07-30 00:28:39 -04:00
parent 0d43c980af
commit 7d2ff5d7e3
3 changed files with 82 additions and 51 deletions

View File

@ -5,6 +5,7 @@ name = "pypi"
[packages] [packages]
opencv-python = "*" opencv-python = "*"
wakepy = "*"
[dev-packages] [dev-packages]

11
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "9cd65918174126dae7310623cabe06ec6e5ce3f1bb0e255262749a78a55d3839" "sha256": "943975627c31fb661a3a1e3a77e80b2e5634baf439d615ccba41a14d7edf09b1"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -80,6 +80,15 @@
"index": "pypi", "index": "pypi",
"markers": "python_version >= '3.6'", "markers": "python_version >= '3.6'",
"version": "==4.10.0.84" "version": "==4.10.0.84"
},
"wakepy": {
"hashes": [
"sha256:4e7e820512e0e69f9aa5fdded2d3225e6f7c9f000814300e8c37d1fa88fe1664",
"sha256:fee0722b554a859724f6f47ec28db1d513f15097f235b09ebb1b58f545f90db2"
],
"index": "pypi",
"markers": "python_version >= '3.7'",
"version": "==0.9.1"
} }
}, },
"develop": {} "develop": {}

View File

@ -1,58 +1,79 @@
from datetime import datetime, date, time, timedelta from datetime import datetime, date, time, timedelta
import os import os
import cv2 import cv2
from wakepy import keep
def five_minute_segment_from(the_time): def five_minute_segment_from(the_time):
return the_time.replace(minute=(the_time.minute // 1)*1, second=0, microsecond=0) # 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): def path_to_video_at(time_stamp):
return 'videos/' + str(time_stamp) + '.mkv' return 'videos/' + str(time_stamp) + '.mkv' # mkv required for h264 encoding
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"]="hwaccel;vaapi;hw_decoders_any;vaapi,vdpau" # maybe this enables hardware accelerated ffmpeg?
#delay for long delay #delay for long delay
long_delay = timedelta(hours=1) long_delay = timedelta(minutes=1)
queue = [(False, None)] * 100 #queue of recent frames
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(0, cv2.CAP_FFMPEG)
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(*'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)))) video_writer = cv2.VideoWriter(path_to_video_at(last_time_stamp),
fourcc,
camera.get(cv2.CAP_PROP_FPS),
(int(camera.get(3)),int(camera.get(4))),
(cv2.VIDEOWRITER_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY)
)
# video feed from previously captured video # video feed from previously captured video
old_time = cv2.VideoCapture(path_to_video_at(last_time_stamp)) old_time = cv2.VideoCapture(path_to_video_at(last_time_stamp), cv2.CAP_FFMPEG)
# fullscreen window yess good
cv2.namedWindow("mirror", cv2.WINDOW_NORMAL) cv2.namedWindow("mirror", cv2.WINDOW_NORMAL)
cv2.setWindowProperty("mirror", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) cv2.setWindowProperty("mirror", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
idx = 0 queue = [(False, None)] * 100 #queue of recent frames
offset = 0 idx = 0 # current index into the frame queue
longterm = False short_term_delay_frames = 20 # hoow many frames back are we going in the buffer when we're on a short term delay?
while(True): longterm = False # are we shoowing a long term delay? (versus a sshort term delay)
with keep.presenting():
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
old_file = path_to_video_at(five_minute_segment_from(datetime.now() - long_delay - timedelta(minutes=5))) old_file = path_to_video_at(five_minute_segment_from(datetime.now() - long_delay - timedelta(minutes=1)))
if os.path.isfile(old_file):
print(f"deleting old file {old_file}") print(f"deleting old file {old_file}")
os.remove(old_file) os.remove(old_file)
out.release() video_writer.release()
out = cv2.VideoWriter(path_to_video_at(last_time_stamp), newly_recording_file = path_to_video_at(five_minute_segment_from(datetime.now()))
print(f"opening {newly_recording_file} too record this upcoming ssegment to")
video_writer = cv2.VideoWriter(newly_recording_file,
fourcc, fourcc,
camera.get(cv2.CAP_PROP_FPS), camera.get(cv2.CAP_PROP_FPS),
(int(camera.get(3)), int(camera.get(4))) (int(camera.get(3)), int(camera.get(4))),
(cv2.VIDEOWRITER_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY)
) )
if old_time.isOpened(): if old_time.isOpened():
old_time.release() old_time.release()
new_file = path_to_video_at(five_minute_segment_from(datetime.now() - timedelta(minutes=10))) new_file = path_to_video_at(five_minute_segment_from(datetime.now() - long_delay))
pint(f"opening the previously recorded file {new_file} to read from for long mirror") print(f"opening the previously recorded file {new_file} to read from for long mirror")
old_time = cv2.VideoCapture(new_file) old_time = cv2.VideoCapture(new_file, cv2.CAP_FFMPEG)
last_time_stamp = five_minute_segment_from(datetime.now()) last_time_stamp = five_minute_segment_from(datetime.now())
@ -60,23 +81,23 @@ while(True):
frame = cv2.flip(frame, 1) frame = cv2.flip(frame, 1)
# record the frame to the appropriate file, and also to the short term buffer # record the frame to the appropriate file, and also to the short term buffer
out.write(frame) video_writer.write(frame)
queue[idx] = ret, frame queue[idx] = ret, frame
prev_frame_valid, prev_frame = queue[(idx - offset) % len(queue)] prev_frame_valid, prev_frame = queue[(idx - short_term_delay_frames) % len(queue)]
old_frame_valid, old_frame = old_time.read() old_frame_valid, old_frame = old_time.read()
if not longterm and prev_frame_valid: if not longterm and prev_frame_valid:
cv2.imshow('mirror', prev_frame) cv2.imshow('mirror', prev_frame)
elif longterm and old_frame_valid: elif longterm and old_frame_valid:
cv2.imshow('mirror', old_frame) cv2.imshow('mirror', old_frame)
key = cv2.waitKey(16) # listen for keyprress for some number of 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'): if key & 0xFF == ord('a'):
offset += 1 short_term_delay_frames += 1
if key & 0xFF == ord('d'): if key & 0xFF == ord('d'):
offset -= 1 short_term_delay_frames -= 1
if key & 0xFF == ord('z'): if key & 0xFF == ord('z'):
longterm = not longterm longterm = not longterm