Minor style updates
This commit is contained in:
parent
79974a71e5
commit
8031327a5b
5 changed files with 124 additions and 86 deletions
|
|
@ -1,25 +1,29 @@
|
|||
#!/usr/bin/env python
|
||||
#TODO: Completely rewrite for new version of StreamingCamera
|
||||
|
||||
from pprint import pprint
|
||||
from importlib import import_module
|
||||
import time, datetime
|
||||
import time
|
||||
import datetime
|
||||
|
||||
from flask import (
|
||||
Flask, render_template, Response,
|
||||
redirect, request, jsonify, send_file)
|
||||
|
||||
from flask import Flask, render_template, Response, redirect, request, jsonify, send_file
|
||||
#import yaml
|
||||
import numpy as np
|
||||
|
||||
# Raspberry Pi camera module (requires picamera package)
|
||||
#from camera.pi import Camera
|
||||
from openflexure_microscope.camera.pi import StreamingCamera
|
||||
|
||||
app = Flask(__name__)
|
||||
cam = StreamingCamera()
|
||||
|
||||
|
||||
def parse_payload(request):
|
||||
# TODO: Try-except for invalid JSON payloads
|
||||
"""Convert request to JSON. Will eventually handle error-checking."""
|
||||
# TODO: Handle invalid JSON payloads
|
||||
state = request.get_json()
|
||||
return state
|
||||
|
||||
|
||||
def gen(camera):
|
||||
"""Video streaming generator function."""
|
||||
while True:
|
||||
|
|
@ -29,31 +33,29 @@ def gen(camera):
|
|||
yield (b'--frame\r\n'
|
||||
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
'''
|
||||
Video streaming home page.
|
||||
'''
|
||||
"""Video streaming home page."""
|
||||
cam.start_worker() # Start the stream
|
||||
return render_template('index.html')
|
||||
|
||||
|
||||
@app.route('/stream')
|
||||
def stream():
|
||||
'''
|
||||
Video streaming route. Put this in the src attribute of an img tag.
|
||||
'''
|
||||
return Response(gen(cam), mimetype='multipart/x-mixed-replace; boundary=frame')
|
||||
"""Video streaming route. Put this in the src attribute of an img tag."""
|
||||
return Response(
|
||||
gen(cam),
|
||||
mimetype='multipart/x-mixed-replace; boundary=frame')
|
||||
|
||||
|
||||
#TODO: Be able to change the image resolution with an option
|
||||
# TODO: Be able to change the image resolution with an option
|
||||
@app.route('/capture/', methods=['GET', 'POST', 'PUT'])
|
||||
def capture():
|
||||
'''
|
||||
POST/PUT: Capture a single image
|
||||
GET: Return capture status, or download latest capture
|
||||
|
||||
Eg. curl http://192.168.1.140:5000/capture/ -H "Content-Type: application/json" -d '{"store":true}' -X POST
|
||||
'''
|
||||
"""
|
||||
POST/PUT: Capture a single image.
|
||||
GET: Return capture status, or download latest capture.
|
||||
"""
|
||||
if request.method == 'POST' or request.method == 'PUT':
|
||||
state = parse_payload(request)
|
||||
print(state)
|
||||
|
|
@ -63,7 +65,7 @@ def capture():
|
|||
filename = state['filename']
|
||||
else:
|
||||
filename = None
|
||||
|
||||
|
||||
if 'write_to_file' in state:
|
||||
write_to_file = bool(state['write_to_file'])
|
||||
else:
|
||||
|
|
@ -73,7 +75,7 @@ def capture():
|
|||
use_video_port = bool(state['use_video_port'])
|
||||
else:
|
||||
use_video_port = False
|
||||
|
||||
|
||||
if 'resize' in state:
|
||||
resize_h = int(state['resize'])
|
||||
resize_w = int(resize_h*(4/3))
|
||||
|
|
@ -82,7 +84,7 @@ def capture():
|
|||
resize = None
|
||||
|
||||
response = cam.capture(
|
||||
write_to_file=write_to_file,
|
||||
write_to_file=write_to_file,
|
||||
use_video_port=use_video_port,
|
||||
filename=filename,
|
||||
resize=resize)
|
||||
|
|
@ -94,22 +96,24 @@ def capture():
|
|||
download = request.args.get('download')
|
||||
|
||||
state = cam.state
|
||||
|
||||
if (download == 'true' or download == 'True' or download =='1') and cam.image is not None:
|
||||
|
||||
if ((
|
||||
download == 'true' or
|
||||
download == 'True' or
|
||||
download == '1') and
|
||||
cam.image is not None):
|
||||
|
||||
return send_file(cam.image.data, mimetype='image/jpeg')
|
||||
else:
|
||||
return jsonify(state)
|
||||
|
||||
|
||||
@app.route('/record/', methods=['GET', 'POST', 'PUT'])
|
||||
def record():
|
||||
'''
|
||||
POST/PUT: Start or stop a video recording
|
||||
"""
|
||||
POST/PUT: Start or stop a video recording.
|
||||
GET: Return recording status, or download latest recording
|
||||
|
||||
Eg.
|
||||
curl http://192.168.1.140:5000/record/ -H "Content-Type: application/json" -d '{"status":true}' -X POST
|
||||
curl http://192.168.1.140:5000/record/ -H "Content-Type: application/json" -d '{"status":false}' -X POST
|
||||
'''
|
||||
"""
|
||||
if request.method == 'POST' or request.method == 'PUT':
|
||||
state = request.get_json()
|
||||
print(state)
|
||||
|
|
@ -125,32 +129,40 @@ def record():
|
|||
status = bool(state['status'])
|
||||
|
||||
# synchronise the arduino_time
|
||||
if status == True:
|
||||
if status is True:
|
||||
response = cam.start_recording(filename=filename)
|
||||
return str(response)
|
||||
|
||||
elif status == False:
|
||||
elif status is False:
|
||||
response = cam.stop_recording()
|
||||
return str(response)
|
||||
|
||||
|
||||
else:
|
||||
download = request.args.get('download')
|
||||
|
||||
state = cam.state
|
||||
|
||||
if (download == 'true' or download == 'True' or download =='1') and cam.state_record['recent'] is not None:
|
||||
return send_file(cam.state_record['recent'],
|
||||
|
||||
if ((
|
||||
download == 'true' or
|
||||
download == 'True' or
|
||||
download == '1') and
|
||||
cam.state['recent_video'] is not None):
|
||||
|
||||
return send_file(
|
||||
cam.state['recent_video'],
|
||||
mimetype='video/H264',
|
||||
as_attachment=True)
|
||||
else:
|
||||
return jsonify(state)
|
||||
|
||||
|
||||
@app.route('/preview/', methods=['GET', 'POST', 'PUT'])
|
||||
def preview():
|
||||
'''
|
||||
POST/PUT: Start or stop the direct-to-GPU camera preview on the Pi
|
||||
"""
|
||||
POST/PUT: Start or stop the direct-to-GPU camera preview on the Pi.
|
||||
|
||||
GET: Return preview status
|
||||
'''
|
||||
"""
|
||||
if request.method == 'POST' or request.method == 'PUT':
|
||||
state = request.get_json()
|
||||
print(state)
|
||||
|
|
@ -158,13 +170,13 @@ def preview():
|
|||
if 'status' in state:
|
||||
status = bool(state['status'])
|
||||
|
||||
if status == False:
|
||||
if status is False:
|
||||
response = cam.stop_preview()
|
||||
else:
|
||||
response = cam.start_preview()
|
||||
|
||||
return str(response)
|
||||
|
||||
|
||||
else:
|
||||
# Get args passed via URL (not useful here. Just for reference.)
|
||||
state = cam.state
|
||||
|
|
@ -172,4 +184,4 @@ def preview():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', threaded=True, debug=True, use_reloader=False)
|
||||
app.run(host='0.0.0.0', threaded=True, debug=True, use_reloader=False)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue