support multiple cameras

This commit is contained in:
Hendrik Schutter 2023-07-04 22:42:21 +02:00
parent 9e4a217b5f
commit 59e6c5048b
8 changed files with 68 additions and 59 deletions

1
.gitignore vendored
View File

@ -189,3 +189,4 @@ cython_debug/
# .nfs files are created when an open file is removed but is still being accessed # .nfs files are created when an open file is removed but is still being accessed
.nfs* .nfs*
test_images/

View File

@ -1,28 +1,9 @@
# msv-webcam-backend # msv-webcam-backend
useradd --system msvwebcambackend -m
passwd -l msvwebcambackend
- `mkdir /opt/msv-webcam-backend/` - `mkdir /opt/msv-webcam-backend/`
- `cd /opt/msv-webcam-backend/` - `cd /opt/msv-webcam-backend/`
- import `msv_webcam_backend.py` and `config.py` - import `msv_webcam_backend.py`
- Set the constants in `config.py`
- `chmod +x /opt/msv-webcam-backend/msv_webcam_backend.py` - `chmod +x /opt/msv-webcam-backend/msv_webcam_backend.py`
- `chown -R msvwebcambackend /opt/msv-webcam-backend/`
- `nano /etc/systemd/system/msv-webcam-backend.service` - `nano /etc/systemd/system/msv-webcam-backend.service`
- create dst dir `mkdir -p /var/www/html/msv-buehl-moos.de/webcam/data`
- `systemctl daemon-reload && systemctl enable --now msv-webcam-backend.service` - `systemctl daemon-reload && systemctl enable --now msv-webcam-backend.service`
mkdir /var/www/html/msv-buehl-moos.de/webcam/
chown -R wwwrun:wwwrun /var/www/html/msv-buehl-moos.de/
location /webcam {
root /var/www/html/msv-buehl-moos.de;
index index.html;
}

View File

@ -1,8 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Author: Hendrik Schutter, mail@hendrikschutter.com
"""
image_search_path = "./test_images/"
destination_image_full_path = "./www/data/still.jpg"
update_interval = 10 # 10sec

View File

@ -4,11 +4,13 @@ After=syslog.target
After=network.target After=network.target
[Service] [Service]
RestartSec=2s Type=simple
Type=oneshot User=root
User=msvwebcambackend Group=root
Restart=on-failure
RestartSec=5s
WorkingDirectory=/opt/msv-webcam-backend/ WorkingDirectory=/opt/msv-webcam-backend/
ExecStart=/usr/bin/python3 /opt/msv-webcam-backend/msv_webcam_backend.py ExecStart=/usr/bin/python3 /opt/msv-webcam-backend/msv_webcam_backend.py interval=10 search_dir=/home/dockeruser/ftps_server/files/msvcamS search_dir=/home/dockeruser/ftps_server/files/msvcamN destination_dir=/var/www/html/msv-buehl-moos.de/webcam/data/ destination_owner=wwwrun
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -3,53 +3,84 @@
""" Author: Hendrik Schutter, mail@hendrikschutter.com """ Author: Hendrik Schutter, mail@hendrikschutter.com
""" """
import config
import glob import glob
import os import os
import time import time
import shutil import shutil
import sys
def copy_image(src): def copy_image(src, destination_dir_path, destination_owner):
shutil.copy(src, config.destination_image_full_path) # print("src: " + str(src))
os.chmod(config.destination_image_full_path, 644) # print("dst: " + str(destination_dir_path))
shutil.copyfile(src, destination_dir_path)
os.chmod(destination_dir_path, 0o0644)
shutil.chown(destination_dir_path, destination_owner)
def main(): def main():
print("starting ...") print("starting ...")
newest_file_path = "" update_interval = -1
newest_file_size = 0 destination_dir_path = -1
destination_owner = -1
search_dirs = list()
for argument in sys.argv:
if argument.startswith('interval'):
update_interval = int(argument.split("=")[1])
if argument.startswith('destination_dir'):
destination_dir_path = argument.split("=")[1]
if argument.startswith('destination_owner'):
destination_owner = argument.split("=")[1]
if argument.startswith('search_dir'):
tmp_search_dir = {
"search_dir_path" : argument.split("=")[1],
"newest_file_path" : "",
"newest_file_size" : 0
}
search_dirs.append(tmp_search_dir)
if ((update_interval == -1) or (destination_dir_path == -1) or (destination_owner == -1) or (len(search_dirs) == 0)):
print("Unable to parse config!")
print("Example usage:")
print(" python msv_webcam_backend.py interval=10 search_dir=test_images/msvcamS search_dir=test_images/msvcamN destination_dir=www/ destination_owner=hendrik")
sys.exit(-1)
while True: while True:
for search_dir in search_dirs:
while True:
try: try:
# print(config.image_search_path) # print("\nSearch dir: " + str(search_dir["search_dir_path"]))
list_of_files = glob.glob(config.image_search_path+"/**/*.jpg", recursive=True) list_of_files = glob.glob(search_dir["search_dir_path"]+"/**/*.jpg", recursive=True)
# print(list_of_files) # print(list_of_files)
newest_file_path_tmp = max(list_of_files, key=os.path.getctime) newest_file_path_tmp = max(list_of_files, key=os.path.getctime)
newest_file_size_tmp = os.stat(newest_file_path_tmp).st_size newest_file_size_tmp = os.stat(newest_file_path_tmp).st_size
list_of_files.clear()
if (newest_file_path_tmp == newest_file_path) and (newest_file_size_tmp == newest_file_size): if (newest_file_path_tmp == search_dir["newest_file_path"]) and (newest_file_size_tmp == search_dir["newest_file_size"]):
# already found the newest file # already found the newest file
print("no newer file found") print("no newer file found")
break else:
time.sleep(1) # wait 1sec to see if the upload is still in progress
time.sleep(1) # wait 1sec to see if the upload is still in progress list_of_files = glob.glob(search_dir["search_dir_path"]+"/**/*.jpg", recursive=True)
newest_file_path_tmp_delayed = max(list_of_files, key=os.path.getctime)
newest_file_size_tmp_delayed = os.stat(newest_file_path_tmp_delayed).st_size
list_of_files.clear()
list_of_files = glob.glob(config.image_search_path+"/**/*.jpg", recursive=True) if (newest_file_path_tmp == newest_file_path_tmp_delayed) and (newest_file_size_tmp == newest_file_size_tmp_delayed):
newest_file_path = max(list_of_files, key=os.path.getctime) search_dir["newest_file_path"] = newest_file_path_tmp_delayed
newest_file_size = os.stat(newest_file_path).st_size search_dir["newest_file_size"] = newest_file_size_tmp_delayed
print("Found new file:")
if (newest_file_path_tmp == newest_file_path) and (newest_file_size_tmp == newest_file_size): print("Name: " + str(search_dir["newest_file_path"]))
print("Found new file:") print("Size: " + str(search_dir["newest_file_size"]))
print("Name: "+str(newest_file_path)) dst_file_tmp = os.path.join(destination_dir_path, (os.path.basename(os.path.normpath(search_dir["search_dir_path"])) + os.path.splitext(search_dir["newest_file_path"])[1]))
print("Size: " + str(newest_file_size)) copy_image(search_dir["newest_file_path"], dst_file_tmp, destination_owner)
copy_image(newest_file_path) #break
break else:
print("Upload not finished yet!")
except Exception as e: except Exception as e:
print(e) print(e)
#break #break
time.sleep(config.update_interval) time.sleep(update_interval)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

BIN
www/data/msvcamN.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

BIN
www/data/msvcamS.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

View File

@ -8,7 +8,9 @@
<p>You are connected!</p> <p>You are connected!</p>
<img src="data/still.jpg" alt="still webcam image"> <img src="data/msvcamN.jpg" alt="msvcamN still webcam image">
<img src="data/msvcamS.jpg" alt="msvcamS still webcam image">
</body> </body>
</html> </html>