diff --git a/.gitignore b/.gitignore index 8a2eb62..530cc46 100644 --- a/.gitignore +++ b/.gitignore @@ -184,3 +184,9 @@ poetry.toml pyrightconfig.json # End of https://www.toptal.com/developers/gitignore/api/python + + +# Custom ignores +src/nenuscanner/static/feed.jpg +db.sqlite + diff --git a/src/nenuscanner/camera.py b/src/nenuscanner/camera.py index 414e163..de9f333 100644 --- a/src/nenuscanner/camera.py +++ b/src/nenuscanner/camera.py @@ -1,4 +1,6 @@ import subprocess + +from flask import jsonify import gphoto2 as gp import shutil from . import leds, config @@ -75,6 +77,16 @@ class RealCamera(Camera): except Exception as e: print('An error occured when capturing photo', e) return None + + def capture_preview(self): + try: + subprocess.run( + "gphoto2 --capture-preview --stdout > src/nenuscanner/static/feed.jpg", + shell=True, check=True + ) + except subprocess.CalledProcessError as e: + print('An error occured when capturing photo', e) + raise CameraException(f"Erreur lors de la capture de l'image: {e}") def save(self, capture, output_file): preview = self.inner.file_get(capture.folder, capture.name[:-3] + 'JPG', gp.GP_FILE_TYPE_NORMAL) @@ -146,3 +158,8 @@ def config(): def set_config(parameter, value): return camera.set_config(parameter, value) + +class CameraException(Exception): + """Exception personnalisée pour les erreurs liées à la caméra.""" + def __init__(self, message): + super().__init__(message) \ No newline at end of file diff --git a/src/nenuscanner/routes/camera.py b/src/nenuscanner/routes/camera.py index 152527b..a050b59 100644 --- a/src/nenuscanner/routes/camera.py +++ b/src/nenuscanner/routes/camera.py @@ -1,8 +1,10 @@ from flask import Blueprint, render_template, request, send_file, jsonify import json +import subprocess from .. import camera as C + blueprint = Blueprint('camera', __name__) # Routes for object management @@ -76,4 +78,16 @@ def get_camera_config(): 'params': section_params }) - return jsonify(grouped_params) \ No newline at end of file + return jsonify(grouped_params) + +@blueprint.route('/capture_preview', methods=['POST']) +def capture_preview(): + """ + Capture un aperçu avec gphoto2 et sauvegarde dans static/feed.jpg + """ + try: + cam = C.get() + cam.capture_preview() + return jsonify({'status': 'ok'}) + except C.CameraException as e: + return jsonify({'status': 'error', 'error': str(e)}), 500 \ No newline at end of file diff --git a/src/nenuscanner/templates/camera.html b/src/nenuscanner/templates/camera.html index 5d3b6f6..a8ecd93 100644 --- a/src/nenuscanner/templates/camera.html +++ b/src/nenuscanner/templates/camera.html @@ -10,29 +10,17 @@
- {#
- Camera Preview
- #} - - - - + + + -
@@ -81,10 +67,10 @@ if (Hls.isSupported()) { window.refreshPreview = function () { const img = document.getElementById('camera-preview'); - const url = '/camera/feed.jpg?' + new Date().getTime(); + const url = '/static/feed.jpg?' + new Date().getTime(); img.src = url; } - //setInterval(window.refreshPreview, 2000); + //setInterval(window.refreshPreview, 1000); // Refresh every second document.addEventListener('DOMContentLoaded', function () { fetch('/camera/config') @@ -162,5 +148,16 @@ if (Hls.isSupported()) { }); }); }); + function CapturePreview() { + fetch('/camera/capture_preview', {method: 'POST'}) + .then(response => response.json()) + .then(data => { + if (data.status === 'ok') { + refreshPreview(); // Rafraîchir l'image après la capture + } else { + alert('Erreur lors de la capture : ' + (data.error || 'inconnue')); + } + }); + } {% endblock extrajs %} \ No newline at end of file diff --git a/stream.sh b/stream.sh new file mode 100755 index 0000000..4b126c3 --- /dev/null +++ b/stream.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# Strame ok mais stream imge fixe +#gphoto2 --stdout --capture-movie | ffmpeg -i - -c:v libx264 -f hls -hls_time 2 -hls_list_size 3 -hls_flags delete_segments src/nenuscanner/static/stream.m3u8 + + +gphoto2 --capture-image-and-download -F 0 -I 2 --stdout | ffmpeg -f image2pipe -framerate 0.5 -i - -r 25 -c:v libx264 -f hls -hls_time 2 -hls_list_size 3 -hls_flags delete_segments -g 50 -keyint_min 50 src/nenuscanner/static/stream.m3u8