I have a program that will run 24/7 getting frames from a camera, doing processing and sending .jpg images via local network. Generally, I don't want any saving of video to file, however I might want to schedule x minutes of saving on certain occasion (not triggered, scheduled).
I handle video recording by calling VideoWriter as a thread of VideoCamera. I found this to be more accurate to handle writing with correct fps. This process works perfectly when I want to record from the beginning and when I just want to stream. I initiate the camera like this.
import ...
# from custom file import `VideoCamera` which has access to `VideoWriter`
from camera import VideoCamera
video_camera = VideoCamera(
flip = False,
usePiCamera = False,
resolution = (640, 480),
record = False,
record_duration = None,
record_timestamp = True
)
The camera can't be initialized twice (can't access same camera twice). So I was thinking about scheduling a stop and restart with new parameters (for example record = True, record_duration = "00:10:00"
).
I call the script from console (python main.py
) which has:
if __name__ == '__main__':
t = threading.Thread(target=processing_fun, args=())
t.daemon = True
t.start()
print("To see feed connect to " + get_ip_address() + ":5000")
# to do, read ifconfig and assign IP using raspberry's IP
app.run(host='0.0.0.0', port = 5000, debug=False)
processing_fun
will be dead if I do del(video_camera)
because it needs frames from camera. Same for the stream. I'm not sure there's a way to delete the camera without breaking the threads.
Idea to solve the problem
I was thinking about a way to
- Do init of
video_camera
without record - At given moment, cleanly stop main.py (or kill it if not possible)
- Restart main.py with new parameters for video_camera
- This might involve saving a cam_config file, which I'm fine with
- repeat 3) and 4) on needed schedule
Places I have looked for help
I have looked here and here but I am not sure how to put things together in a scheduled way.