116 lines
3.6 KiB
Python
116 lines
3.6 KiB
Python
from flask import render_template, Response, flash, jsonify, request, stream_with_context, send_file
|
|
|
|
from phytopi.camera.camera import CameraWorker
|
|
from phytopi.forms import CameraSettingsForm
|
|
|
|
from phytopi import app
|
|
from phytopi import db
|
|
from phytopi.models import Dataset
|
|
|
|
from io import BytesIO
|
|
import zipstream
|
|
|
|
camera = CameraWorker(app=app)
|
|
camera.start()
|
|
# camera = app.camera
|
|
save_frames = False
|
|
|
|
@app.route('/')
|
|
@app.route('/index')
|
|
def index():
|
|
content = {'timelapse': save_frames}
|
|
return render_template('index.html', content=content)
|
|
|
|
@app.route('/toggle_timelapse', methods=['GET', 'POST'])
|
|
def start_stop_timelapse():
|
|
global save_frames
|
|
global current_dataset
|
|
global camera
|
|
if save_frames:
|
|
save_frames = False
|
|
timelapse_interval = None
|
|
btn_text = "Start"
|
|
btn_class = 'btn-primary'
|
|
camera.current_dataset = None
|
|
camera.last_saved = None
|
|
print(" > switched off timelapse mode")
|
|
else:
|
|
save_frames = True
|
|
timelapse_interval = 1200
|
|
btn_text = "Stop"
|
|
btn_class = 'btn-danger'
|
|
dataset = Dataset()
|
|
db.session.add(dataset)
|
|
db.session.commit()
|
|
camera.current_dataset = dataset.id
|
|
print(" > switched on timelapse mode")
|
|
|
|
camera.set_timelapse_interval(timelapse_interval)
|
|
return jsonify(btn_text=btn_text, btn_class=btn_class)
|
|
|
|
|
|
def gen(camera):
|
|
# def gen():
|
|
"""Video streaming generator function."""
|
|
while True:
|
|
frame = camera.get_frame(thumbnail=True)
|
|
yield (b'--frame\r\n'
|
|
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
|
|
|
|
|
@app.route('/data')
|
|
def data():
|
|
return Response(app.config['DATA_PATH'])
|
|
|
|
@app.route('/video_feed')
|
|
def video_feed():
|
|
global camera
|
|
return Response(gen(camera),
|
|
mimetype='multipart/x-mixed-replace; boundary=frame')
|
|
|
|
@app.route('/settings', methods=['GET', 'POST'])
|
|
def settings():
|
|
form = CameraSettingsForm()
|
|
return render_template('settings.html', form=form)
|
|
#global camera
|
|
#camera_settings = {'fps': camera.camera.framerate,
|
|
# 'iso': camera.camera.iso}
|
|
#return render_template('settings.html', settings=camera_settings)
|
|
|
|
@app.route('/datasets')
|
|
@app.route('/datasets/<dataset_id>')
|
|
def datasets(dataset_id=None):
|
|
if dataset_id:
|
|
dataset = Dataset.query.get(int(dataset_id))
|
|
data = dataset.images.all()
|
|
else:
|
|
data = Dataset.query.order_by(Dataset.datetime_created.desc()).all()
|
|
|
|
content = {'data': data,
|
|
'id': dataset_id}
|
|
return render_template('datasets.html', content=content)
|
|
|
|
@app.route('/datasets/<dataset_id>/delete')
|
|
def dataset_delete(dataset_id):
|
|
Dataset.query.filter_by(id=dataset_id).delete()
|
|
db.session.commit()
|
|
return datasets()
|
|
|
|
@app.route('/datasets/<dataset_id>/download')
|
|
def dataset_download(dataset_id):
|
|
images = Dataset.query.get(int(dataset_id)).images.all()
|
|
|
|
def generateZip(images):
|
|
zf = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED)
|
|
for image in images:
|
|
filename = '{}/{}'.format(app.config['DATA_PATH'], image.filename)
|
|
zf.write(filename, arcname=image.filename)
|
|
for chunk in zf:
|
|
yield chunk
|
|
zf.close()
|
|
|
|
|
|
#return send_file(generateZip(images), attachment_filename='dataset_{}.zip'.format(dataset_id), as_attachment=True)
|
|
response = Response(stream_with_context(generateZip(images)), mimetype='application/zip')
|
|
response.headers['Content-Disposition'] = 'attachment; filename=dataset_{}.zip'.format(dataset_id)
|
|
return response
|